-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.c
280 lines (235 loc) · 6.78 KB
/
interpreter.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
271
272
273
274
275
276
277
278
#include <stdio.h>
#include <glib.h>
#include <math.h>
#include <time.h>
#include "interpreter.h"
#include "builtins.h"
GMainLoop *loop = NULL;
static void quit_interpreter() {
printf("Quitting main loop\n");
g_main_loop_quit(loop);
g_main_loop_unref(loop);
printf("\n\nProgram finished\n\n");
}
void init_interpreter() {
printf("Interpreter starting\n\n");
loop = g_main_loop_new(NULL, FALSE);
}
void start_interpreter() {
printf("\nStarting main loop\n");
g_main_loop_run(loop);
}
Variable null = {.type=Null,.i=0};
extern Variable evaluate(MapObject * ,const char *);
Variable get_argument_value(Context *c, int argix) {
Variable v = c->program[c->current_line][argix];
switch(v.type) {
case Eval:
return evaluate(c->locals, v.s);
case Var:
return map_get(c->locals, v.s);
// case KeyWord:
// if(v.i == Call) {
// return (Variable){.type=WaitingObject, .i=0};
// }
}
return v;
}
Context *new_context() {
Context *c = malloc(sizeof(Context));
c->status = malloc(sizeof(int));
*c->status = Running;
c->locals = new_map();
c->current_line = 0;
c->subcontext = c->supercontext = NULL;
return c;
}
Context *new_subcontext(Context *supercontext, Program p) {
Context *c = malloc(sizeof(Context));
c->status = supercontext->status;
c->supercontext = supercontext;
c->program = p;
c->current_line = 0;
supercontext->subcontext = c;
c->locals = new_map();
c->locals->parent = supercontext->locals;
c->subcontext = NULL;
return c;
}
static inline void run_codeblock(Context *c, void *codeblock) {
*c->status = Waiting;
g_timeout_add_once(0, run_context, new_subcontext(c, codeblock));
}
void free_context(Context *c) {
if(c->supercontext) {
c->supercontext->subcontext = NULL;
}
else {
free(c->status);
}
if(c->subcontext) {
free_context(c->subcontext);
}
map_free(c->locals);
free(c);
}
void if_statement(Context *c) {
Variable *args = c->program[c->current_line];
Variable condition = get_argument_value(c, 1);
int is_true = 1;
switch(condition.type) {
case Integer:
is_true = condition.i != 0;
break;
case Float:
is_true = condition.f != 0.0;
break;
case Null:
is_true = 0;
break;
default:
fprintf(stderr, " unimplemented type in if check ");
exit(1);
}
if(is_true) {
run_codeblock(c, args[2].p);
}
}
void while_statement(Context *c) {
Variable *args = c->program[c->current_line];
Variable condition = get_argument_value(c, 1);
int is_true = 1;
switch(condition.type) {
case Integer:
is_true = condition.i != 0;
break;
case Float:
is_true = condition.f != 0.0;
break;
case Null:
is_true = 0;
break;
default:
fprintf(stderr, " unimplemented type in while check ");
exit(1);
}
if(is_true) {
c->current_line--;
run_codeblock(c, args[2].p);
}
}
struct ret {
Context *caller;
Context *callee;
};
void set_return(void *d) {
struct ret *r = d;
Variable *retadr = r->caller->custom_data;
r->caller->custom_data = NULL;
Variable retval = *((Variable *) r->callee->custom_data);
r->callee->custom_data = NULL;
*retadr = retval;
Context *c = r->caller;
free(r);
g_timeout_add_once(0, run_context, c);
}
int running_programs = 0;
void program_call(Context *c, void *program, Variable *return_address) {
*c->status = Waiting;
c->custom_data = return_address;
Context *newctx = new_context();
newctx->program = program;
newctx->final_callback = set_return;
newctx->data = malloc(sizeof(struct ret));
*((struct ret *)newctx->data) = (struct ret){.caller=c, .callee=newctx};
running_programs++;
g_timeout_add_once(0, run_context, newctx);
}
void set_key(Context *c) {
Variable *args = c->program[c->current_line];
Variable var_name = args[1];
Variable value = get_argument_value(c, 2);
if(value.type == ProgramPointer) {
Variable *return_address = map_set(c->locals, var_name.s, null);
program_call(c, value.p, return_address);
}
else {
map_set(c->locals, var_name.s, value);
}
}
void (*KeywordMap[])(Context *) = {
[IfKey] = if_statement,
[WhileKey] = while_statement,
[SetKey] = set_key
};
static inline Context *collapse_subcontexts(Context *c) {
Context *cur = c, *sup;
while((sup = cur->supercontext) != NULL) {
free_context(cur);
cur = sup;
}
return cur;
}
static inline void finish_context(Context *c) {
c->final_callback(c->data);
free_context(c);
if(--running_programs == 0) {
quit_interpreter();
}
}
void run_context(void *d) {
Context *c = (Context *)d;
switch(*c->status) {
case Waiting:
*c->status = Running;
break;
case Ended:
finish_context(collapse_subcontexts(c));
return;
}
while(1) {
Variable *line = c->program[c->current_line];
Variable arg0 = line[0];
switch(arg0.type) {
case End:
switch(arg0.i) {
case program_end:
Variable retval = get_argument_value(c, 1);
Context *root = collapse_subcontexts(c);
root->custom_data = &retval;
finish_context(root);
return;
case codeblock_end:
if(c->supercontext == NULL) {
fprintf(stderr, " codeblock end without supercontext ???");
exit(1);
}
Context *supercontext = c->supercontext;
free_context(c);
g_timeout_add_once(0, run_context, supercontext);
return;
default:
fprintf(stderr, " invalid End symbol");
exit(1);
}
case Builtin:
arg0.pf(c);
break;
case KeyWord:
KeywordMap[arg0.i](c);
break;
}
c->current_line++;
if(*c->status) {
return;
}
}
}
void start_program(Program program, CallbackFunctionPointer final_callback, void *data) {
Context *c = new_context();
c->program = program;
c->final_callback = final_callback;
c->data = data;
running_programs++;
run_context(c);
}