22public final class Expression {
33 // Implemented as a linked list of ExpressionNodes. This allows us to indicate operators,
44 // and iteratively solve by reducing the list according to the order of operations.
5-
5+
66 var first : ExpressionNode
77 var last : ExpressionNode
88 var count : Int
9-
9+
1010 init ( node: ExpressionNode ) {
11- self . first = node
12- self . last = node
11+ first = node
12+ last = node
1313 count = 1
1414 }
15-
15+
1616 /// Initializes an expression from a string.
1717 ///
1818 /// Parsing rules:
@@ -29,25 +29,25 @@ public final class Expression {
2929 /// - Parameter expr: The string expression to parse.
3030 public init ( _ expr: String ) throws {
3131 let parsed = try Parser ( expr) . parseExpression ( )
32- self . first = parsed. first
33- self . last = parsed. last
34- self . count = parsed. count
32+ first = parsed. first
33+ last = parsed. last
34+ count = parsed. count
3535 }
36-
36+
3737 /// Reduces the expression to a single measurement, respecting the [order of operations](https://en.wikipedia.org/wiki/Order_of_operations)
3838 public func solve( ) throws -> Measurement {
3939 let copy = self . copy ( )
4040 return try copy. computeAndDestroy ( )
4141 }
42-
42+
4343 @discardableResult
4444 func append( op: Operator , node: ExpressionNode ) -> Self {
4545 last. next = . init( op: op, node: node)
4646 last = node
4747 count = count + 1
4848 return self
4949 }
50-
50+
5151 func copy( ) -> Expression {
5252 // Copy the expression list so the original is not destroyed
5353 let copy = Expression ( node: first. copy ( ) )
@@ -58,12 +58,11 @@ public final class Expression {
5858 }
5959 return copy
6060 }
61-
61+
6262 /// Reduces the expression to a single measurement, respecting the [order of operations](https://en.wikipedia.org/wiki/Order_of_operations)
6363 ///
6464 /// NOTE: This flattens the list, destroying it. Use `solve` for non-destructive behavior.
6565 private func computeAndDestroy( ) throws -> Measurement {
66-
6766 // SubExpressions
6867 func computeSubExpression( node: ExpressionNode ) throws {
6968 switch node. value {
@@ -81,7 +80,7 @@ public final class Expression {
8180 }
8281 try computeSubExpression ( node: left)
8382 // At this point, there should be no more sub expressions
84-
83+
8584 // Exponentals
8685 func exponentiate( node: ExpressionNode ) throws {
8786 guard let exponent = node. exponent else {
@@ -102,7 +101,7 @@ public final class Expression {
102101 left = next. node
103102 }
104103 try exponentiate ( node: left)
105-
104+
106105 // Multiplication
107106 left = first
108107 while let next = left. next {
@@ -123,15 +122,15 @@ public final class Expression {
123122 fatalError ( " Parentheses still present during multiplication phase " )
124123 }
125124 }
126-
125+
127126 // Addition
128127 left = first
129128 while let next = left. next {
130129 let right = next. node
131130 switch ( left. value, right. value) {
132131 case let ( . measurement( leftMeasurement) , . measurement( rightMeasurement) ) :
133132 switch next. op {
134- case . add: // Compute and absorb right node into left
133+ case . add: // Compute and absorb right node into left
135134 left. value = try . measurement( leftMeasurement + rightMeasurement)
136135 left. next = right. next
137136 case . subtract: // Compute and absorb right node into left
@@ -144,7 +143,7 @@ public final class Expression {
144143 fatalError ( " Parentheses still present during addition phase " )
145144 }
146145 }
147-
146+
148147 if first. next != nil {
149148 fatalError ( " Expression list reduction not complete " )
150149 }
@@ -194,15 +193,15 @@ class ExpressionNode {
194193 var value : ExpressionNodeValue
195194 var exponent : Int ?
196195 var next : ExpressionLink ?
197-
196+
198197 init ( _ value: ExpressionNodeValue , exponent: Int ? = nil , next: ExpressionLink ? = nil ) {
199198 self . value = value
200199 self . exponent = exponent
201200 self . next = next
202201 }
203-
202+
204203 func copy( ) -> ExpressionNode {
205- return . init( value. copy ( ) , exponent: self . exponent)
204+ return . init( value. copy ( ) , exponent: exponent)
206205 }
207206}
208207
@@ -216,7 +215,7 @@ extension ExpressionNode: Equatable {
216215enum ExpressionNodeValue {
217216 case measurement( Measurement )
218217 case subExpression( Expression )
219-
218+
220219 func copy( ) -> ExpressionNodeValue {
221220 switch self {
222221 case let . measurement( measurement) :
@@ -254,7 +253,7 @@ extension ExpressionNodeValue: Equatable {
254253class ExpressionLink {
255254 let op : Operator
256255 let node : ExpressionNode
257-
256+
258257 init ( op: Operator , node: ExpressionNode ) {
259258 self . op = op
260259 self . node = node
0 commit comments