diff options
author | Tucker Evans <tuckerevans24@gmail.com> | 2019-07-27 21:09:10 -0400 |
---|---|---|
committer | Tucker Evans <tuckerevans24@gmail.com> | 2019-07-27 21:09:10 -0400 |
commit | f4d98d609e09ae172dbcccff742cc654d8b51a52 (patch) | |
tree | 739026dd7a36015b6f23f2670781c247f92171e2 | |
parent | 60507214c098beaf3ae01a202e564bfe2f7c8364 (diff) |
Add initial scope checking
-rw-r--r-- | pc.y | 56 | ||||
-rw-r--r-- | scope.c | 4 | ||||
-rw-r--r-- | tree.c | 8 | ||||
-rw-r--r-- | tree.h | 3 |
4 files changed, 54 insertions, 17 deletions
@@ -73,6 +73,8 @@ extern scope *cur_scope; %type <tval> simple_expr %type <tval> id_list +%type <tval> param_list +%type <tval> arguments %type <tval> expr_list %type <tval> statement @@ -100,11 +102,15 @@ program id_list :ID { - $$ = mkid($1); + check_id(cur_scope, $1); + + $$ = scope_insert(cur_scope, $1); } |id_list ',' ID { - $$ = mktree(LIST, $1, mkid($3)); + check_id(cur_scope, $3); + + $$ = mktree(LIST, $1, scope_insert(cur_scope, $3)); } ; @@ -156,18 +162,28 @@ sub_prog_declaration } ; +/*push_scope called in pc.l*/ sub_prog_head :FUNC ID arguments ':' standard_type ';' { + check_id(cur_scope->prev, $2); + scope_insert(cur_scope->prev, $2); + + cur_scope->ret_var = scope_insert(cur_scope, $2); + cur_scope->ret_var->var_type = $5; + } |PROC ID arguments ';' { + check_id(cur_scope->prev, $2); + scope_insert(cur_scope->prev, $2); } ; arguments :'(' param_list ')' { + $$ = $2; } |/*empty*/ ; @@ -175,9 +191,11 @@ arguments param_list :id_list ':' type { + $$ = $1; } |param_list ';' id_list ':' type { + $$ = mktree(LIST, $1, $3); } ; @@ -243,22 +261,33 @@ TD: TO | DT; var :ID { - $$ = mkid($1); + check_id(cur_scope, $1); + $$ = mkid(scope_insert(cur_scope,$1)); } |ID '[' expr ']' { - $$ = mktree(ARRAY_ACCESS, mkid($1), $3); + node* tmp; + check_id(cur_scope, $1); + tmp = scope_insert(cur_scope, $1); + + $$ = mktree(ARRAY_ACCESS, mkid(tmp), $3); } ; proc_statement :ID { - $$ = mkid($1); + node *tmp; + + tmp = check_exists(cur_scope, $1); + $$ = mktree(PCALL, mkid(tmp), NULL); } |ID '(' expr_list ')' { - $$ = mktree(PCALL, mkid($1), $3); + node *tmp; + + tmp = check_exists(cur_scope, $1); + $$ = mktree(PCALL, mkid(tmp), $3); } ; @@ -309,15 +338,24 @@ term factor :ID { - $$ = mkid($1); + node *tmp; + + tmp = check_exists(cur_scope, $1); + $$ = mkid(tmp); } |ID '[' expr ']' { - $$ = mktree(ARRAY_ACCESS, mkid($1), $3); + node *tmp; + + tmp = check_exists(cur_scope, $1); + $$ = mktree(ARRAY_ACCESS, mkid(tmp), $3); } |ID '(' expr_list ')' { - $$ = mktree(FCALL, mkid($1), $3); + node *tmp; + + tmp = check_exists(cur_scope, $1); + $$ = mktree(FCALL, mkid(tmp), $3); } |INUM { @@ -16,8 +16,8 @@ scope* mkscope() for (i = 0; i < HASH_SIZE; i++) p->table[i] = NULL; - p->next = NULL; - p->function_boundry = 0; + p->prev = NULL; + p->ret_var= NULL; return p; } @@ -23,11 +23,11 @@ ptree *l, *r; return t; } -ptree* mkid(str) -char *str; +ptree* mkid(n) +node *n; { ptree *p = mktree(ID, NULL, NULL); - p->attr.sval = strdup(str); /* memory leak? double strdup*/ + p->attr.nval = n; /* memory leak? double strdup*/ return p; } @@ -94,7 +94,7 @@ int spaces; fprintf(stderr, "[LIST]"); break; case ID: - fprintf(stderr, "[ID: %s]", t->attr.sval); + fprintf(stderr, "[ID: %s]", t->attr.nval->name); break; case INUM: fprintf(stderr, "[INUM: %d]", t->attr.ival); @@ -6,7 +6,6 @@ typedef struct parse_tree { union { int ival; /* NUM */ float rval; /* RNUM */ - char *sval; /* ID */ node *nval; int opval; /* RELOP: LT LE GT GE EQ NE ADDOP: PLUS MINUS OR @@ -20,7 +19,7 @@ void aux_tree_print(ptree*, int); void print_tree(ptree*); ptree* mktree(int, ptree*, ptree*); -ptree* mkid(char*); +ptree* mkid(node*); ptree* mkinum(int); ptree* mkrnum(float); ptree* mkop(int, int, ptree*, ptree*); |