aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/generator.c4
-rw-r--r--src/symbol_table.c28
-rw-r--r--src/symbol_table.h4
3 files changed, 28 insertions, 8 deletions
diff --git a/src/generator.c b/src/generator.c
index 33fade9..0040c02 100644
--- a/src/generator.c
+++ b/src/generator.c
@@ -96,12 +96,12 @@ void gen_const_declaration() {
96void gen_const(const char name[], int value, Scope scope) { 96void gen_const(const char name[], int value, Scope scope) {
97 switch (scope) { 97 switch (scope) {
98 case LOCAL: 98 case LOCAL:
99 loc_addVar(name, INT); // TODO: make read only 99 loc_addConst(name);
100 fprintf(output, "push %d\n", value); 100 fprintf(output, "push %d\n", value);
101 return; 101 return;
102 102
103 case GLOBAL: 103 case GLOBAL:
104 glo_addVar(name, INT); // TODO: make read only 104 glo_addConst(name);
105 fprintf(output, "%s: db QWORD %d\n", name, value); 105 fprintf(output, "%s: db QWORD %d\n", name, value);
106 return; 106 return;
107 } 107 }
diff --git a/src/symbol_table.c b/src/symbol_table.c
index 483de4c..08124ac 100644
--- a/src/symbol_table.c
+++ b/src/symbol_table.c
@@ -66,7 +66,7 @@ int fun_lookup(const char name[], int nb_param) {
66 return -1; 66 return -1;
67} 67}
68 68
69void glo_addVar(const char name[], int type) { 69static void glo_add(const char name[], int type, bool read_only) {
70 int count; 70 int count;
71 for (count = 0; count < glo_symbol_table.size; count++) { 71 for (count = 0; count < glo_symbol_table.size; count++) {
72 if (!strcmp(glo_symbol_table.entries[count].name, name)) { 72 if (!strcmp(glo_symbol_table.entries[count].name, name)) {
@@ -82,8 +82,16 @@ void glo_addVar(const char name[], int type) {
82 } 82 }
83 strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); 83 strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name);
84 glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; 84 glo_symbol_table.entries[glo_symbol_table.size - 1].type = type;
85 glo_symbol_table.entries[glo_symbol_table.size - 1].addr = 85 glo_symbol_table.entries[glo_symbol_table.size - 1].addr = (glo_symbol_table.size - 1) * 8;
86 (glo_symbol_table.size - 1) * 8; 86 glo_symbol_table.entries[glo_symbol_table.size - 1].read_only = read_only;
87}
88
89void glo_addVar(const char name[], int type) {
90 glo_add(name, type, false);
91}
92
93void glo_addConst(const char name[]) {
94 glo_add(name, INT, true);
87} 95}
88 96
89// Verifies that the variable exists and returns the type 97// Verifies that the variable exists and returns the type
@@ -125,7 +133,7 @@ void glo_display_table() {
125 fprintf(output, "\n"); 133 fprintf(output, "\n");
126} 134}
127 135
128void loc_addVar(const char name[], int type) { 136static void loc_add(const char name[], int type, bool read_only) {
129 int count; 137 int count;
130 for (count = 0; count < loc_symbol_table.size; count++) { 138 for (count = 0; count < loc_symbol_table.size; count++) {
131 if (!strcmp(loc_symbol_table.entries[count].name, name)) { 139 if (!strcmp(loc_symbol_table.entries[count].name, name)) {
@@ -141,8 +149,16 @@ void loc_addVar(const char name[], int type) {
141 } 149 }
142 strcpy(loc_symbol_table.entries[loc_symbol_table.size - 1].name, name); 150 strcpy(loc_symbol_table.entries[loc_symbol_table.size - 1].name, name);
143 loc_symbol_table.entries[loc_symbol_table.size - 1].type = type; 151 loc_symbol_table.entries[loc_symbol_table.size - 1].type = type;
144 loc_symbol_table.entries[loc_symbol_table.size - 1].addr = 152 loc_symbol_table.entries[loc_symbol_table.size - 1].addr = (loc_symbol_table.size - 1) * 8 + 8;
145 (loc_symbol_table.size - 1) * 8 + 8; 153 loc_symbol_table.entries[loc_symbol_table.size - 1].read_only = read_only;
154}
155
156void loc_addVar(const char name[], int type) {
157 loc_add(name, type, false);
158}
159
160void loc_addConst(const char name[]) {
161 loc_add(name, INT, true);
146} 162}
147 163
148int loc_lookup(const char name[]) { 164int loc_lookup(const char name[]) {
diff --git a/src/symbol_table.h b/src/symbol_table.h
index d0ac440..b2c8879 100644
--- a/src/symbol_table.h
+++ b/src/symbol_table.h
@@ -9,6 +9,7 @@
9#include <stdio.h> 9#include <stdio.h>
10#include <stdlib.h> 10#include <stdlib.h>
11#include <string.h> 11#include <string.h>
12#include <stdbool.h>
12 13
13#define MAXNAME 32 14#define MAXNAME 32
14#define MAXSYMBOLS 256 15#define MAXSYMBOLS 256
@@ -24,6 +25,7 @@ typedef struct {
24 char name[MAXNAME]; 25 char name[MAXNAME];
25 int type; 26 int type;
26 int addr; 27 int addr;
28 bool read_only;
27} STentry; 29} STentry;
28 30
29typedef struct { 31typedef struct {
@@ -48,10 +50,12 @@ void fun_add(const char name[], int rt_type, int nb_par);
48void fun_display_table(); 50void fun_display_table();
49int fun_lookup(const char name[], int nb_param); 51int fun_lookup(const char name[], int nb_param);
50void glo_addVar(const char name[], int type); 52void glo_addVar(const char name[], int type);
53void glo_addConst(const char name[]);
51int glo_lookup(const char name[]); 54int glo_lookup(const char name[]);
52int glo_get_addr(const char name[]); 55int glo_get_addr(const char name[]);
53void glo_display_table(); 56void glo_display_table();
54void loc_addVar(const char name[], int type); 57void loc_addVar(const char name[], int type);
58void loc_addConst(const char name[]);
55int loc_lookup(const char name[]); 59int loc_lookup(const char name[]);
56int loc_get_addr(const char name[]); 60int loc_get_addr(const char name[]);
57void loc_display_table(); 61void loc_display_table();