aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/generator.c24
-rw-r--r--src/generator.h2
-rw-r--r--src/tpc.y20
3 files changed, 22 insertions, 24 deletions
diff --git a/src/generator.c b/src/generator.c
index bbb2af5..1b11b67 100644
--- a/src/generator.c
+++ b/src/generator.c
@@ -264,7 +264,7 @@ void gen_ifelse_end(int idx) {
264 264
265// ----- OPERATORS ----- 265// ----- OPERATORS -----
266 266
267int gen_assign(const char ident[], Scope scope) { 267static int gen_assign_simple(const char ident[], Scope scope) {
268 int l_addr = loc_get_addr(ident); 268 int l_addr = loc_get_addr(ident);
269 int g_addr = glo_get_addr(ident); 269 int g_addr = glo_get_addr(ident);
270 270
@@ -290,7 +290,7 @@ int gen_assign(const char ident[], Scope scope) {
290 } 290 }
291} 291}
292 292
293int gen_assign_tab(const char ident[], Scope scope) { 293static int gen_assign_tab(const char ident[], Scope scope) {
294 int l_addr = loc_get_addr(ident); 294 int l_addr = loc_get_addr(ident);
295 int g_addr = glo_get_addr(ident); 295 int g_addr = glo_get_addr(ident);
296 296
@@ -322,6 +322,13 @@ int gen_assign_tab(const char ident[], Scope scope) {
322 } 322 }
323} 323}
324 324
325int gen_assign(const char ident[], Scope scope) {
326 switch (loc_lookup(ident)) {
327 case TAB: return gen_assign_tab(ident, scope);
328 default: return gen_assign_simple(ident, scope);
329 }
330}
331
325void gen_or(int left, int right, int idx) { 332void gen_or(int left, int right, int idx) {
326 check_expected_types(left, INT, TAB); 333 check_expected_types(left, INT, TAB);
327 check_expected_types(right, INT, TAB); 334 check_expected_types(right, INT, TAB);
@@ -452,7 +459,7 @@ int gen_negate_expr(int type) {
452 return type; 459 return type;
453} 460}
454 461
455int gen_value(const char ident[], Scope scope) { 462static int gen_value_simple(const char ident[], Scope scope) {
456 int l_addr = loc_get_addr(ident); 463 int l_addr = loc_get_addr(ident);
457 464
458 switch (scope) { 465 switch (scope) {
@@ -475,7 +482,8 @@ int gen_value(const char ident[], Scope scope) {
475 exit(1); 482 exit(1);
476 } 483 }
477} 484}
478int gen_value_tab(const char ident[], Scope scope) { 485
486static int gen_value_tab(const char ident[], Scope scope) {
479 int l_addr = loc_get_addr(ident); 487 int l_addr = loc_get_addr(ident);
480 int g_addr = glo_get_addr(ident); 488 int g_addr = glo_get_addr(ident);
481 switch (scope) { 489 switch (scope) {
@@ -500,6 +508,14 @@ int gen_value_tab(const char ident[], Scope scope) {
500 exit(1); 508 exit(1);
501 } 509 }
502} 510}
511
512int gen_value(const char ident[], Scope scope) {
513 switch (loc_lookup(ident)) {
514 case TAB: return gen_value_tab(ident, scope);
515 default: return gen_value_simple(ident, scope);
516 }
517}
518
503int gen_num(int value, Scope scope) { 519int gen_num(int value, Scope scope) {
504 fprintf(output, "push %d\n", value); 520 fprintf(output, "push %d\n", value);
505 return INT; 521 return INT;
diff --git a/src/generator.h b/src/generator.h
index 9d6dd65..e1e47df 100644
--- a/src/generator.h
+++ b/src/generator.h
@@ -39,7 +39,6 @@ void gen_if_end(int idx);
39void gen_ifelse_end(int idx); 39void gen_ifelse_end(int idx);
40 40
41int gen_assign(const char ident[], Scope scope); 41int gen_assign(const char ident[], Scope scope);
42int gen_assign_tab(const char ident[], Scope scope);
43 42
44void gen_or(int left, int right, int idx); 43void gen_or(int left, int right, int idx);
45void gen_and(int left, int right, int idx); 44void gen_and(int left, int right, int idx);
@@ -51,7 +50,6 @@ void gen_divstar(char op, int left, int right);
51int gen_signed_expr(char op, int type); 50int gen_signed_expr(char op, int type);
52int gen_negate_expr(int type); 51int gen_negate_expr(int type);
53int gen_value(const char ident[], Scope scope); 52int gen_value(const char ident[], Scope scope);
54int gen_value_tab(const char ident[], Scope scope);
55 53
56int gen_num(int value, Scope scope); 54int gen_num(int value, Scope scope);
57int gen_char(int value, Scope scope); 55int gen_char(int value, Scope scope);
diff --git a/src/tpc.y b/src/tpc.y
index 74d31c6..6e733d0 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -2,11 +2,6 @@
2/* 2/*
3 * UPEM / Compilation / Projet 3 * UPEM / Compilation / Projet
4 * Pacien TRAN-GIRARD, Adam NAILI 4 * Pacien TRAN-GIRARD, Adam NAILI
5 *
6 * TODO :
7 * ------
8 * - arrays
9 *
10 */ 5 */
11 6
12int nb_globals = 0; 7int nb_globals = 0;
@@ -147,13 +142,7 @@ IfHandling: { gen_if_start($<num>$ = num_if++); };
147IfEndHandling: { gen_if_end($<num>-3); }; 142IfEndHandling: { gen_if_end($<num>-3); };
148IfElseEndHandling: { gen_ifelse_end($<num>-5); }; 143IfElseEndHandling: { gen_ifelse_end($<num>-5); };
149Exp: 144Exp:
150 LValue '=' Exp { 145 LValue '=' Exp { $$ = gen_assign($<ident>1, scope); }
151 if(loc_lookup($<ident>1) != TAB){
152 $$ = gen_assign($<ident>1, scope);
153 }else{
154 $$ = gen_assign_tab($<ident>1,scope);
155 }
156 }
157| EB 146| EB
158; 147;
159EB: 148EB:
@@ -184,12 +173,7 @@ F:
184 ADDSUB F { $$ = gen_signed_expr($1, $2); } 173 ADDSUB F { $$ = gen_signed_expr($1, $2); }
185| '!' F { $$ = gen_negate_expr($2); } 174| '!' F { $$ = gen_negate_expr($2); }
186| '(' Exp ')' { $$ = $2; } 175| '(' Exp ')' { $$ = $2; }
187| LValue { if(loc_lookup($<ident>1) != TAB){ 176| LValue { $$ = gen_value($<ident>1, scope); }
188 $$ = gen_value($<ident>1, scope);
189 }else{
190 $$ = gen_value_tab($<ident>1,scope);
191 }
192 }
193| NUM { $$ = gen_num($1, scope); } 177| NUM { $$ = gen_num($1, scope); }
194| CARACTERE { $$ = gen_char($1, scope); } 178| CARACTERE { $$ = gen_char($1, scope); }
195| IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1,$<num>3); } 179| IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1,$<num>3); }