diff options
author | pacien | 2018-06-05 22:16:36 +0200 |
---|---|---|
committer | pacien | 2018-06-05 22:16:36 +0200 |
commit | d07aa27c361c424e1da383ae49e98e5dfb33425e (patch) | |
tree | ba1d1e01e9cf3a5ff31d591ba5a807d0bd5f6c10 /src | |
parent | cabbac92126f168124372fc502dbc1fa2313eee3 (diff) | |
download | tpc-compiler-d07aa27c361c424e1da383ae49e98e5dfb33425e.tar.gz |
handle decl. of read-only vars
Diffstat (limited to 'src')
-rw-r--r-- | src/generator.c | 4 | ||||
-rw-r--r-- | src/symbol_table.c | 28 | ||||
-rw-r--r-- | src/symbol_table.h | 4 |
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() { | |||
96 | void gen_const(const char name[], int value, Scope scope) { | 96 | void 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 | ||
69 | void glo_addVar(const char name[], int type) { | 69 | static 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 | |||
89 | void glo_addVar(const char name[], int type) { | ||
90 | glo_add(name, type, false); | ||
91 | } | ||
92 | |||
93 | void 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 | ||
128 | void loc_addVar(const char name[], int type) { | 136 | static 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 | |||
156 | void loc_addVar(const char name[], int type) { | ||
157 | loc_add(name, type, false); | ||
158 | } | ||
159 | |||
160 | void loc_addConst(const char name[]) { | ||
161 | loc_add(name, INT, true); | ||
146 | } | 162 | } |
147 | 163 | ||
148 | int loc_lookup(const char name[]) { | 164 | int 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 | ||
29 | typedef struct { | 31 | typedef struct { |
@@ -48,10 +50,12 @@ void fun_add(const char name[], int rt_type, int nb_par); | |||
48 | void fun_display_table(); | 50 | void fun_display_table(); |
49 | int fun_lookup(const char name[], int nb_param); | 51 | int fun_lookup(const char name[], int nb_param); |
50 | void glo_addVar(const char name[], int type); | 52 | void glo_addVar(const char name[], int type); |
53 | void glo_addConst(const char name[]); | ||
51 | int glo_lookup(const char name[]); | 54 | int glo_lookup(const char name[]); |
52 | int glo_get_addr(const char name[]); | 55 | int glo_get_addr(const char name[]); |
53 | void glo_display_table(); | 56 | void glo_display_table(); |
54 | void loc_addVar(const char name[], int type); | 57 | void loc_addVar(const char name[], int type); |
58 | void loc_addConst(const char name[]); | ||
55 | int loc_lookup(const char name[]); | 59 | int loc_lookup(const char name[]); |
56 | int loc_get_addr(const char name[]); | 60 | int loc_get_addr(const char name[]); |
57 | void loc_display_table(); | 61 | void loc_display_table(); |