summaryrefslogtreecommitdiff
path: root/crenshaw/main.pas
diff options
context:
space:
mode:
Diffstat (limited to 'crenshaw/main.pas')
-rw-r--r--crenshaw/main.pas21
1 files changed, 11 insertions, 10 deletions
diff --git a/crenshaw/main.pas b/crenshaw/main.pas
index 12fec6e..04a3624 100644
--- a/crenshaw/main.pas
+++ b/crenshaw/main.pas
@@ -72,10 +72,17 @@ begin;
WriteLn(s+':');
end;
+procedure Expression; Forward;
+
procedure Factor;
begin
- EmitLn('mov eax, ' + GetNum);
- EmitLn('push eax');
+ if Look = '(' then begin
+ Match('(');
+ Expression;
+ Match(')');
+ end
+ else
+ EmitLn('mov eax, ' + GetNum);
end;
procedure Multiply;
@@ -83,9 +90,7 @@ begin
Match('*');
Factor;
EmitLn('pop ebx');
- EmitLn('pop eax');
EmitLn('mul ebx');
- EmitLn('push eax');
end;
procedure Divide;
@@ -93,9 +98,7 @@ begin
Match('/');
Factor;
EmitLn('pop ebx');
- EmitLn('pop eax');
EmitLn('div ebx');
- EmitLn('push eax');
end;
procedure Term;
@@ -103,6 +106,7 @@ begin
Factor;
while Look in ['*', '/'] do
begin
+ EmitLn('push eax');
case Look of
'*': Multiply;
'/': Divide;
@@ -116,9 +120,7 @@ begin;
Match('+');
Term;
Emitln('pop ebx');
- EmitLn('pop eax');
EmitLn('add eax, ebx');
- EmitLn('push eax');
end;
procedure Subtract;
@@ -126,9 +128,7 @@ begin;
Match('-');
Term;
EmitLn('pop ebx');
- EmitLn('pop eax');
EmitLn('sub eax, ebx');
- EmitLn('push eax');
end;
procedure Expression;
@@ -136,6 +136,7 @@ begin
Term;
while Look in ['+', '-'] do
begin
+ EmitLn('push eax');
case Look of
'+': Add;
'-': Subtract;