summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--calc1.pas33
1 files changed, 30 insertions, 3 deletions
diff --git a/calc1.pas b/calc1.pas
index a9b373b..fd522a9 100644
--- a/calc1.pas
+++ b/calc1.pas
@@ -6,7 +6,8 @@ uses
typinfo, sysutils, character;
type
- TokenType = (TT_Integer, TT_Plus, TT_Minus, TT_Asterisk, TT_Slash, TT_Eof);
+ TokenType = (TT_Integer, TT_Plus, TT_Minus, TT_Asterisk, TT_Slash, TT_Eof,
+ TT_LParen, TT_RParen);
Token = class
TokenType: TokenType;
@@ -171,6 +172,16 @@ begin
Result := Token.Create(TT_Slash);
Inc(CurPos);
end
+ else if CurChar = '(' then
+ begin
+ Result := Token.Create(TT_LParen);
+ Inc(CurPos);
+ end
+ else if CurChar = ')' then
+ begin
+ Result := Token.Create(TT_RParen);
+ Inc(CurPos);
+ end
else
Error;
end;
@@ -202,14 +213,30 @@ var
T: Token;
begin
T := CurrentToken;
- Eat(TT_Integer);
- Result := TokenInteger(T).Val;
+
+ if T.TokenType = TT_Integer then
+ begin
+ Eat(TT_Integer);
+ Result := TokenInteger(T).Val;
+ end
+ else if T.TokenType = TT_LParen then
+ begin
+ Eat(TT_LParen);
+ Result := Expr;
+ Eat(TT_RParen);
+ end
+ else
+ Error;
end;
function TInterpreter.Expr: Integer;
var
Tok: Token;
begin
+ // expr : term ((PLUS|MINUS) term)*
+ // term : factor ((MUL|DIV) factor)*
+ // factor : INTEGER | LPAREN expr RPAREN
+
Result := Term;
while CurrentToken.TokenType in [TT_Plus, TT_Minus] do