diff options
-rw-r--r-- | res/test_add.tpc | 5 | ||||
-rw-r--r-- | res/test_global.tpc | 11 | ||||
-rw-r--r-- | res/test_mul.tpc | 7 | ||||
-rw-r--r-- | src/tpc.y | 31 |
4 files changed, 33 insertions, 21 deletions
diff --git a/res/test_add.tpc b/res/test_add.tpc index 2f6b9dd..848130c 100644 --- a/res/test_add.tpc +++ b/res/test_add.tpc | |||
@@ -1,12 +1,11 @@ | |||
1 | /* test-table-symboles.tpc */ | ||
2 | |||
3 | /* Test file for simplified translator of a declaration of variables in C */ | ||
4 | entier a; | 1 | entier a; |
5 | 2 | ||
6 | entier main(void) { | 3 | entier main(void) { |
7 | entier a,b,res; | 4 | entier a,b,res; |
8 | a = 2; | 5 | a = 2; |
9 | b = 3; | 6 | b = 3; |
7 | res = a + b; | ||
8 | print(res); | ||
10 | res = a - b; | 9 | res = a - b; |
11 | print(res); | 10 | print(res); |
12 | } | 11 | } |
diff --git a/res/test_global.tpc b/res/test_global.tpc index 731fffb..8fda248 100644 --- a/res/test_global.tpc +++ b/res/test_global.tpc | |||
@@ -1,15 +1,14 @@ | |||
1 | /* test-table-symboles.tpc */ | ||
2 | |||
3 | /* Test file for simplified translator of a declaration of variables in C */ | ||
4 | entier r1,b,s,c,r2 ; | 1 | entier r1,b,s,c,r2 ; |
5 | caractere letter, digit, punct; | 2 | caractere letter, digit, punct; |
3 | entier i; | ||
4 | |||
6 | 5 | ||
7 | void calcul(void) { | 6 | void calcul(void) { |
8 | entier i; | ||
9 | i = 3; | ||
10 | r1=12; | 7 | r1=12; |
11 | r2=13; | 8 | r2=13; |
12 | b = r1 + r2 + i; | 9 | s = 3; |
10 | |||
11 | b = r1 + r2 + s; | ||
13 | print(b); | 12 | print(b); |
14 | } | 13 | } |
15 | 14 | ||
diff --git a/res/test_mul.tpc b/res/test_mul.tpc index f3b6705..62a8080 100644 --- a/res/test_mul.tpc +++ b/res/test_mul.tpc | |||
@@ -1,12 +1,13 @@ | |||
1 | /* test-table-symboles.tpc */ | ||
2 | |||
3 | /* Test file for simplified translator of a declaration of variables in C */ | ||
4 | entier a; | 1 | entier a; |
5 | 2 | ||
6 | entier main(void) { | 3 | entier main(void) { |
7 | entier a,b,res; | 4 | entier a,b,res; |
8 | a = 2; | 5 | a = 2; |
9 | b = 3; | 6 | b = 3; |
7 | res = a * b; | ||
8 | print(res); | ||
9 | res = a / b; | ||
10 | print(res); | ||
10 | res = a % b; | 11 | res = a % b; |
11 | print(res); | 12 | print(res); |
12 | } | 13 | } |
@@ -26,8 +26,9 @@ static int num_if = 0; | |||
26 | static int num_while = 0; | 26 | static int num_while = 0; |
27 | static int nb_param[255]; | 27 | static int nb_param[255]; |
28 | static int num_scope = -1; | 28 | static int num_scope = -1; |
29 | static int offset = 0; | ||
30 | static int is_array = 0; | ||
29 | static char fname[64]; | 31 | static char fname[64]; |
30 | static Type type; | ||
31 | %} | 32 | %} |
32 | 33 | ||
33 | %union { | 34 | %union { |
@@ -49,7 +50,7 @@ static Type type; | |||
49 | %token <type> TYPE | 50 | %token <type> TYPE |
50 | 51 | ||
51 | %type <num> Exp EB TB FB M E T F Parametres | 52 | %type <num> Exp EB TB FB M E T F Parametres |
52 | %type <ident> LValue | 53 | %type <ident> LValue Declarateur |
53 | 54 | ||
54 | %left ',' | 55 | %left ',' |
55 | %precedence ')' | 56 | %precedence ')' |
@@ -76,16 +77,28 @@ NombreSigne: | |||
76 | | ADDSUB NUM { $<num>$ = $<addsub>1 == '-' ? - $<num>2 : $<num>2; } | 77 | | ADDSUB NUM { $<num>$ = $<addsub>1 == '-' ? - $<num>2 : $<num>2; } |
77 | ; | 78 | ; |
78 | DeclVars: | 79 | DeclVars: |
79 | DeclVars TYPE Declarateurs ';' {type = $<type>2;} | 80 | DeclVars TYPE Declarateurs ';' |
80 | | | 81 | | |
81 | ; | 82 | ; |
82 | Declarateurs: | 83 | Declarateurs: |
83 | Declarateurs ',' Declarateur | 84 | Declarateurs ',' Declarateur { |
84 | | Declarateur | 85 | if(!is_array){ |
86 | gen_declaration($<ident>3, $<type>0, scope); | ||
87 | }else{ | ||
88 | gen_tab_declaration($<ident>3, scope, offset); | ||
89 | } | ||
90 | } | ||
91 | | Declarateur { | ||
92 | if(!is_array){ | ||
93 | gen_declaration($<ident>1, $<type>0, scope); | ||
94 | }else{ | ||
95 | gen_tab_declaration($<ident>1, scope, offset); | ||
96 | } | ||
97 | } | ||
85 | ; | 98 | ; |
86 | Declarateur: | 99 | Declarateur: |
87 | IDENT { gen_declaration($<ident>1, type, scope);} | 100 | IDENT {strcpy($$,$1);is_array = 0;} |
88 | | IDENT '[' NUM ']' { gen_tab_declaration($<ident>1, scope, $<num>3);} | 101 | | IDENT '[' NUM ']' {offset = $<num>3;strcpy($$,$1);is_array=1;} |
89 | ; | 102 | ; |
90 | DeclFoncts: | 103 | DeclFoncts: |
91 | DeclFoncts DeclFonct | 104 | DeclFoncts DeclFonct |
@@ -182,8 +195,8 @@ F: | |||
182 | | IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1,$<num>3); } | 195 | | IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1,$<num>3); } |
183 | ; | 196 | ; |
184 | LValue: | 197 | LValue: |
185 | IDENT { gen_check($<ident>1, scope); } | 198 | IDENT { gen_check($<ident>1, scope); strcpy($$, $1);} |
186 | | IDENT '[' Exp ']' { gen_check($<ident>1, scope); } | 199 | | IDENT '[' Exp ']' { gen_check($<ident>1, scope); strcpy($$, $1);} |
187 | ; | 200 | ; |
188 | Arguments: | 201 | Arguments: |
189 | ListExp | 202 | ListExp |