on master help sign in
Truther/Operator.swift+1−1
since splitsnapshots
unifiedsplit
@@ -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}