From c62e2d897f2e400d9608ff9abb285ceab4bdfc48 Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Thu, 10 Dec 2015 19:44:17 +0200 Subject: add TInterpreter.Term() --- calc1.pas | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/calc1.pas b/calc1.pas index 9819c54..a9b373b 100644 --- a/calc1.pas +++ b/calc1.pas @@ -56,6 +56,7 @@ type procedure Eat(T: TokenType); function Factor: Integer; function Expr: Integer; + function Term: Integer; end; constructor Token.Create(Type_: TokenType); @@ -206,12 +207,34 @@ begin end; function TInterpreter.Expr: Integer; +var + Tok: Token; +begin + Result := Term; + + while CurrentToken.TokenType in [TT_Plus, TT_Minus] do + begin + Tok := CurrentToken; + if Tok.TokenType = TT_Plus then + begin + Eat(TT_Plus); + Result := Round(Result + Term); + end + else if Tok.TokenType = TT_Minus then + begin + Eat(TT_Minus); + Result := Round(Result - Term); + end; + end; +end; + +function TInterpreter.Term: Integer; var Tok: Token; begin Result := Factor; - while CurrentToken.TokenType in [TT_Asterisk, TT_Slash, TT_Plus, TT_Minus] do + while CurrentToken.TokenType in [TT_Asterisk, TT_Slash] do begin Tok := CurrentToken; if Tok.TokenType = TT_Asterisk then @@ -223,16 +246,6 @@ begin begin Eat(TT_Slash); Result := Round(Result / Factor); - end - else if Tok.TokenType = TT_Plus then - begin - Eat(TT_Plus); - Result := Round(Result + Factor); - end - else if Tok.TokenType = TT_Minus then - begin - Eat(TT_Minus); - Result := Round(Result - Factor); end; end; end; -- cgit v1.2.3