From dd07055aca1c45d147d773350e0c21930822a74f Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sun, 18 Aug 2019 22:27:52 -0400 Subject: Add for loop parsing Also fixes tree printing --- check/for.p | 7 +------ pc.y | 28 +++++++++++++++++++++++++--- tree.c | 19 ++++++++++++++----- 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 type %type standard_type +%type 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); -- cgit v1.1