From f4d98d609e09ae172dbcccff742cc654d8b51a52 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 27 Jul 2019 21:09:10 -0400 Subject: Add initial scope checking --- pc.y | 56 +++++++++++++++++++++++++++++++++++++++++++++++--------- scope.c | 4 ++-- tree.c | 8 ++++---- tree.h | 3 +-- 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/pc.y b/pc.y index e7cb06e..dc6a05b 100644 --- a/pc.y +++ b/pc.y @@ -73,6 +73,8 @@ extern scope *cur_scope; %type simple_expr %type id_list +%type param_list +%type arguments %type expr_list %type 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 { diff --git a/scope.c b/scope.c index 379be97..11a9c5b 100644 --- a/scope.c +++ b/scope.c @@ -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; } diff --git a/tree.c b/tree.c index 0301ec3..a1518f0 100644 --- a/tree.c +++ b/tree.c @@ -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); diff --git a/tree.h b/tree.h index b17c276..fffca94 100644 --- a/tree.h +++ b/tree.h @@ -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*); -- cgit v1.1