diff options
author | Adam NAILI | 2018-05-04 01:03:21 +0200 |
---|---|---|
committer | Adam NAILI | 2018-05-04 01:03:21 +0200 |
commit | c9c4fbbde6e4f4f3942777cd9930cf40375e0ea9 (patch) | |
tree | dc6c860b2934dadd75a000bd797934371a651f64 /src | |
parent | 2793b31c330396956e4fbeef454e26a37b055ebd (diff) | |
download | tpc-compiler-c9c4fbbde6e4f4f3942777cd9930cf40375e0ea9.tar.gz |
Add sub working (local/global not fully implemented, checking type function available
Diffstat (limited to 'src')
-rw-r--r-- | src/symboltable.c | 124 | ||||
-rw-r--r-- | src/symboltable.h | 3 | ||||
-rw-r--r-- | src/tpc.y | 92 |
3 files changed, 157 insertions, 62 deletions
diff --git a/src/symboltable.c b/src/symboltable.c index cdbd442..5c63d3f 100644 --- a/src/symboltable.c +++ b/src/symboltable.c | |||
@@ -2,28 +2,30 @@ | |||
2 | 2 | ||
3 | extern int lineno; /* from lexical analyser */ | 3 | extern int lineno; /* from lexical analyser */ |
4 | 4 | ||
5 | SymbolTable glo_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 | SymbolTable loc_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0}; |
7 | 7 | ||
8 | void glo_addVar(const char name[], int type) { | 8 | void glo_addVar(const char name[], int type) { |
9 | int count; | 9 | int count; |
10 | for (count = 0; count < glo_symbol_table.size; count++) { | 10 | for (count = 0; count < glo_symbol_table.size; count++) { |
11 | if (!strcmp(glo_symbol_table.entries[count].name, name)) { | 11 | if (!strcmp(glo_symbol_table.entries[count].name, name)) { |
12 | fprintf(stderr,"semantic error, redefinition of variable %s near line %d\n", name, | 12 | fprintf(stderr, |
13 | lineno); | 13 | "semantic error, redefinition of variable %s near line %d\n", |
14 | name, lineno); | ||
14 | return; | 15 | return; |
15 | } | 16 | } |
16 | } | 17 | } |
17 | if (++glo_symbol_table.size > glo_symbol_table.maxsize) { | 18 | if (++glo_symbol_table.size > glo_symbol_table.maxsize) { |
18 | fprintf(stderr,"too many variables near line %d\n", lineno); | 19 | fprintf(stderr, "too many variables near line %d\n", lineno); |
19 | exit(1); | 20 | exit(1); |
20 | } | 21 | } |
21 | strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); | 22 | strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); |
22 | glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; | 23 | 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; | 24 | glo_symbol_table.entries[glo_symbol_table.size - 1].addr = |
25 | (glo_symbol_table.size - 1) * 8; | ||
24 | } | 26 | } |
25 | 27 | ||
26 | //Verifies that the variable exists and returns the type | 28 | // Verifies that the variable exists and returns the type |
27 | int glo_lookup(const char name[]) { | 29 | int glo_lookup(const char name[]) { |
28 | int count; | 30 | int count; |
29 | 31 | ||
@@ -32,43 +34,58 @@ int glo_lookup(const char name[]) { | |||
32 | return glo_symbol_table.entries[count].type; | 34 | return glo_symbol_table.entries[count].type; |
33 | } | 35 | } |
34 | } | 36 | } |
35 | fprintf(stderr,"No definition of the variable %s near line %d\n", name, | 37 | fprintf(stderr, "No definition of the variable %s near line %d\n", name, |
36 | lineno); | 38 | lineno); |
37 | return -1; | 39 | return -1; |
38 | } | 40 | } |
41 | int glo_get_addr(const char name[]) { | ||
42 | int count; | ||
39 | 43 | ||
40 | 44 | for (count = 0; count < glo_symbol_table.size; count++) { | |
41 | void glo_display_table(){ | 45 | if (!strcmp(glo_symbol_table.entries[count].name, name)) { |
42 | int count; | 46 | return glo_symbol_table.entries[count].addr; |
43 | for (count=0;count<glo_symbol_table.size;count++) { | ||
44 | if(glo_symbol_table.entries[count].type == INT) | ||
45 | printf("entier: %s, pos: %d \n", glo_symbol_table.entries[count].name, glo_symbol_table.entries[count].addr); | ||
46 | else | ||
47 | printf("caractere: %s, pos: %d \n", glo_symbol_table.entries[count].name, glo_symbol_table.entries[count].addr); | ||
48 | } | 47 | } |
49 | printf("\n"); | 48 | } |
49 | fprintf(stderr, "No definition of the variable %s near line %d\n", name, | ||
50 | lineno); | ||
51 | return -1; | ||
50 | } | 52 | } |
51 | 53 | ||
54 | void glo_display_table() { | ||
55 | int count; | ||
56 | for (count = 0; count < glo_symbol_table.size; count++) { | ||
57 | if (glo_symbol_table.entries[count].type == INT) | ||
58 | printf(";entier: %s, pos: %d \n", | ||
59 | glo_symbol_table.entries[count].name, | ||
60 | glo_symbol_table.entries[count].addr); | ||
61 | else | ||
62 | printf(";caractere: %s, pos: %d \n", | ||
63 | glo_symbol_table.entries[count].name, | ||
64 | glo_symbol_table.entries[count].addr); | ||
65 | } | ||
66 | printf("\n"); | ||
67 | } | ||
52 | 68 | ||
53 | void loc_addVar(const char name[], int type) { | 69 | void loc_addVar(const char name[], int type) { |
54 | int count; | 70 | int count; |
55 | for (count = 0; count < loc_symbol_table.size; count++) { | 71 | for (count = 0; count < loc_symbol_table.size; count++) { |
56 | if (!strcmp(loc_symbol_table.entries[count].name, name)) { | 72 | if (!strcmp(loc_symbol_table.entries[count].name, name)) { |
57 | fprintf(stderr,"semantic error, redefinition of variable %s near line %d\n", name, | 73 | fprintf(stderr, |
58 | lineno); | 74 | "semantic error, redefinition of variable %s near line %d\n", |
75 | name, lineno); | ||
59 | return; | 76 | return; |
60 | } | 77 | } |
61 | } | 78 | } |
62 | if (++glo_symbol_table.size > glo_symbol_table.maxsize) { | 79 | if (++loc_symbol_table.size > loc_symbol_table.maxsize) { |
63 | fprintf(stderr,"too many variables near line %d\n", lineno); | 80 | fprintf(stderr, "too many variables near line %d\n", lineno); |
64 | exit(1); | 81 | exit(1); |
65 | } | 82 | } |
66 | strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); | 83 | strcpy(loc_symbol_table.entries[loc_symbol_table.size - 1].name, name); |
67 | glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; | 84 | loc_symbol_table.entries[loc_symbol_table.size - 1].type = type; |
68 | glo_symbol_table.entries[glo_symbol_table.size - 1].addr = (glo_symbol_table.size - 1)*8; | 85 | loc_symbol_table.entries[loc_symbol_table.size - 1].addr = |
86 | (loc_symbol_table.size - 1) * 8; | ||
69 | } | 87 | } |
70 | 88 | ||
71 | |||
72 | int loc_lookup(const char name[]) { | 89 | int loc_lookup(const char name[]) { |
73 | int count; | 90 | int count; |
74 | 91 | ||
@@ -77,17 +94,52 @@ int loc_lookup(const char name[]) { | |||
77 | return loc_symbol_table.entries[count].type; | 94 | return loc_symbol_table.entries[count].type; |
78 | } | 95 | } |
79 | } | 96 | } |
80 | fprintf(stderr,"No definition of the variable %s near line %d\n", name, | 97 | fprintf(stderr, "No definition of the variable %s near line %d\n", name, |
81 | lineno); | 98 | lineno); |
82 | return -1; | 99 | return -1; |
83 | } | 100 | } |
84 | void loc_display_table(){ | 101 | |
85 | int count; | 102 | int loc_get_addr(const char name[]) { |
86 | for (count=0;count<loc_symbol_table.size;count++) { | 103 | int count; |
87 | if(loc_symbol_table.entries[count].type == INT) | 104 | |
88 | printf("entier: %s, pos: %d \n", loc_symbol_table.entries[count].name, loc_symbol_table.entries[count].addr); | 105 | for (count = 0; count < loc_symbol_table.size; count++) { |
89 | else | 106 | if (!strcmp(loc_symbol_table.entries[count].name, name)) { |
90 | printf("caractere: %s, pos: %d \n", loc_symbol_table.entries[count].name, loc_symbol_table.entries[count].addr); | 107 | return loc_symbol_table.entries[count].addr; |
91 | } | 108 | } |
92 | printf("\n"); | 109 | } |
110 | fprintf(stderr, "No definition of the variable %s near line %d\n", name, | ||
111 | lineno); | ||
112 | return -1; | ||
113 | } | ||
114 | void loc_display_table() { | ||
115 | int count; | ||
116 | for (count = 0; count < loc_symbol_table.size; count++) { | ||
117 | if (loc_symbol_table.entries[count].type == INT) | ||
118 | printf(";entier: %s, pos: %d \n", | ||
119 | loc_symbol_table.entries[count].name, | ||
120 | loc_symbol_table.entries[count].addr); | ||
121 | else | ||
122 | printf(";caractere: %s, pos: %d \n", | ||
123 | loc_symbol_table.entries[count].name, | ||
124 | loc_symbol_table.entries[count].addr); | ||
125 | } | ||
126 | printf("\n"); | ||
127 | } | ||
128 | |||
129 | void loc_clean_table() { | ||
130 | int i; | ||
131 | for (i = 0; i < loc_symbol_table.size; i++) { | ||
132 | printf("pop eax\n"); | ||
133 | } | ||
134 | loc_symbol_table.size = 0; | ||
135 | } | ||
136 | |||
137 | void check_expected_type(int type_to_check, int type_expected) { | ||
138 | if (type_to_check != type_expected) | ||
139 | fprintf(stderr, "Expected type : %s -> Got type : %s (near line %d)\n", | ||
140 | type_expected == INT ? "INT" : type_expected == CHAR ? "CHAR" | ||
141 | : "UNDEFINED", | ||
142 | type_to_check == INT ? "INT" : type_to_check == CHAR ? "CHAR" | ||
143 | : "UNDEFINED", | ||
144 | lineno); | ||
93 | } \ No newline at end of file | 145 | } \ No newline at end of file |
diff --git a/src/symboltable.h b/src/symboltable.h index bd7a373..ac6db36 100644 --- a/src/symboltable.h +++ b/src/symboltable.h | |||
@@ -26,8 +26,11 @@ typedef struct { | |||
26 | 26 | ||
27 | void glo_addVar(const char name[], int type); | 27 | void glo_addVar(const char name[], int type); |
28 | int glo_lookup(const char name[]); | 28 | int glo_lookup(const char name[]); |
29 | int glo_get_addr(const char name[]); | ||
29 | void glo_display_table(); | 30 | void glo_display_table(); |
30 | void loc_addVar(const char name[], int type); | 31 | void loc_addVar(const char name[], int type); |
31 | int loc_lookup(const char name[]); | 32 | int loc_lookup(const char name[]); |
33 | int loc_get_addr(const char name[]); | ||
32 | void loc_display_table(); | 34 | void loc_display_table(); |
35 | void check_expected_type(int type_to_check, int type_expected); | ||
33 | #endif \ No newline at end of file | 36 | #endif \ No newline at end of file |
@@ -10,10 +10,10 @@ | |||
10 | extern int lineno; | 10 | extern int lineno; |
11 | int yylex(); | 11 | int yylex(); |
12 | void yyerror(char *); | 12 | void yyerror(char *); |
13 | |||
14 | #define GLOBAL 0 | 13 | #define GLOBAL 0 |
15 | #define LOCAL 1 | 14 | #define LOCAL 1 |
16 | static int status = GLOBAL; | 15 | static int status = GLOBAL; |
16 | |||
17 | %} | 17 | %} |
18 | 18 | ||
19 | %union { | 19 | %union { |
@@ -44,9 +44,9 @@ static int status = GLOBAL; | |||
44 | %% | 44 | %% |
45 | Prog:{printf("extern printf\n"); | 45 | Prog:{printf("extern printf\n"); |
46 | printf("section .data\n"); | 46 | printf("section .data\n"); |
47 | printf("format_int db \"%%d \\n\",10,0\n\n"); | 47 | printf("format_int db \"%%d\",10,0\n\n"); |
48 | printf("section .bss\nsection .text\n\nglobal _start\n"); | 48 | printf("section .bss\nsection .text\n\nglobal _start\n"); |
49 | printf("print: ;print needs an argument in rax\n\n"); | 49 |