@@ -45,6 +45,12 @@ struct Formula {
45 return startingLeft
45 return startingLeft
47 var ns = s.trimmingCharacters(in: .whitespaces)
47 var ns = s.trimmingCharacters(in: .whitespaces)
49+ return .constant(false)
52+ return .constant(true)
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!)")
70+ ns = ns.dropFirst().trimmingCharacters(in: .whitespaces)
73+ ns = ns.dropFirst().trimmingCharacters(in: .whitespaces)
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)
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 {
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):