@@ -15,7 +15,7 @@ public class Parser {
1515 /// - returns: An expression that will result in the value
1616 public static func parse( string: String ) -> Expression ? {
1717 var updatedString = Parser . changeMinusIntoOther ( change: Operators . subOp, into: string)
18- guard updatedString. contains ( Operators . divOp) || updatedString. contains ( Operators . multOp) || updatedString. contains ( Operators . addOp) || updatedString. contains ( Operators . subOp) || updatedString. contains ( Operators . powOp) || Decimal ( string) != nil else {
18+ guard updatedString. contains ( Operators . divOp) || updatedString. contains ( Operators . multOp) || updatedString. contains ( Operators . addOp) || updatedString. contains ( Operators . subOp) || updatedString. contains ( Operators . powOp) || updatedString . contains ( " ( " ) || Decimal ( string) != nil else {
1919 return nil
2020 }
2121 if updatedString. contains ( " ( " ) {
@@ -29,16 +29,20 @@ public class Parser {
2929 parenCounts += 1
3030 }
3131 if parenCounts == - 1 {
32+ // Allows multiplication without the * sign
33+ var myIdx = updatedString. index ( after: nextIdx)
34+ while ( myIdx != updatedString. endIndex && updatedString. index ( after: myIdx) != updatedString. endIndex && updatedString [ myIdx] == " " ) {
35+ myIdx = updatedString. index ( after: myIdx)
36+ }
37+ if myIdx != updatedString. endIndex && updatedString [ myIdx] == " ( " {
38+ updatedString. insert ( " * " , at: myIdx)
39+ }
3240 updatedString. replaceSubrange ( idx..< nextIdx, with: " \( decimal: Parser . parse ( string: String ( updatedString [ updatedString. index ( after: idx) ..< nextIdx] ) ) ? . evaluate ( ) ) " )
33- break
41+ return Parser . parse ( string : updatedString )
3442 }
3543 nextIdx = updatedString. index ( after: nextIdx)
3644 }
37- if updatedString. contains ( " ( " ) {
38- return nil
39- } else {
40- return Parser . parse ( string: updatedString)
41- }
45+ return nil
4246 } else if updatedString. contains ( Operators . addOp) || updatedString. contains ( Operators . subOp) {
4347 let addAll = updatedString. split ( separator: Operators . addOp [ Operators . addOp. startIndex] ) . map ( String . init)
4448 let subMids = addAll. map ( { string in string. split ( separator: Operators . subOp. first!) . map ( String . init) } )
@@ -88,7 +92,7 @@ public class Parser {
8892 } else if updatedString. contains ( Operators . powOp) {
8993 let split = updatedString. split ( separator: Operators . powOp. first!) . lazy. map ( String . init) . map ( { $0. trimmingCharacters ( in: . whitespaces) } )
9094 if let first = split. last, let firstExpr = Parser . parse ( string: first) {
91- return split. dropLast ( ) . reversed ( ) . reduce ( firstExpr as? Expression , { ( result, next) in
95+ return split. dropLast ( ) . reversed ( ) . reduce ( firstExpr, { ( result: Expression ? , next: String ) in
9296 if let val = result, let nextVal = Parser . parse ( string: next) {
9397 return PowExpression ( leftExpression: nextVal, rightExpression: val)
9498 } else {
@@ -122,7 +126,7 @@ extension String {
122126 func endsWithNum( ) -> Bool {
123127 let val = self . trimmingCharacters ( in: . whitespaces)
124128 if let lastVal = val. last {
125- return Float ( String ( lastVal) ) != nil
129+ return Float ( String ( lastVal) ) != nil || String ( lastVal ) == " ) "
126130 } else {
127131 return false
128132 }
0 commit comments