Skip to content

Commit 0c0a1f1

Browse files
committed
🐛 fixing handling of same priority operations in parser
1 parent 185885e commit 0c0a1f1

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

SwiftPascalInterpreter/SwiftPascalInterpreter/Parser.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class Parser {
4646

4747
/**
4848
Factor for the grammar described in the `expr` method
49-
49+
5050
Returns: Int value
5151
*/
5252
private func factor() -> AST {
@@ -67,20 +67,20 @@ public class Parser {
6767

6868
/**
6969
Term for the grammar described in the `expr` method
70-
70+
7171
Returns: Int value
7272
*/
7373
private func term() -> AST {
74-
let node = factor()
74+
var node = factor()
7575

7676
while [.operation(.mult), .operation(.div)].contains(currentToken) {
7777
let token = currentToken
7878
if token == .operation(.mult) {
7979
eatOperation(.mult)
80-
return .binaryOperation(left: node, operation: .mult, right: factor())
80+
node = .binaryOperation(left: node, operation: .mult, right: factor())
8181
} else if token == .operation(.div) {
8282
eatOperation(.div)
83-
return .binaryOperation(left: node, operation: .div, right: factor())
83+
node = .binaryOperation(left: node, operation: .div, right: factor())
8484
}
8585
}
8686

@@ -89,25 +89,25 @@ public class Parser {
8989

9090
/**
9191
Arithmetic expression parser
92-
92+
9393
expr : term (PLUS | MINUS) term)*
9494
term : factor ((MUL | DIV) factor)*
9595
factor : INTEGER | LPAREN factor RPAREN
96-
96+
9797
Returns: Int value
9898
*/
9999
public func expr() -> AST {
100100

101-
let node = term()
101+
var node = term()
102102

103103
while [.operation(.plus), .operation(.minus)].contains(currentToken) {
104104
let token = currentToken
105105
if token == .operation(.plus) {
106106
eatOperation(.plus)
107-
return .binaryOperation(left: node, operation: .plus, right: term())
107+
node = .binaryOperation(left: node, operation: .plus, right: term())
108108
} else if token == .operation(.minus) {
109109
eatOperation(.minus)
110-
return .binaryOperation(left: node, operation: .minus, right: term())
110+
node = .binaryOperation(left: node, operation: .minus, right: term())
111111
}
112112
}
113113

0 commit comments

Comments
 (0)