aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-08-18 22:27:52 -0400
committerTucker Evans <tuckerevans24@gmail.com>2019-08-18 22:27:52 -0400
commitdd07055aca1c45d147d773350e0c21930822a74f (patch)
tree8d19ee459aabb4fb1a849049299fb06a106508ee
parentf9ff4c818cee8c8b83e9a3d5161d4de2b8443146 (diff)
Add for loop parsing
Also fixes tree printing
-rw-r--r--check/for.p7
-rw-r--r--pc.y28
-rw-r--r--tree.c19
3 files changed, 40 insertions, 14 deletions
diff --git a/check/for.p b/check/for.p
index 694f9a5..5391c8c 100644
--- a/check/for.p
+++ b/check/for.p
@@ -1,14 +1,9 @@
program main ( input, output );
- var a, b: integer;
- var x,y,z: real;
- var ai :array [1..10] of integer;
+ var a, b,c: integer;
begin
-{ TEST }
a := 1;
- x := 3.14;
- b := a + 35;
(* test *)
for c := 0 to 10 do begin
for a:= 10 downto 0 do b := a - c
diff --git a/pc.y b/pc.y
index 264218c..d679c18 100644
--- a/pc.y
+++ b/pc.y
@@ -84,6 +84,8 @@ extern scope *cur_scope;
%type <ival> type
%type <ival> standard_type
+%type <ival> TD
+
%%
program
@@ -255,8 +257,19 @@ statement
}
|FOR var ASSIGNOP expr TD expr DO statement
{
- /*TODO design tree structure for FOR loops*/
- $$ = NULL;
+ /*
+ FOR
+ / \
+ TD STATEMENT
+ / \
+ ASSIGNOP INUM
+ */
+ ptree *tmp;
+
+ tmp = mktree(ASSIGNOP, $2, $4);
+ tmp = mktree($5, tmp, $6); //$5 is TD
+
+ $$ = mktree(FOR, tmp, $8);
}
| expr
{
@@ -264,7 +277,16 @@ statement
}
;
-TD: TO | DT;
+TD
+ :TO
+ {
+ $$ = TO;
+ }
+ |DT
+ {
+ $$ = DT;
+ }
+;
var
:ID
diff --git a/tree.c b/tree.c
index 1874712..fd40781 100644
--- a/tree.c
+++ b/tree.c
@@ -164,15 +164,24 @@ int spaces;
case IF:
fprintf(stderr, "[IF]");
break;
+ case THEN:
+ fprintf(stderr, "[THEN]");
+ break;
case WHILE:
fprintf(stderr, "[WHILE]");
break;
- case THEN:
- fprintf(stderr, "[THEN]");
+ case FOR:
+ fprintf(stderr, "[FOR]");
+ break;
+ case TO:
+ fprintf(stderr, "[TO]");
+ break;
+ case DT:
+ fprintf(stderr, "[DOWN-TO]");
break;
- default:
- fprintf(stderr, "\t%d", t->type);
- yyerror("Error in tree_print");
+ default:
+ fprintf(stderr, "\t%d", t->type);
+ yyerror("Error in tree_print");
}
fprintf(stderr," %d\n", t->ret_type);
aux_tree_print(t->l, spaces + 2);