diff options
author | Adam NAILI | 2018-04-22 14:49:42 +0200 |
---|---|---|
committer | Adam NAILI | 2018-04-22 14:49:42 +0200 |
commit | 40f423a4b3b1e64e8424ab239cc14ecc5077640f (patch) | |
tree | c7da7c2eed97d62cc8ab2e003293935cda5a93dc /src/symboltable.c | |
parent | c0802bc17f856546b95a5b51252f7a35d9e1ab10 (diff) | |
download | tpc-compiler-40f423a4b3b1e64e8424ab239cc14ecc5077640f.tar.gz |
Beginning of the symbol table implementation
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 | |||