diff options
author | Tucker Evans <tuckerevans24@gmail.com> | 2019-07-27 21:08:22 -0400 |
---|---|---|
committer | Tucker Evans <tuckerevans24@gmail.com> | 2019-07-27 21:08:22 -0400 |
commit | 60507214c098beaf3ae01a202e564bfe2f7c8364 (patch) | |
tree | f189004a59cf1a19a743469a92f996f7615cdc02 | |
parent | d9ba5ec2f86dd69eae4edac586a889fccd8d31b1 (diff) |
Add basic scoping
-rw-r--r-- | main.c | 5 | ||||
-rw-r--r-- | makefile | 17 | ||||
-rw-r--r-- | pc.l | 8 | ||||
-rw-r--r-- | pc.y | 3 | ||||
-rw-r--r-- | scope.c | 35 | ||||
-rw-r--r-- | scope.h | 8 |
6 files changed, 46 insertions, 30 deletions
@@ -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; @@ -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.* @@ -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; } @@ -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; %} @@ -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; } @@ -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*); |