From e0e828e2c2f2032d1c9616950d0208f2aab6fcb8 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 27 Jul 2019 12:41:50 -0400 Subject: Add basic scopes --- hash.c | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hash.h | 27 +++++++++++++ node.c | 10 +++++ node.h | 6 ++- 4 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 hash.c create mode 100644 hash.h diff --git a/hash.c b/hash.c new file mode 100644 index 0000000..21ddb1a --- /dev/null +++ b/hash.c @@ -0,0 +1,143 @@ +#include +#include +#include +#include + +#include "node.h" +#include "hash.h" + +scope* mkscope(prev) +scope* prev; +{ + int i; + + scope *p = malloc(sizeof(scope)); + assert(p); + + for (i = 0; i < HASH_SIZE; i++) + p->table[i] = NULL; + + p->next = next; + p->function_boundry = 0; + + return p; +} + +void free_scope(s) +scope *s; +{ + int i; + + if (!s) + return; + + for (i = 0; i < HASH_SIZE; i++) { + free_nodes(s->table[i]); + } + + free(s); +} + +/*Copied from Compilers, Aho*/ +#define EOS '\0' +int hashpjw(s) +char* s; +{ + char *p; + unsigned h = 0, g; + + for (p = s; *p != EOS; p++) { + h = (h<<4) + *p; + if (g = h & 0xf0000000) { + h^= g>>24; + h^= g; + } + } + + return h % HASH_SIZE; +} + +scope* push_scope(root); +scope* root; +{ + scope *p = mkscope(); + p->next = root; + return p; +} + +scope* pop_scope(root); +scope *root; +{ + scope *p; + + if (!root) + return NULL; + + p = root->next; + + free_scope(root); + return p; +} + +node* scope_insert(s, name) +scope *s; +char *name; +{ + int hash = hashpwj(name); + + node *tmp = root->table[hash]; + return root->table[hash] = list_insert(tmp, name); +} + +node* scope_search(root, name) +scope *root; +char *name; +{ + int hash = hashpwj(name); + + node *tmp = root->table[hash]; + return list_search(tmp, name); +} + +node* scope_search_all(root, name) +scope *root; +char *name; +{ + scope *p; + node *tmp; + + for (p = root; p; p = p->next) + if (tmp = scope_search(p, name)) + return tmp; + + return NULL; +} + +node* scope_safe_search(root, name) +scope *root; +char *name; +{ + scope *p; + node *tmp; + + for (p = root; p; p = p->next) { + if (tmp = scope_search(p, name)) + return tmp; + if (p->f) + return NULL + } + + return NULL; +} + +void print_scope(s) +scope *s; +{ + int i; + node_t * tmp; + for (i = 0; i < HASH_SIZE; i++) { + for( tmp=s->table[i]; tmp; tmp = tmp->next) { + fprintf(stderr, "\t%s\n", tmp->name); + } + } +} diff --git a/hash.h b/hash.h new file mode 100644 index 0000000..8a8cdb1 --- /dev/null +++ b/hash.h @@ -0,0 +1,27 @@ +#ifndef SCOPE_H +#define SCOPE_H + +#define HASH_SIZE 211 + +typedef struct hash { + node* table[HASH_SIZE]; + struct hash *prev, *next; + char function_boundry; +} scope; + +scope* mkscope(scope*); +void free_scope(scope*); + +/*stack routines*/ +scope* pop_scope(scope*); +scope* push_scope(scope*); + +/*helpers*/ +node* scope_insert(scope*, char*); +node* scope_search_all(scope*, char*); +node* scope_search(scope*, char*); +node* scope_safe_search_all(scope*, char*); + +/*hash function*/ +int hash_pwj(char*); +#endif diff --git a/node.c b/node.c index e1b7920..ca6548b 100644 --- a/node.c +++ b/node.c @@ -41,3 +41,13 @@ char * str; p->next = root; return p; } + +void free_list(n) +node *n; +{ + node *tmp; + + for(tmp = n; tmp; tmp = n = n->next) { + free(tmp); + } +} diff --git a/node.h b/node.h index 5206db4..90e3936 100644 --- a/node.h +++ b/node.h @@ -13,7 +13,9 @@ typedef struct node_s { node* mknode(char *); /* helpers */ -node* search(node*, char *); -node* insert(node*, char*); +node* list_search(node*, char *); +node* list_insert(node*, char*); + +void free_list(node*); #endif -- cgit v1.1 From 0e835d9b8f97087810dddb9743aa84b6431ff3a7 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 27 Jul 2019 12:55:31 -0400 Subject: Rename hash.* -> scope.* --- hash.c | 143 --------------------------------------------------------------- hash.h | 27 ------------ makefile | 8 ++-- pc.y | 2 + scope.c | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ scope.h | 27 ++++++++++++ 6 files changed, 176 insertions(+), 174 deletions(-) delete mode 100644 hash.c delete mode 100644 hash.h create mode 100644 scope.c create mode 100644 scope.h diff --git a/hash.c b/hash.c deleted file mode 100644 index 21ddb1a..0000000 --- a/hash.c +++ /dev/null @@ -1,143 +0,0 @@ -#include -#include -#include -#include - -#include "node.h" -#include "hash.h" - -scope* mkscope(prev) -scope* prev; -{ - int i; - - scope *p = malloc(sizeof(scope)); - assert(p); - - for (i = 0; i < HASH_SIZE; i++) - p->table[i] = NULL; - - p->next = next; - p->function_boundry = 0; - - return p; -} - -void free_scope(s) -scope *s; -{ - int i; - - if (!s) - return; - - for (i = 0; i < HASH_SIZE; i++) { - free_nodes(s->table[i]); - } - - free(s); -} - -/*Copied from Compilers, Aho*/ -#define EOS '\0' -int hashpjw(s) -char* s; -{ - char *p; - unsigned h = 0, g; - - for (p = s; *p != EOS; p++) { - h = (h<<4) + *p; - if (g = h & 0xf0000000) { - h^= g>>24; - h^= g; - } - } - - return h % HASH_SIZE; -} - -scope* push_scope(root); -scope* root; -{ - scope *p = mkscope(); - p->next = root; - return p; -} - -scope* pop_scope(root); -scope *root; -{ - scope *p; - - if (!root) - return NULL; - - p = root->next; - - free_scope(root); - return p; -} - -node* scope_insert(s, name) -scope *s; -char *name; -{ - int hash = hashpwj(name); - - node *tmp = root->table[hash]; - return root->table[hash] = list_insert(tmp, name); -} - -node* scope_search(root, name) -scope *root; -char *name; -{ - int hash = hashpwj(name); - - node *tmp = root->table[hash]; - return list_search(tmp, name); -} - -node* scope_search_all(root, name) -scope *root; -char *name; -{ - scope *p; - node *tmp; - - for (p = root; p; p = p->next) - if (tmp = scope_search(p, name)) - return tmp; - - return NULL; -} - -node* scope_safe_search(root, name) -scope *root; -char *name; -{ - scope *p; - node *tmp; - - for (p = root; p; p = p->next) { - if (tmp = scope_search(p, name)) - return tmp; - if (p->f) - return NULL - } - - return NULL; -} - -void print_scope(s) -scope *s; -{ - int i; - node_t * tmp; - for (i = 0; i < HASH_SIZE; i++) { - for( tmp=s->table[i]; tmp; tmp = tmp->next) { - fprintf(stderr, "\t%s\n", tmp->name); - } - } -} diff --git a/hash.h b/hash.h deleted file mode 100644 index 8a8cdb1..0000000 --- a/hash.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef SCOPE_H -#define SCOPE_H - -#define HASH_SIZE 211 - -typedef struct hash { - node* table[HASH_SIZE]; - struct hash *prev, *next; - char function_boundry; -} scope; - -scope* mkscope(scope*); -void free_scope(scope*); - -/*stack routines*/ -scope* pop_scope(scope*); -scope* push_scope(scope*); - -/*helpers*/ -node* scope_insert(scope*, char*); -node* scope_search_all(scope*, char*); -node* scope_search(scope*, char*); -node* scope_safe_search_all(scope*, char*); - -/*hash function*/ -int hash_pwj(char*); -#endif diff --git a/makefile b/makefile index 9a2f51a..8dd5899 100644 --- a/makefile +++ b/makefile @@ -3,8 +3,8 @@ FLAGS = -g YACC = yacc LEX = lex -mypc: y.tab.o lex.yy.o tree.o hash.o node.o pc.o - $(CC) $(FLAGS) -o mypc main.o tree.o hash.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 + $(CC) $(FLAGS) -o mypc main.o tree.o scope.o node.o y.tab.o lex.yy.o -lfl -ly pc.o: main.c pc.h $(CC) $(FLAGS) -c main.c @@ -12,8 +12,8 @@ pc.o: main.c pc.h tree.o: tree.c tree.h $(CC) $(FLAGS) -c tree.c -hash.o: hash.c hash.h - $(CC) $(FLAGS) -c hash.c +scope.o: scope.c scope.h + $(CC) $(FLAGS) -c scope.c node.o: node.c node.h $(CC) $(FLAGS) -c node.c diff --git a/pc.y b/pc.y index 495f64a..e8f2baa 100644 --- a/pc.y +++ b/pc.y @@ -2,6 +2,8 @@ #include #include +#include "node.h" +#include "scope.h" #include "tree.h" #include "y.tab.h" #include "pc.h" diff --git a/scope.c b/scope.c new file mode 100644 index 0000000..32167fc --- /dev/null +++ b/scope.c @@ -0,0 +1,143 @@ +#include +#include +#include +#include + +#include "node.h" +#include "scope.h" + +scope* mkscope(prev) +scope* prev; +{ + int i; + + scope *p = malloc(sizeof(scope)); + assert(p); + + for (i = 0; i < HASH_SIZE; i++) + p->table[i] = NULL; + + p->next = next; + p->function_boundry = 0; + + return p; +} + +void free_scope(s) +scope *s; +{ + int i; + + if (!s) + return; + + for (i = 0; i < HASH_SIZE; i++) { + free_nodes(s->table[i]); + } + + free(s); +} + +/*Copied from Compilers, Aho*/ +#define EOS '\0' +int hashpjw(s) +char* s; +{ + char *p; + unsigned h = 0, g; + + for (p = s; *p != EOS; p++) { + h = (h<<4) + *p; + if (g = h & 0xf0000000) { + h^= g>>24; + h^= g; + } + } + + return h % HASH_SIZE; +} + +scope* push_scope(root); +scope* root; +{ + scope *p = mkscope(); + p->next = root; + return p; +} + +scope* pop_scope(root); +scope *root; +{ + scope *p; + + if (!root) + return NULL; + + p = root->next; + + free_scope(root); + return p; +} + +node* scope_insert(s, name) +scope *s; +char *name; +{ + int hash = hashpwj(name); + + node *tmp = root->table[hash]; + return root->table[hash] = list_insert(tmp, name); +} + +node* scope_search(root, name) +scope *root; +char *name; +{ + int hash = hashpwj(name); + + node *tmp = root->table[hash]; + return list_search(tmp, name); +} + +node* scope_search_all(root, name) +scope *root; +char *name; +{ + scope *p; + node *tmp; + + for (p = root; p; p = p->next) + if (tmp = scope_search(p, name)) + return tmp; + + return NULL; +} + +node* scope_safe_search(root, name) +scope *root; +char *name; +{ + scope *p; + node *tmp; + + for (p = root; p; p = p->next) { + if (tmp = scope_search(p, name)) + return tmp; + if (p->f) + return NULL + } + + return NULL; +} + +void print_scope(s) +scope *s; +{ + int i; + node_t * tmp; + for (i = 0; i < HASH_SIZE; i++) { + for( tmp=s->table[i]; tmp; tmp = tmp->next) { + fprintf(stderr, "\t%s\n", tmp->name); + } + } +} diff --git a/scope.h b/scope.h new file mode 100644 index 0000000..8a8cdb1 --- /dev/null +++ b/scope.h @@ -0,0 +1,27 @@ +#ifndef SCOPE_H +#define SCOPE_H + +#define HASH_SIZE 211 + +typedef struct hash { + node* table[HASH_SIZE]; + struct hash *prev, *next; + char function_boundry; +} scope; + +scope* mkscope(scope*); +void free_scope(scope*); + +/*stack routines*/ +scope* pop_scope(scope*); +scope* push_scope(scope*); + +/*helpers*/ +node* scope_insert(scope*, char*); +node* scope_search_all(scope*, char*); +node* scope_search(scope*, char*); +node* scope_safe_search_all(scope*, char*); + +/*hash function*/ +int hash_pwj(char*); +#endif -- cgit v1.1 From d9ba5ec2f86dd69eae4edac586a889fccd8d31b1 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 27 Jul 2019 13:26:15 -0400 Subject: Fix typos & errors --- main.c | 2 ++ node.c | 4 ++-- scope.c | 27 +++++++++++++-------------- scope.h | 4 ++-- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/main.c b/main.c index 3d1401c..fbd96d0 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,8 @@ #include #include +#include "node.h" +#include "scope.h" #include "y.tab.h" #include "pc.h" diff --git a/node.c b/node.c index ca6548b..fbbfd14 100644 --- a/node.c +++ b/node.c @@ -19,7 +19,7 @@ char *str; } /* helpers */ -node* search(root, str) +node* list_search(root, str) node *root; char *str; { @@ -33,7 +33,7 @@ char *str; return NULL; } -node* insert(root, str) /*TODO change to accept double pointer*/ +node* list_insert(root, str) /*TODO change to accept double pointer*/ node *root; char * str; { diff --git a/scope.c b/scope.c index 32167fc..d09f1b4 100644 --- a/scope.c +++ b/scope.c @@ -6,8 +6,7 @@ #include "node.h" #include "scope.h" -scope* mkscope(prev) -scope* prev; +scope* mkscope() { int i; @@ -17,7 +16,7 @@ scope* prev; for (i = 0; i < HASH_SIZE; i++) p->table[i] = NULL; - p->next = next; + p->next = NULL; p->function_boundry = 0; return p; @@ -32,7 +31,7 @@ scope *s; return; for (i = 0; i < HASH_SIZE; i++) { - free_nodes(s->table[i]); + free_list(s->table[i]); } free(s); @@ -57,15 +56,15 @@ char* s; return h % HASH_SIZE; } -scope* push_scope(root); -scope* root; +scope* push_scope(root) +scope *root; { scope *p = mkscope(); p->next = root; return p; } -scope* pop_scope(root); +scope* pop_scope(root) scope *root; { scope *p; @@ -79,11 +78,11 @@ scope *root; return p; } -node* scope_insert(s, name) -scope *s; +node* scope_insert(root, name) +scope *root; char *name; { - int hash = hashpwj(name); + int hash = hashpjw(name); node *tmp = root->table[hash]; return root->table[hash] = list_insert(tmp, name); @@ -93,7 +92,7 @@ node* scope_search(root, name) scope *root; char *name; { - int hash = hashpwj(name); + int hash = hashpjw(name); node *tmp = root->table[hash]; return list_search(tmp, name); @@ -123,8 +122,8 @@ char *name; for (p = root; p; p = p->next) { if (tmp = scope_search(p, name)) return tmp; - if (p->f) - return NULL + if (p->function_boundry) + return NULL; } return NULL; @@ -134,7 +133,7 @@ void print_scope(s) scope *s; { int i; - node_t * tmp; + node * tmp; for (i = 0; i < HASH_SIZE; i++) { for( tmp=s->table[i]; tmp; tmp = tmp->next) { fprintf(stderr, "\t%s\n", tmp->name); diff --git a/scope.h b/scope.h index 8a8cdb1..7160b5e 100644 --- a/scope.h +++ b/scope.h @@ -9,7 +9,7 @@ typedef struct hash { char function_boundry; } scope; -scope* mkscope(scope*); +scope* mkscope(); void free_scope(scope*); /*stack routines*/ @@ -23,5 +23,5 @@ node* scope_search(scope*, char*); node* scope_safe_search_all(scope*, char*); /*hash function*/ -int hash_pwj(char*); +int hashpjw(char*); #endif -- cgit v1.1 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 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 From a7d28593769b094d52407c92b8e5554a3e9cd178 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sun, 28 Jul 2019 19:50:59 -0400 Subject: Add pop scope --- pc.y | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pc.y b/pc.y index dc6a05b..f00d4fe 100644 --- a/pc.y +++ b/pc.y @@ -159,6 +159,7 @@ sub_prog_declaration sub_prog_declarations compound_statement { + pop_scope(&cur_scope); } ; @@ -171,7 +172,6 @@ sub_prog_head cur_scope->ret_var = scope_insert(cur_scope, $2); cur_scope->ret_var->var_type = $5; - } |PROC ID arguments ';' { -- cgit v1.1 From f1c02ce1c731d3b7cbe505764352012b7ab83887 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Wed, 31 Jul 2019 21:59:48 -0400 Subject: WIP Start dealing with semantic checking of ids Current bugs: Checks id during var assignment --- pc.y | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pc.y b/pc.y index f00d4fe..a4a918b 100644 --- a/pc.y +++ b/pc.y @@ -102,21 +102,27 @@ program id_list :ID { + /*TODO remove check_ids*/ + node *tmp; check_id(cur_scope, $1); - $$ = scope_insert(cur_scope, $1); + tmp = scope_insert(cur_scope, $1); + $$ = mkid(tmp); } |id_list ',' ID { - check_id(cur_scope, $3); + node *tmp; - $$ = mktree(LIST, $1, scope_insert(cur_scope, $3)); + check_id(cur_scope, $3); + tmp = scope_insert(cur_scope, $3); + $$ = mktree(LIST, $1, mkid(tmp)); } ; var_declarations :var_declarations VAR id_list ':' type ';' { + /*CHECK IDS HERE*/ ptree *tmp; for(tmp = $3; tmp; tmp = tmp->l) { tmp->type = $5; -- cgit v1.1 From 3c0ca817145ad919dc6b92638edcffd739da7f8f Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Thu, 1 Aug 2019 21:52:08 -0400 Subject: WIP Add sem_check files Current (known) bugs: Checks id during var assignment --- sem_check.c | 36 ++++++++++++++++++++++++++++++++++++ sem_check.h | 8 ++++++++ 2 files changed, 44 insertions(+) create mode 100644 sem_check.c create mode 100644 sem_check.h diff --git a/sem_check.c b/sem_check.c new file mode 100644 index 0000000..637d044 --- /dev/null +++ b/sem_check.c @@ -0,0 +1,36 @@ +#include +#include + +#include "node.h" +#include "scope.h" +#include "tree.h" +#include "y.tab.h" +#include "pc.h" +#include "sem_check.h" + +void check_id(s, n) +scope *s; +char *n; +{ + char buf[100]; + + if (scope_search(s, n)) { + snprintf(buf, 100, "\"%s\" already defined in scope...\n", n); + yyerror(buf); + } +} + +node* check_exists(s, n) +scope *s; +char *n; +{ + node *tmp; + char buf[100]; + + if(!(tmp = scope_search(s,n))) { + snprintf(buf, 100, "Cannot find \"%s\"\n", n); + yyerror(buf); + } + + return tmp; +} diff --git a/sem_check.h b/sem_check.h new file mode 100644 index 0000000..eef9e96 --- /dev/null +++ b/sem_check.h @@ -0,0 +1,8 @@ +#ifndef SEMCHECK_H +#define SEMCHECK_H + +void check_id(scope*, char*); + +node* check_exists(scope*, char*); + +#endif -- cgit v1.1 From 0e35d5eb2338d878283cd16357db99b49a8ee0ad Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 3 Aug 2019 13:43:37 -0400 Subject: Adds pascal files for testing --- check/main.p | 34 ++++++++++++++++++++++++++++ check/nesting.p | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 check/main.p create mode 100644 check/nesting.p diff --git a/check/main.p b/check/main.p new file mode 100644 index 0000000..f05808b --- /dev/null +++ b/check/main.p @@ -0,0 +1,34 @@ +program main ( input, output ); + + var a, b: integer; + var x,y,z: real; + var ai :array [1..10] of integer; + procedure boo (n: integer); + var a,c: integer; + begin + a := n + end; + function bar (a: integer) : real; + var test:integer; + begin + test := 2; + a := 2 + end; + procedure foo; + begin + a := 33333; + x := 1e10; + z := 1e-10; + y := 2.5543e-2 + end; +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 + end +end. diff --git a/check/nesting.p b/check/nesting.p new file mode 100644 index 0000000..6d24b6b --- /dev/null +++ b/check/nesting.p @@ -0,0 +1,69 @@ + +program main ( input, output ); + + var a, b: integer; + var x,y,z: real; + var ai :array [1..10] of integer; + procedure boo (n: integer); + var a,c: integer; + begin + a := n + end; + function bar (a: integer) : real; + var test:integer; + begin + test := 2; + a := 2 + end; + procedure foo; + procedure foo2; + procedure foo13; + procedure foo4; + procedure foo5; + procedure foo6; + procedure foo7; + procedure foo46; + procedure fooaoesut; + procedure foosathst; + procedure foothsth; + begin + a := 33333 + end; + begin + a := 33333 + end; + begin + a := 33333 + end; + begin + a := 33333 + end; + begin + a := 33333 + end; + begin + a := 33333 + end; + begin + a := 33333 + end; + begin + a := 33333 + end; + begin + a := 33333 + end; + begin + a := 33333 + end; + begin + a := 33333 + end; +begin +{ TEST } + + a := 1; + x := 3.14; + b := a + 35 + (* test *) +end. -- cgit v1.1 From 51c0c989e7beb784c772dd8328f51fb6443e751c Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 3 Aug 2019 17:40:26 -0400 Subject: Fix id declare error during var assignment --- pc.y | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/pc.y b/pc.y index a4a918b..6538ef6 100644 --- a/pc.y +++ b/pc.y @@ -9,10 +9,6 @@ #include "pc.h" #include "sem_check.h" -/* -TODO: -- Add checkid() to counter mkid() -*/ extern int yylex(); extern scope *cur_scope; @@ -122,11 +118,10 @@ id_list var_declarations :var_declarations VAR id_list ':' type ';' { - /*CHECK IDS HERE*/ ptree *tmp; - for(tmp = $3; tmp; tmp = tmp->l) { + for(tmp = $3; tmp; tmp = tmp->l) tmp->type = $5; - } + } |/*empty*/ ; @@ -267,13 +262,11 @@ TD: TO | DT; var :ID { - check_id(cur_scope, $1); $$ = mkid(scope_insert(cur_scope,$1)); } |ID '[' expr ']' { node* tmp; - check_id(cur_scope, $1); tmp = scope_insert(cur_scope, $1); $$ = mktree(ARRAY_ACCESS, mkid(tmp), $3); -- cgit v1.1 From 8cd6be853b61798a5fd4114292c62bb9384497bc Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 3 Aug 2019 17:45:09 -0400 Subject: Fix double free error --- node.c | 5 ++++- scope.c | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/node.c b/node.c index fbbfd14..586ed52 100644 --- a/node.c +++ b/node.c @@ -47,7 +47,10 @@ node *n; { node *tmp; - for(tmp = n; tmp; tmp = n = n->next) { + for(tmp = n; tmp;) { + n = tmp->next; free(tmp); + tmp = NULL; + tmp = n; } } diff --git a/scope.c b/scope.c index 11a9c5b..6706765 100644 --- a/scope.c +++ b/scope.c @@ -35,6 +35,7 @@ scope *s; } free(s); + s = NULL; } /*Copied from Compilers, Aho*/ -- cgit v1.1 From 12f50dec6a529784a360dfaec9dc562b810ae2fd Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 3 Aug 2019 17:48:44 -0400 Subject: Fix change compiler in makefile --- makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefile b/makefile index c429f3c..27d0649 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ -CC = gcc +CC = tcc FLAGS = -g YACC = yacc LEX = lex -- cgit v1.1