-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpseudo_code.c
244 lines (236 loc) · 8.06 KB
/
pseudo_code.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "pseudo_code.h"
#include "pseudo_code.tab.h"
#define STACKSIZE 64000
void *stack;
void *sp;
void *fp;
FnList *functions;
#define BUFSIZE 1000
char buf[BUFSIZE];
void FncList(Node *p);
Function *FindFunction(char *s){
int i;
for(i = 0; i < functions->fn_count; i++)
if(strcasecmp(functions->functions[i]->name, s) == 0)
return functions->functions[i];
return NULL;
}
int Add(int *a, int *b) { return *a + *b; }
int Sub(int *a, int *b) { return *a - *b; }
int Mul(int *a, int *b) { return (*a) * (*b); }
int Div(int *a, int *b) { return (*a) / (*b); }
int leq(int *a, int *b) { return (*a) <= (*b); }
int geq(int *a, int *b) { return (*a) >= (*b); }
int eq(int *a, int *b) { return (*a) == (*b); }
int neq(int *a, int *b) { return (*a) != (*b); }
int les(int *a, int *b) { return (*a) < (*b); }
int gre(int *a, int *b) { return (*a) > (*b); }
void Op2(OpNode *op, int (*fn)(int *a, int *b))
{
FncList(op->operands[0]);
sp = sp + sizeof(int);
FncList(op->operands[1]);
sp = sp - sizeof(int);
*(int*)sp = fn(sp, sp + sizeof(int));
}
void ExecuteFunction(Function *fn){
*(void**)sp = fp;//store reference to previous fncalls variables
sp = sp + sizeof(void*);//move sp forward to a free spot
fp = sp;//assign new functions variable space from this location
sp = sp + fn->var_count * sizeof(int);//move sp forward to space after fn variables
FncList(fn->body);
int retvalue = *(int*)fp;
sp = (void*)fp - sizeof(void*);//restore sp to old value
fp = *(void**)sp;//restore fp
*(int*)sp = retvalue;//put fn return value at top of stack
}
void FncList(Node *p){
if(p == NULL)
return;
int i;
int j;
int arr;
int arr2;
int arr1;
Function *tmp;
switch(p->type){
case tConst:
*(int*)sp = p->con.value;
break;
case tVar:
//printf("%s %p\n", p->var->name, fp + p->var->index * sizeof(int));
*(int*)sp = *(int*)(fp + p->var->index * sizeof(int));
break;
case tString:
*(char**)sp = p->str.s;
case tOp:
switch(p->op.type){
case '+':
Op2(&p->op, Add);
break;
case '-':
Op2(&p->op, Sub);
break;
case '*':
Op2(&p->op, Mul);
break;
case '/':
Op2(&p->op, Div);
break;
case '>':
Op2(&p->op, gre);
break;
case '<':
Op2(&p->op, les);
break;
case EQ:
Op2(&p->op, eq);
break;
case NEQ:
Op2(&p->op, neq);
break;
case GEQ:
Op2(&p->op, geq);
break;
case LEQ:
Op2(&p->op, leq);
break;
case '=':
FncList(p->op.operands[1]);//execute right half and have it stored at sp
if(p->op.operands[0]->type == tVar) {
*(int*)(fp + p->op.operands[0]->var->index * sizeof(int)) = *(int*)sp;
} else {
arr = *(int*)sp;
FncList(p->op.operands[0]->op.operands[1]);
// printf("%i\n", *(int*)(fp + (p->op.operands[0]->op.operands[0]->var->index + *(int*)sp) * sizeof(int)));
*(int*)(fp + (p->op.operands[0]->op.operands[0]->var->index + *(int*)sp) * sizeof(int)) = arr;
}
break;
case NOT:
FncList(p->op.operands[0]);
*(int*)sp = !*(int*)sp;
break;
case PRINT:
FncList(p->op.operands[0]);
if(p->op.operands[0]->type == tString)
printf("%s\n", *(char**)sp);
else
printf("%d\n", *(int*)sp);
break;
case SWAP:
if(p->op.operands[0]->type == tVar && p->op.operands[1]->type == tVar) {
i = p->op.operands[0]->var->index;
p->op.operands[0]->var->index = p->op.operands[1]->var->index;
p->op.operands[1]->var->index = i;
} else {
if (p->op.operands[0]->type != tVar) {
FncList(p->op.operands[0]->op.operands[1]);
arr = *(int*)sp;
if(p->op.operands[1]->type == tVar) {
FncList(p->op.operands[1]);
arr1 = *(int*)sp;
*(int*)(fp +p->op.operands[1]->var->index * sizeof(int)) =
*(int*)(fp + (p->op.operands[0]->op.operands[0]->var->index + arr) * sizeof(int));
*(int*)(fp + (p->op.operands[0]->op.operands[0]->var->index + arr) * sizeof(int)) = arr1;
} else {
FncList(p->op.operands[1]->op.operands[1]);
arr1 = *(int*)sp;
arr2 = *(int*)(fp + (p->op.operands[1]->op.operands[0]->var->index + arr1) * sizeof(int));
*(int*)(fp + (p->op.operands[1]->op.operands[0]->var->index + arr1) * sizeof(int)) =
*(int*)(fp + (p->op.operands[0]->op.operands[0]->var->index + arr) * sizeof(int));
*(int*)(fp + (p->op.operands[0]->op.operands[0]->var->index + arr) * sizeof(int)) = arr2;
}
} else {
FncList(p->op.operands[1]->op.operands[1]);
arr = *(int*)sp;
FncList(p->op.operands[0]);
*(int*)(fp + p->op.operands[0]->var->index * sizeof(int)) =
*(int*)(fp + (p->op.operands[1]->op.operands[0]->var->index + arr) * sizeof(int));
*(int*)(fp + (p->op.operands[1]->op.operands[0]->var->index + arr) * sizeof(int)) = *(int*)sp;
}
}
break;
case LEN:
*(int*)sp = p->op.operands[0]->var->len;
break;
case ARRAY:
FncList(p->op.operands[1]);
*(int*)sp = *(int*)(fp + (p->op.operands[0]->var->index + *(int*)sp) * sizeof(int));
break;
case READ:
fgets(buf, BUFSIZE, stdin);
*(int*)(fp + p->op.operands[0]->var->index * sizeof(int)) = atoi(buf);
break;
case IF:
FncList(p->op.operands[0]);
if( *(int*)sp )
FncList(p->op.operands[1]);
break;
case ELSE:
FncList(p->op.operands[0]);
if( *(int*)sp )
FncList(p->op.operands[1]);
else
FncList(p->op.operands[2]);
break;
case FOR:
FncList(p->op.operands[2]);
j = *(int*)sp;
FncList(p->op.operands[1]);
for(*(int*)(fp + p->op.operands[0]->var->index * sizeof(int)) = *(int*)sp;
*(int*)(fp + p->op.operands[0]->var->index * sizeof(int)) <= j;
++(*(int*)(fp + p->op.operands[0]->var->index * sizeof(int)))) {
FncList(p->op.operands[3]);
}
break;
case FUNC:
tmp = FindFunction(p->op.operands[0]->var->name);
if(tmp == NULL || tmp->param_count != p->op.operands[1]->par.n){
printf("couldn't find function %s (%d)\n", p->op.operands[0]->var->name,
p->op.operands[1]->par.n);
exit(0);
break;
}else{
sp = sp + sizeof(void*);
for(i = 0; i < p->op.operands[1]->par.n; i++){
sp = sp + sizeof(int);
FncList(p->op.operands[1]->par.params[i]);
}
sp = sp - sizeof(void*) - p->op.operands[1]->par.n * sizeof(int);
ExecuteFunction(tmp);
}
}
break;
case tBlock:
for(i = 0; i < p->block.n; i++)
FncList(p->block.statements[i]);
break;
default:
break;
}
}
int main(int argc, char **argv){
if(argc < 2){
printf("No input files!\n");
return 0;
}
int i;
functions = NULL;
for(i = 1; i < argc; i++){
functions = ReadFunctions(argv[i], functions);
}
Function *main_fn = FindFunction("MAIN");
if(main_fn == NULL){
printf("No main function!\n");
}else{
stack = malloc(STACKSIZE);
sp = stack;
fp = NULL;
ExecuteFunction(main_fn);
}
FreeFunctions(functions);
return 0;
}