diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2015-12-08 22:45:16 +0200 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2015-12-08 22:45:16 +0200 |
| commit | 34758ebd78d39075dc58f88d33a28c700612121c (patch) | |
| tree | 0e7edc6e525196bdc6e7dbb88fb0111c222b3a8b | |
| parent | 266012d2beb4bfb0e4b342fb697a2472cba7d25d (diff) | |
| download | lbasi-34758ebd78d39075dc58f88d33a28c700612121c.tar.gz lbasi-34758ebd78d39075dc58f88d33a28c700612121c.zip | |
handle multiple +/- operations at the beginning of expression
| -rw-r--r-- | calc1.pas | 26 | ||||
| -rw-r--r-- | tests/calc1.txt | 6 |
2 files changed, 27 insertions, 5 deletions
@@ -187,11 +187,27 @@ begin Op := Current; - if Op.TokenType = TT_Plus then - Eat(TT_Plus) - else if Op.TokenType = TT_Minus then - Eat(TT_Minus) - else if Op.TokenType = TT_Asterisk then + while (Op.TokenType = TT_Plus) or (Op.TokenType = TT_Minus) do + begin + Eat(Op.TokenType); + + Right := Current; + Eat(TT_Integer); + + if Op.TokenType = TT_Plus then + Result := TokenInteger(Left).Val + TokenInteger(Right).Val + else + Result := TokenInteger(Left).Val - TokenInteger(Right).Val; + + TokenInteger(Left).Val := Result; + + Op := Current; + + if Op.TokenType = TT_Eof then + Exit; + end; + + if Op.TokenType = TT_Asterisk then Eat(TT_Asterisk) else Eat(TT_Slash); diff --git a/tests/calc1.txt b/tests/calc1.txt index ad30e6e..127a498 100644 --- a/tests/calc1.txt +++ b/tests/calc1.txt @@ -31,3 +31,9 @@ Simple calculations 10/2 5 3*4 12 + +Multiple Runs Of Plus And Minus + [Template] The result of ${calculations} should be ${expected} + 1+1+1 3 + 1-1-1 -1 + 5+1-2 4 |
