From 60507214c098beaf3ae01a202e564bfe2f7c8364 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 27 Jul 2019 21:08:22 -0400 Subject: Add basic scoping --- main.c | 5 +++++ makefile | 17 +++++++++++------ pc.l | 8 +++++--- pc.y | 3 ++- scope.c | 35 +++++++++++++++++++---------------- scope.h | 8 ++++---- 6 files changed, 46 insertions(+), 30 deletions(-) diff --git a/main.c b/main.c index fbd96d0..dba3b7f 100644 --- a/main.c +++ b/main.c @@ -10,6 +10,8 @@ extern char *yytext; extern int line_num; +scope *cur_scope; + char* pretty_type(t) int t; { @@ -136,6 +138,9 @@ char* msg; int main() { + cur_scope = mkscope(); + assert(cur_scope); + yyparse(); return 0; diff --git a/makefile b/makefile index 8dd5899..c429f3c 100644 --- a/makefile +++ b/makefile @@ -3,19 +3,22 @@ FLAGS = -g YACC = yacc LEX = lex -mypc: y.tab.o lex.yy.o tree.o scope.o node.o pc.o - $(CC) $(FLAGS) -o mypc main.o tree.o scope.o node.o y.tab.o lex.yy.o -lfl -ly +mypc: y.tab.o lex.yy.o tree.o scope.o node.o pc.o sem_check.o + $(CC) $(FLAGS) -o mypc main.o tree.o scope.o sem_check.o node.o y.tab.o lex.yy.o -lfl -ly -pc.o: main.c pc.h +pc.o: main.c headers $(CC) $(FLAGS) -c main.c -tree.o: tree.c tree.h +tree.o: tree.c headers $(CC) $(FLAGS) -c tree.c -scope.o: scope.c scope.h +sem_check.o: sem_check.c headers + $(CC) $(FLAGS) -c sem_check.c + +scope.o: scope.c headers $(CC) $(FLAGS) -c scope.c -node.o: node.c node.h +node.o: node.c headers $(CC) $(FLAGS) -c node.c y.tab.o: y.tab.c @@ -30,5 +33,7 @@ y.tab.c: pc.y lex.yy.c: pc.l $(LEX) -l pc.l +headers: pc.h tree.h sem_check.h y.tab.h scope.h node.h y.tab.c + clean: rm -f mypc *.o y.tab.* lex.yy.* diff --git a/pc.l b/pc.l index 1a686d3..657f080 100644 --- a/pc.l +++ b/pc.l @@ -1,9 +1,11 @@ %{ - +#include "node.h" +#include "scope.h" #include "y.tab.h" #include "pc.h" int line_num=1; +extern scope *cur_scope; %} @@ -39,13 +41,13 @@ id [A-Za-z][A-Za-z0-9_]* "procedure" { debug_print(PROC, NULL); - /*s = push_scope(s);*/ + push_scope(&cur_scope); return PROC; } "function" { debug_print(FUNC, NULL); - /*s = push_scope(s);*/ + push_scope(&cur_scope); return FUNC; } diff --git a/pc.y b/pc.y index e8f2baa..e7cb06e 100644 --- a/pc.y +++ b/pc.y @@ -7,7 +7,7 @@ #include "tree.h" #include "y.tab.h" #include "pc.h" -/*#include "sem_check.h"*/ +#include "sem_check.h" /* TODO: @@ -15,6 +15,7 @@ TODO: */ extern int yylex(); +extern scope *cur_scope; %} diff --git a/scope.c b/scope.c index d09f1b4..379be97 100644 --- a/scope.c +++ b/scope.c @@ -56,26 +56,29 @@ char* s; return h % HASH_SIZE; } -scope* push_scope(root) -scope *root; +void push_scope(root) +scope **root; { - scope *p = mkscope(); - p->next = root; - return p; + scope *tmp = mkscope(); + + assert(tmp); + + tmp->prev = *root; + *root = tmp; } -scope* pop_scope(root) -scope *root; +void pop_scope(root) +scope **root; { - scope *p; + scope *tmp; - if (!root) - return NULL; + if (!*root) + return; - p = root->next; + tmp = *root; + *root = (*root)->prev; - free_scope(root); - return p; + free_scope(tmp); } node* scope_insert(root, name) @@ -105,7 +108,7 @@ char *name; scope *p; node *tmp; - for (p = root; p; p = p->next) + for (p = root; p; p = p->prev) if (tmp = scope_search(p, name)) return tmp; @@ -119,10 +122,10 @@ char *name; scope *p; node *tmp; - for (p = root; p; p = p->next) { + for (p = root; p; p = p->prev) { if (tmp = scope_search(p, name)) return tmp; - if (p->function_boundry) + if (p->ret_var) return NULL; } diff --git a/scope.h b/scope.h index 7160b5e..0a07a26 100644 --- a/scope.h +++ b/scope.h @@ -5,16 +5,16 @@ typedef struct hash { node* table[HASH_SIZE]; - struct hash *prev, *next; - char function_boundry; + struct hash *prev; + node* ret_var; } scope; scope* mkscope(); void free_scope(scope*); /*stack routines*/ -scope* pop_scope(scope*); -scope* push_scope(scope*); +void pop_scope(scope**); +void push_scope(scope**); /*helpers*/ node* scope_insert(scope*, char*); -- cgit v1.1