Skip to content

Commit 44ca20c

Browse files
committed
Updated parser so that you can multiply with parenthesis
1 parent 800a611 commit 44ca20c

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

Example/Tests/Tests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,11 @@ class Tests: XCTestCase {
5656
XCTAssertNil(Parser.parse(string: "2 + ((((((((4 * 3) - 4) + 2)"))
5757
XCTAssertEqual(Parser.parse(string: "(2 + (3 + (4 * (5)) + 2))")?.evaluate(), 27)
5858
}
59+
60+
func testParenthesisMult() {
61+
XCTAssertNotNil(Parser.parse(string: "2 + (3 + 4)(5 + 3)"))
62+
XCTAssertEqual(58, Parser.parse(string: "2 + (3 + 4)(5 + 3)")?.evaluate())
63+
XCTAssertEqual(Parser.parse(string: "2 * (2 + 4)(1 * 10) - 4")?.evaluate(), 116)
64+
}
5965

6066
}

MathParser.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'MathParser'
11-
s.version = '1.3'
11+
s.version = '1.4'
1212
s.summary = 'A simple parser for Mathematical Expressions.'
1313

1414
# This description is used to generate tags and improve search results.

MathParser/Classes/Source/Parser.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)