diff options
Diffstat (limited to 'src/symboltable.c')
-rw-r--r-- | src/symboltable.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/symboltable.c b/src/symboltable.c new file mode 100644 index 0000000..852d1b8 --- /dev/null +++ b/src/symboltable.c | |||
@@ -0,0 +1,35 @@ | |||
1 | #include "symboltable.h" | ||
2 | |||
3 | extern int lineno; /* from lexical analyser */ | ||
4 | |||
5 | SymbolTable symbol_table = {{{{0},0}},MAXSYMBOLS,0}; | ||
6 | |||
7 | void addVar(const char name[], int type) { | ||
8 | int count; | ||
9 | for (count = 0; count < symbol_table.size; count++) { | ||
10 | if (!strcmp(symbol_table.entries[count].name, name)) { | ||
11 | printf("semantic error, redefinition of variable %s near line %d\n", name, | ||
12 | lineno); | ||
13 | return; | ||
14 | } | ||
15 | } | ||
16 | if (++symbol_table.size > symbol_table.maxsize) { | ||
17 | printf("too many variables near line %d\n", lineno); | ||
18 | exit(1); | ||
19 | } | ||
20 | strcpy(symbol_table.entries[symbol_table.size - 1].name, name); | ||
21 | symbol_table.entries[symbol_table.size - 1].type = type; | ||
22 | } | ||
23 | |||
24 | void lookup(const char name[]) { | ||
25 | int count; | ||
26 | |||
27 | for (count = 0; count < symbol_table.size; count++) { | ||
28 | if (!strcmp(symbol_table.entries[count].name, name)) { | ||
29 | return; | ||
30 | } | ||
31 | printf("No definition of the variable %s near line %d\n", name, | ||
32 | lineno); | ||
33 | } | ||
34 | } | ||
35 | |||