on master help sign in
Truther/README.md → README.mdrenamed+7−6
since splitsnapshots
unifiedsplit
@@ -2,6 +2,7 @@
2
2
3Makes a truth table for the given boolean function
3Makes a truth table for the given boolean function
4
4
5+*All operators have equal precedence and are left-associative*
5
6
6## Usage
7## Usage
7
8
@@ -24,13 +25,13 @@ a | b | c | F
24
25
25## Available functions
26## Available functions
26
27
27−- `+`/`or`
28+- `+` `or`
28−- `(+)`/`xor`/`/=`
29+- `(+)` `xor` `/=`
29−- `*`/`and`/`&`
30+- `*` `and` `&`
30- `=>`
31- `=>`
31- `=/>`
32- `=/>`
32- `<=`
33- `<=`
33- `</=`
34- `</=`
34−- `o`/`nor`
35+- `o` `nor`
35−- `=`/`<=>`
36+- `=` `<=>`
36−- `|`/`nand`
37+- `|` `nand`
Truther.xcodeproj/project.pbxproj+1−1
@@ -48,6 +48,7 @@
48 097824ED1F87E21400064ADB = {
48 097824ED1F87E21400064ADB = {
49 isa = PBXGroup;
49 isa = PBXGroup;
50 children = (
50 children = (
51+ 0915F8131F8907DB00D96279 /* README.md */,
51 097824F81F87E21400064ADB /* Truther */,
52 097824F81F87E21400064ADB /* Truther */,
52 097824F71F87E21400064ADB /* Products */,
53 097824F71F87E21400064ADB /* Products */,
53 );
54 );
@@ -66,7 +67,6 @@
66 children = (
67 children = (
67 097824F91F87E21400064ADB /* main.swift */,
68 097824F91F87E21400064ADB /* main.swift */,
68 097825001F87E23000064ADB /* Truther.swift */,
69 097825001F87E23000064ADB /* Truther.swift */,
69− 0915F8131F8907DB00D96279 /* README.md */,
70 091685E21F8834C800CC8645 /* Formatter.swift */,
70 091685E21F8834C800CC8645 /* Formatter.swift */,
71 097825021F87E48500064ADB /* Operator.swift */,
71 097825021F87E48500064ADB /* Operator.swift */,
72 );
72 );
Truther/Operator.swift+1−1
@@ -44,7 +44,7 @@ enum Operator {
44 case .f7:
44 case .f7:
45 return {$0 || $1}
45 return {$0 || $1}
46 case .f8:
46 case .f8:
47− return {!($0 && $1)}
47+ return {!($0 || $1)}
48 case .f9:
48 case .f9:
49 return {$0 == $1}
49 return {$0 == $1}
50 case .f10:
50 case .f10:
Truther/Truther.swift+19
@@ -45,6 +45,12 @@ struct Formula {
45 return startingLeft
45 return startingLeft
46 }
46 }
47 var ns = s.trimmingCharacters(in: .whitespaces)
47 var ns = s.trimmingCharacters(in: .whitespaces)
48+ if ns == "0" {
49+ return .constant(false)
50+ }
51+ if ns == "1" {
52+ return .constant(true)
53+ }
48 if ns.trimmingCharacters(in: .whitespaces).reduce(true, {$0 && CharacterSet.letters.contains($1.unicodeScalars.first!)}) {
54 if ns.trimmingCharacters(in: .whitespaces).reduce(true, {$0 && CharacterSet.letters.contains($1.unicodeScalars.first!)}) {
49 vars.insert(String(s))
55 vars.insert(String(s))
50 return .variable(String(s))
56 return .variable(String(s))
@@ -59,6 +65,16 @@ struct Formula {
59 print("Internal error: weird variable name first charecter \(ns.first!)")
65 print("Internal error: weird variable name first charecter \(ns.first!)")
60 exit(1)
66 exit(1)
61 }
67 }
68+ switch ns.first! {
69+ case "0":
70+ ns = ns.dropFirst().trimmingCharacters(in: .whitespaces)
71+ return "0"
72+ case "1":
73+ ns = ns.dropFirst().trimmingCharacters(in: .whitespaces)
74+ return "1"
75+ default:
76+ break
77+ }
62 var i = ns.startIndex
78 var i = ns.startIndex
63 while i < ns.endIndex {
79 while i < ns.endIndex {
64 if !CharacterSet.letters.contains(ns[i].unicodeScalars.first!) {
80 if !CharacterSet.letters.contains(ns[i].unicodeScalars.first!) {
@@ -123,6 +139,7 @@ private func extractBrackets(from s: String) -> (wasInside: Substring, after: Su
123private typealias Variable = String
139private typealias Variable = String
124private indirect enum FormulaElement {
140private indirect enum FormulaElement {
125 case variable(Variable)
141 case variable(Variable)
142+ case constant(Bool)
126 case operation(FormulaElement, Operator, FormulaElement)
143 case operation(FormulaElement, Operator, FormulaElement)
127 func evaluate(with variables: [Variable: Bool]) -> Bool {
144 func evaluate(with variables: [Variable: Bool]) -> Bool {
128 switch self {
145 switch self {
@@ -135,6 +152,8 @@ private indirect enum FormulaElement {
135 }
152 }
136 case .operation(let l, let op, let r):
153 case .operation(let l, let op, let r):
137 return op.value(l.evaluate(with: variables), r.evaluate(with: variables))
154 return op.value(l.evaluate(with: variables), r.evaluate(with: variables))
155+ case .constant(let b):
156+ return b
138 }
157 }
139 }
158 }
140}
159}