From 34758ebd78d39075dc58f88d33a28c700612121c Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Tue, 8 Dec 2015 22:45:16 +0200 Subject: handle multiple +/- operations at the beginning of expression --- calc1.pas | 26 +++++++++++++++++++++----- tests/calc1.txt | 6 ++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/calc1.pas b/calc1.pas index c77b427..541ae15 100644 --- a/calc1.pas +++ b/calc1.pas @@ -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 -- cgit v1.2.3