aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/symboltable.c124
-rw-r--r--src/symboltable.h3
-rw-r--r--src/tpc.y92
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
3extern int lineno; /* from lexical analyser */ 3extern int lineno; /* from lexical analyser */
4 4
5SymbolTable glo_symbol_table = {{{{0},0}},MAXSYMBOLS,0}; 5SymbolTable glo_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0};
6SymbolTable loc_symbol_table = {{{{0},0}},MAXSYMBOLS,0}; 6SymbolTable loc_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0};
7 7
8void glo_addVar(const char name[], int type) { 8void 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
27int glo_lookup(const char name[]) { 29int 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}
41int glo_get_addr(const char name[]) {
42 int count;
39 43
40 44 for (count = 0; count < glo_symbol_table.size; count++) {
41void 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
54void 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
53void loc_addVar(const char name[], int type) { 69void 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
72int loc_lookup(const char name[]) { 89int 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}
84void loc_display_table(){ 101
85 int count; 102int 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}
114void 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
129void 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
137void 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
27void glo_addVar(const char name[], int type); 27void glo_addVar(const char name[], int type);
28int glo_lookup(const char name[]); 28int glo_lookup(const char name[]);
29int glo_get_addr(const char name[]);
29void glo_display_table(); 30void glo_display_table();
30void loc_addVar(const char name[], int type); 31void loc_addVar(const char name[], int type);
31int loc_lookup(const char name[]); 32int loc_lookup(const char name[]);
33int loc_get_addr(const char name[]);
32void loc_display_table(); 34void loc_display_table();
35void 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
diff --git a/src/tpc.y b/src/tpc.y
index c575863..387b3ef 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -10,10 +10,10 @@
10extern int lineno; 10extern int lineno;
11int yylex(); 11int yylex();
12void yyerror(char *); 12void yyerror(char *);
13
14#define GLOBAL 0 13#define GLOBAL 0
15#define LOCAL 1 14#define LOCAL 1
16static int status = GLOBAL; 15static int status = GLOBAL;
16
17%} 17%}
18 18
19%union { 19%union {
@@ -44,9 +44,9 @@ static int status = GLOBAL;
44%% 44%%
45Prog:{printf("extern printf\n"); 45Prog:{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 printf("print: ;print needs an argument in rax\n");
50 printf("push rbp\n"); 50 printf("push rbp\n");
51 printf("mov rbp, rsp\n"); 51 printf("mov rbp, rsp\n");