summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2015-12-10 19:44:17 +0200
committerOskari Timperi <oskari.timperi@iki.fi>2015-12-10 19:44:17 +0200
commitc62e2d897f2e400d9608ff9abb285ceab4bdfc48 (patch)
tree5ab4d4d2450d3d3a7ca9916a29c73df8f48930ca
parenta6285451de83de2aee777322f62fafeccea283da (diff)
downloadlbasi-c62e2d897f2e400d9608ff9abb285ceab4bdfc48.tar.gz
lbasi-c62e2d897f2e400d9608ff9abb285ceab4bdfc48.zip
add TInterpreter.Term()
-rw-r--r--calc1.pas35
1 files 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);
@@ -209,9 +210,31 @@ 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;