diff options
Diffstat (limited to 'src/symboltable.c')
-rw-r--r-- | src/symboltable.c | 85 |
1 files changed, 61 insertions, 24 deletions
diff --git a/src/symboltable.c b/src/symboltable.c index 3f22b44..cdbd442 100644 --- a/src/symboltable.c +++ b/src/symboltable.c | |||
@@ -2,55 +2,92 @@ | |||
2 | 2 | ||
3 | extern int lineno; /* from lexical analyser */ | 3 | extern int lineno; /* from lexical analyser */ |
4 | 4 | ||
5 | SymbolTable symbol_table = {{{{0},0}},MAXSYMBOLS,0}; | 5 | SymbolTable glo_symbol_table = {{{{0},0}},MAXSYMBOLS,0}; |
6 | SymbolTable loc_symbol_table = {{{{0},0}},MAXSYMBOLS,0}; | ||
6 | 7 | ||
7 | void addVar(const char name[], int type) { | 8 | void glo_addVar(const char name[], int type) { |
8 | int count; | 9 | int count; |
9 | for (count = 0; count < symbol_table.size; count++) { | 10 | for (count = 0; count < glo_symbol_table.size; count++) { |
10 | if (!strcmp(symbol_table.entries[count].name, name)) { | 11 | if (!strcmp(glo_symbol_table.entries[count].name, name)) { |
11 | printf("semantic error, redefinition of variable %s near line %d\n", name, | 12 | fprintf(stderr,"semantic error, redefinition of variable %s near line %d\n", name, |
12 | lineno); | 13 | lineno); |
13 | return; | 14 | return; |
14 | } | 15 | } |
15 | } | 16 | } |
16 | if (++symbol_table.size > symbol_table.maxsize) { | 17 | if (++glo_symbol_table.size > glo_symbol_table.maxsize) { |
17 | printf("too many variables near line %d\n", lineno); | 18 | fprintf(stderr,"too many variables near line %d\n", lineno); |
18 | exit(1); | 19 | exit(1); |
19 | } | 20 | } |
20 | strcpy(symbol_table.entries[symbol_table.size - 1].name, name); | 21 | strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); |
21 | symbol_table.entries[symbol_table.size - 1].type = type; | 22 | glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; |
23 | glo_symbol_table.entries[glo_symbol_table.size - 1].addr = (glo_symbol_table.size - 1)*8; | ||
22 | } | 24 | } |
23 | 25 | ||
24 | void lookup(const char name[]) { | 26 | //Verifies that the variable exists and returns the type |
27 | int glo_lookup(const char name[]) { | ||
25 | int count; | 28 | int count; |
26 | 29 | ||
27 | for (count = 0; count < symbol_table.size; count++) { | 30 | for (count = 0; count < glo_symbol_table.size; count++) { |
28 | if (!strcmp(symbol_table.entries[count].name, name)) { | 31 | if (!strcmp(glo_symbol_table.entries[count].name, name)) { |
29 | return; | 32 | return glo_symbol_table.entries[count].type; |
30 | } | 33 | } |
31 | } | 34 | } |
32 | printf("No definition of the variable %s near line %d\n", name, | 35 | fprintf(stderr,"No definition of the variable %s near line %d\n", name, |
33 | lineno); | 36 | lineno); |
37 | return -1; | ||
34 | } | 38 | } |
35 | 39 | ||
36 | void display_table(){ | 40 | |
41 | void glo_display_table(){ | ||
37 | int count; | 42 | int count; |
38 | for (count=0;count<symbol_table.size;count++) { | 43 | for (count=0;count<glo_symbol_table.size;count++) { |
39 | if(symbol_table.entries[count].type == INT) | 44 | if(glo_symbol_table.entries[count].type == INT) |
40 | printf("entier: %s, pos: %d \n", symbol_table.entries[count].name, symbol_table.entries[count].addr); | 45 | printf("entier: %s, pos: %d \n", glo_symbol_table.entries[count].name, glo_symbol_table.entries[count].addr); |
41 | else | 46 | else |
42 | printf("caractere: %s, pos: %d \n", symbol_table.entries[count].name, symbol_table.entries[count].addr); | 47 | printf("caractere: %s, pos: %d \n", glo_symbol_table.entries[count].name, glo_symbol_table.entries[count].addr); |
43 | } | 48 | } |
44 | printf("\n"); | 49 | printf("\n"); |
45 | } | 50 | } |
46 | 51 | ||
47 | int get_type(const char name[]){ | ||
48 | int count; | ||
49 | 52 | ||
50 | for (count = 0; count < symbol_table.size; count++) { | 53 | void loc_addVar(const char name[], int type) { |
51 | if (!strcmp(symbol_table.entries[count].name, name)) { | 54 | int count; |
52 | return symbol_table.entries[count].type; | 55 | for (count = 0; count < loc_symbol_table.size; count++) { |
56 | if (!strcmp(loc_symbol_table.entries[count].name, name)) { | ||
57 | fprintf(stderr,"semantic error, redefinition of variable %s near line %d\n", name, | ||
58 | lineno); | ||
59 | return; | ||
53 | } | 60 | } |
54 | } | 61 | } |
62 | if (++glo_symbol_table.size > glo_symbol_table.maxsize) { | ||
63 | fprintf(stderr,"too many variables near line %d\n", lineno); | ||
64 | exit(1); | ||
65 | } | ||
66 | strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); | ||
67 | glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; | ||
68 | glo_symbol_table.entries[glo_symbol_table.size - 1].addr = (glo_symbol_table.size - 1)*8; | ||
69 | } | ||
70 | |||
71 | |||
72 | int loc_lookup(const char name[]) { | ||
73 | int count; | ||
74 | |||
75 | for (count = 0; count < loc_symbol_table.size; count++) { | ||
76 | if (!strcmp(loc_symbol_table.entries[count].name, name)) { | ||
77 | return loc_symbol_table.entries[count].type; | ||
78 | } | ||
79 | } | ||
80 | fprintf(stderr,"No definition of the variable %s near line %d\n", name, | ||
81 | lineno); | ||
55 | return -1; | 82 | return -1; |
83 | } | ||
84 | void loc_display_table(){ | ||
85 | int count; | ||
86 | for (count=0;count<loc_symbol_table.size;count++) { | ||
87 | if(loc_symbol_table.entries[count].type == INT) | ||
88 | printf("entier: %s, pos: %d \n", loc_symbol_table.entries[count].name, loc_symbol_table.entries[count].addr); | ||
89 | else | ||
90 | printf("caractere: %s, pos: %d \n", loc_symbol_table.entries[count].name, loc_symbol_table.entries[count].addr); | ||
91 | } | ||
92 | printf("\n"); | ||
56 | } \ No newline at end of file | 93 | } \ No newline at end of file |