-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpret-expr-test.c
270 lines (221 loc) · 11.3 KB
/
interpret-expr-test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <stdio.h>
#include "ial.h"
#include "interpret.h"
#include "instruction_list.h"
int main () {
stab_t *sym_tab;
instruction_list_t *i_list;
data_t input_data;
data_t *ptr_to_table1, *ptr_to_table2;
char lokal_a[] = "Main.Run.a";
char lokal_b[] = "Main.Run.b";
char lokal_c[] = "Main.Run.c";
char stack_top[] = "stack-top";
sym_tab = stable_init(1024);
input_data.data.arg_type = INTEGER;
input_data.data.data.i = 5;
stable_add_var(sym_tab, lokal_b, input_data);
input_data.data.arg_type = INTEGER;
input_data.data.data.i = 8;
stable_add_var(sym_tab, lokal_a, input_data);
input_data.data.arg_type = STACK_EBP;
input_data.data.data.i = 1;
stable_add_var(sym_tab, lokal_c, input_data);
input_data.data.arg_type = ON_TOP;
input_data.data.data.i = 1;
stable_add_var(sym_tab, stack_top, input_data);
stable_print(sym_tab);
//TEST1
printf("#################### INTEGET-ONLY ##############\n");
printf("####################TEST1#######################\n");
printf("8 + 5 = 13 <<<>>> 8 5 +\nOcakavany vystup : 13\n");
i_list = init_inst_list();
create_and_add_instruction(i_list, INST_PUSH_INT, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_a);
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_b);
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_EXPR_ADD, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_c);
ptr_to_table2 = stable_get_var(sym_tab, stack_top);
create_and_add_instruction(i_list, INST_STORE, &ptr_to_table1->data, &ptr_to_table2->data, 0);
create_and_add_instruction(i_list, INST_POP, 0, 0, 0);
create_and_add_instruction(i_list, INST_WRITE, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_HALT, 0, 0, 0);
interpret(i_list, sym_tab);
dest_inst_list(&i_list);
printf("\n\n");
//TEST2
printf("####################TEST2#######################\n");
printf("8 - 5 = 3 <<<>>> 8 5 -\nOcakavany vystup : 3\n");
i_list = init_inst_list();
create_and_add_instruction(i_list, INST_PUSH_INT, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_a);
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_b);
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_EXPR_SUB, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_c);
ptr_to_table2 = stable_get_var(sym_tab, stack_top);
create_and_add_instruction(i_list, INST_STORE, &ptr_to_table1->data, &ptr_to_table2->data, 0);
create_and_add_instruction(i_list, INST_POP, 0, 0, 0);
create_and_add_instruction(i_list, INST_WRITE, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_HALT, 0, 0, 0);
interpret(i_list, sym_tab);
dest_inst_list(&i_list);
printf("\n\n");
//TEST3
printf("####################TEST3#######################\n");
printf("8 * 5 = 40 <<<>>> 8 5 *\nOcakavany vystup : 40\n");
i_list = init_inst_list();
create_and_add_instruction(i_list, INST_PUSH_INT, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_a);
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_b);
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_EXPR_MUL, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_c);
ptr_to_table2 = stable_get_var(sym_tab, stack_top);
create_and_add_instruction(i_list, INST_STORE, &ptr_to_table1->data, &ptr_to_table2->data, 0);
create_and_add_instruction(i_list, INST_POP, 0, 0, 0);
create_and_add_instruction(i_list, INST_WRITE, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_HALT, 0, 0, 0);
interpret(i_list, sym_tab);
dest_inst_list(&i_list);
printf("\n\n");
//TEST4
printf("####################TEST4#######################\n");
printf("8 / 5 = 1 <<<>>> 8 5 /\nOcakavany vystup : 1\n");
i_list = init_inst_list();
create_and_add_instruction(i_list, INST_PUSH_INT, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_a);
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_b);
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_EXPR_DIV, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_c);
ptr_to_table2 = stable_get_var(sym_tab, stack_top);
create_and_add_instruction(i_list, INST_STORE, &ptr_to_table1->data, &ptr_to_table2->data, 0);
create_and_add_instruction(i_list, INST_POP, 0, 0, 0);
create_and_add_instruction(i_list, INST_WRITE, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_HALT, 0, 0, 0);
interpret(i_list, sym_tab);
dest_inst_list(&i_list);
printf("\n\n");
//TEST5
printf("####################TEST5#######################\n");
printf("456 / 9 = 50 <<<>>> 456 9 /\nOcakavany vystup : 50\n");
i_list = init_inst_list();
create_and_add_instruction(i_list, INST_PUSH_INT, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_a);
ptr_to_table1->data.data.i = 456;
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_b);
ptr_to_table1->data.data.i = 9;
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_EXPR_DIV, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_c);
ptr_to_table2 = stable_get_var(sym_tab, stack_top);
create_and_add_instruction(i_list, INST_STORE, &ptr_to_table1->data, &ptr_to_table2->data, 0);
create_and_add_instruction(i_list, INST_POP, 0, 0, 0);
create_and_add_instruction(i_list, INST_WRITE, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_HALT, 0, 0, 0);
interpret(i_list, sym_tab);
dest_inst_list(&i_list);
printf("\n\n");
printf("#################### DOUBLE-ONLY ##############\n");
//TEST6
printf("####################TEST6#######################\n");
printf("123.456 + 56.333365 = 179,789365 <<<>>> 123.456 56.333365 +\nOcakavany vystup : 179,789365\n");
i_list = init_inst_list();
create_and_add_instruction(i_list, INST_PUSH_DOUBLE, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_a);
ptr_to_table1->data.arg_type = DOUBLE;
ptr_to_table1->data.data.d = 123.456;
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_b);
ptr_to_table1->data.arg_type = DOUBLE;
ptr_to_table1->data.data.d = 56.333365;
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_EXPR_ADD, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_c);
ptr_to_table2 = stable_get_var(sym_tab, stack_top);
create_and_add_instruction(i_list, INST_STORE, &ptr_to_table1->data, &ptr_to_table2->data, 0);
create_and_add_instruction(i_list, INST_POP, 0, 0, 0);
create_and_add_instruction(i_list, INST_WRITE, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_HALT, 0, 0, 0);
interpret(i_list, sym_tab);
dest_inst_list(&i_list);
printf("\n\n");
//TEST7
printf("####################TEST7#######################\n");
printf("123.456 - 56.333365 = 67,122635 <<<>>> 123.456 56.333365 -\nOcakavany vystup : 67,122635\n");
i_list = init_inst_list();
create_and_add_instruction(i_list, INST_PUSH_DOUBLE, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_a);
ptr_to_table1->data.arg_type = DOUBLE;
ptr_to_table1->data.data.d = 123.456;
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_b);
ptr_to_table1->data.arg_type = DOUBLE;
ptr_to_table1->data.data.d = 56.333365;
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_EXPR_SUB, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_c);
ptr_to_table2 = stable_get_var(sym_tab, stack_top);
create_and_add_instruction(i_list, INST_STORE, &ptr_to_table1->data, &ptr_to_table2->data, 0);
create_and_add_instruction(i_list, INST_POP, 0, 0, 0);
create_and_add_instruction(i_list, INST_WRITE, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_HALT, 0, 0, 0);
interpret(i_list, sym_tab);
dest_inst_list(&i_list);
printf("\n\n");
//TEST8
printf("####################TEST8#######################\n");
printf("123.456 * 56.333365 = 6954,69190944 <<<>>> 123.456 56.333365 *\nOcakavany vystup : 179,789365\n");
i_list = init_inst_list();
create_and_add_instruction(i_list, INST_PUSH_DOUBLE, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_a);
ptr_to_table1->data.arg_type = DOUBLE;
ptr_to_table1->data.data.d = 123.456;
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_b);
ptr_to_table1->data.arg_type = DOUBLE;
ptr_to_table1->data.data.d = 56.333365;
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_EXPR_MUL, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_c);
ptr_to_table2 = stable_get_var(sym_tab, stack_top);
create_and_add_instruction(i_list, INST_STORE, &ptr_to_table1->data, &ptr_to_table2->data, 0);
create_and_add_instruction(i_list, INST_POP, 0, 0, 0);
create_and_add_instruction(i_list, INST_WRITE, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_HALT, 0, 0, 0);
interpret(i_list, sym_tab);
dest_inst_list(&i_list);
printf("\n\n");
//TEST9
printf("####################TEST9#######################\n");
printf("123.456 / 56.333365 = 2,191525395 <<<>>> 123.456 56.333365 /\nOcakavany vystup : 179,789365\n");
i_list = init_inst_list();
create_and_add_instruction(i_list, INST_PUSH_DOUBLE, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_a);
ptr_to_table1->data.arg_type = DOUBLE;
ptr_to_table1->data.data.d = 123.456;
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_b);
ptr_to_table1->data.arg_type = DOUBLE;
ptr_to_table1->data.data.d = 56.333365;
create_and_add_instruction(i_list, INST_PUSH, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_EXPR_DIV, 0, 0, 0);
ptr_to_table1 = stable_get_var(sym_tab, lokal_c);
ptr_to_table2 = stable_get_var(sym_tab, stack_top);
create_and_add_instruction(i_list, INST_STORE, &ptr_to_table1->data, &ptr_to_table2->data, 0);
create_and_add_instruction(i_list, INST_POP, 0, 0, 0);
create_and_add_instruction(i_list, INST_WRITE, &ptr_to_table1->data, 0, 0);
create_and_add_instruction(i_list, INST_HALT, 0, 0, 0);
interpret(i_list, sym_tab);
dest_inst_list(&i_list);
printf("\n\n");
stable_destroy(&sym_tab);
return 0;
}