-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.c
331 lines (317 loc) · 10.6 KB
/
vm.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*****************************************************************************
*
* Copyright (C) 2014 - 2016 Lukas Singer
*
* This file is part of 'qpl'.
*
* 'qpl' is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 'qpl' is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 'qpl'. If not, see <http://www.gnu.org/licenses/>.
*
* The name 'qpl' stands for Quick Programming Language and is a working
* title. It may changes in the future.
*
* Source code can be found under: <https://github.com/linluk/project-qpl/>.
*
****************************************************************************/
/* lib */
#include <stdlib.h>
#include <stdio.h>
/* own */
#include "ast.h"
#include "ops.h"
#include "env.h"
#include "utils.h"
#include "error.h"
/* self */
#include "vm.h"
ast_t* eval_call(env_t* env, ast_t* ast) {
ast_t* func = NULL;
ast_t* result = NULL;
char* fn = NULL; /* function name */
switch(ast->data.call.call_type) {
case ct_anonymous:
fn = "<anonymous>";
func = ast->data.call.function.function;
break;
case ct_named:
fn = ast->data.call.function.id;
func = get_ast_by_id(env, fn);
if(func == NULL) {
error_id(NULL, fn);
};
break;
}
switch(func->type) {
case at_function:{
size_t i;
if(ast->data.call.callargs->data.callargs.count != func->data.function.params->data.params.count) {
error_paramcount(NULL, fn, func->data.function.params->data.params.count, ast->data.call.callargs->data.callargs.count);
}
env_t* inner = create_env();
inner->parent = env;
for(i = 0; i < func->data.function.params->data.params.count; i++) {
set_ast_to_id(
inner,
func->data.function.params->data.params.params[i],
eval_expression(env,ast->data.call.callargs->data.callargs.callargs[i])
);
}
/* execute the function */
exec_statements(inner, func->data.function.statements);
/* get the result */
inner->parent = NULL; /* must be NULL, get_ast_by_id() also searches the parent environment */
result = get_ast_by_id(inner, "@");
free_env(inner);
break;
}
case at_builtin:
if(ast->data.call.callargs->data.callargs.count != func->data.builtin.paramcount) {
error_paramcount(NULL, fn, func->data.function.params->data.params.count, ast->data.call.callargs->data.callargs.count);
}
switch(func->data.builtin.paramcount) {
case 0:
result = func->data.builtin.function.builtin_0();
break;
case 1: {
ast_t* p = eval_expression(env,ast->data.call.callargs->data.callargs.callargs[0]);
result = func->data.builtin.function.builtin_1(p);
dec_ref(p);
break;
}
case 2: {
ast_t* p1 = eval_expression(env,ast->data.call.callargs->data.callargs.callargs[0]);
ast_t* p2 = eval_expression(env,ast->data.call.callargs->data.callargs.callargs[1]);
result = func->data.builtin.function.builtin_2(p1,p2);
dec_ref(p1);
dec_ref(p2);
break;
}
case 3: {
ast_t* p1 = eval_expression(env, ast->data.call.callargs->data.callargs.callargs[0]);
ast_t* p2 = eval_expression(env, ast->data.call.callargs->data.callargs.callargs[1]);
ast_t* p3 = eval_expression(env, ast->data.call.callargs->data.callargs.callargs[2]);
result = func->data.builtin.function.builtin_3(p1,p2,p3);
dec_ref(p1);
dec_ref(p2);
dec_ref(p3);
break;
}
default:
printf("\n\n*** HINT TO DEVELOPER ***\nimplement builtincall in vm.c\n\n");
exit(1);
/* if you create a builtin function with more parameters then you have to add a case here */
break;
}
break;
default:
error_expected(NULL, get_ast_type_name(at_function), get_ast_type_name(func->type));
break;
}
return result;
}
ast_t* eval_expression(env_t* env, ast_t* ast) {
switch(ast->type) {
/* valid */
case at_call: return eval_call(env,ast);
case at_identifier: return get_ast_by_id(env, ast->data.id);
case at_expression: {
ast_t* result = NULL;
ast_t* left = eval_expression(env, ast->data.expression.left);
ast_t* right = eval_expression(env, ast->data.expression.right);
inc_ref(left);
inc_ref(right);
switch(ast->data.expression.op) {
case op_add: result = eval_add(env, left, right); break;
case op_mul: result = eval_mul(env, left, right); break;
case op_div: result = eval_div(env, left, right); break;
case op_sub: result = eval_sub(env, left, right); break;
case op_mod: result = eval_mod(env, left, right); break;
case op_and: result = eval_and(env, left, right); break;
case op_or: result = eval_or(env, left, right); break;
case op_gt: result = eval_gt(env, left, right); break;
case op_ge: result = eval_ge(env, left, right); break;
case op_lt: result = eval_lt(env, left, right); break;
case op_le: result = eval_le(env, left, right); break;
case op_eq: result = eval_eq(env, left, right); break;
case op_neq: result = eval_neq(env, left, right); break;
case op_cat: result = eval_cat(env, left, right); break;
case op_deref: {
ast_t* index = eval_expression(env, right);
if (index->type != at_integer) {
// TODO: error -> index must be an integer!
} else {
switch(left->type) {
case at_list: result = left->data.list.elements[index->data.i];
}
}
}
}
result->ref_count = 0;
dec_ref(left);
dec_ref(right);
return result;
}
/* no need to evaluate */
case at_integer:
case at_bool:
case at_double:
case at_string:
case at_function:
case at_statements:
case at_list:
return ast;
/* invalid */
case at_assignment:
case at_callargs:
case at_conditional:
case at_dowhile:
case at_elif:
case at_if:
case at_params:
case at_while:
case at_builtin:
error_expected(NULL,"expression",get_ast_type_name(ast->type));
}
return NULL; /* this should never happen */
}
int exec_if(env_t* env, ast_t* ast) {
int result = 0; /* not null when condition was true,
null when condition was false */
ast_t* cond = eval_expression(env, ast->data.if_statement.condition);
if(cond->type != at_bool) {
error_expected(NULL,get_ast_type_name(at_bool),get_ast_type_name(cond->type));
} else {
if(cond->data.b) {
exec_statements(env, ast->data.if_statement.statements);
result = 1;
}
dec_ref(cond);
}
return result;
}
void exec_conditional(env_t* env, ast_t* ast) {
/* if */
if(exec_if(env, ast->data.conditional.if_statement)) {
return;
} else {
if(ast->data.conditional.elif_statements != NULL) {
size_t i;
/* elif */
for(i = 0; i < ast->data.conditional.elif_statements->data.elif_statements.count; i++) {
if(exec_if(env, ast->data.conditional.elif_statements->data.elif_statements.elif_statements[i])) {
return;
}
}
}
if(ast->data.conditional.else_statement != NULL) {
/* else */
exec_statements(env, ast->data.conditional.else_statement);
}
}
}
void exec_while(env_t* env, ast_t* ast) {
ast_t* cond = eval_expression(env, ast->data.while_statement.condition);
if(cond->type != at_bool) {
error_expected(NULL,get_ast_type_name(at_bool),get_ast_type_name(cond->type));
} else {
while(cond->data.b) {
exec_statements(env, ast->data.while_statement.statements);
dec_ref(cond);
cond = eval_expression(env, ast->data.while_statement.condition);
}
dec_ref(cond);
}
}
void exec_dowhile(env_t* env, ast_t* ast) {
/* same as exec_while() but with a call of exec_statements() before. */
exec_statements(env, ast->data.while_statement.statements);
ast_t* cond = eval_expression(env, ast->data.while_statement.condition);
if(cond->type != at_bool) {
error_expected(NULL,get_ast_type_name(at_bool),get_ast_type_name(cond->type));
} else {
while(cond->data.b) {
exec_statements(env, ast->data.while_statement.statements);
dec_ref(cond);
cond = eval_expression(env, ast->data.while_statement.condition);
}
dec_ref(cond);
}
}
void exec_statements(env_t* env, ast_t* ast) {
size_t i;
for(i = 0; i < ast->data.statements.count; i++) {
switch(ast->data.statements.statements[i]->type) {
case at_assignment:
exec_assignment(env, ast->data.statements.statements[i]);
break;
case at_call: {
ast_t* tmp = eval_call(env, ast->data.statements.statements[i]);
if(tmp != NULL) {
if(tmp->ref_count == 0) {
free_ast(tmp);
}
}
break;
}
case at_conditional:
exec_conditional(env, ast->data.statements.statements[i]);
break;
case at_while:
exec_while(env, ast->data.statements.statements[i]);
break;
case at_dowhile:
exec_dowhile(env, ast->data.statements.statements[i]);
break;
default:
error_unexpected(NULL,get_ast_type_name(ast->data.statements.statements[i]->type));
break;
}
}
}
void exec_assignment(env_t* env, ast_t* ast) {
ast_t* right = NULL;
switch(ast->data.assignment.right->type) {
case at_function:
right = ast->data.assignment.right;
break;
case at_bool:
case at_double:
case at_integer:
case at_string:
case at_statements:
right = ast->data.assignment.right;
break;
case at_identifier:
right = get_ast_by_id(env, ast->data.assignment.right->data.id);
break;
case at_expression:
right = eval_expression(env, ast->data.assignment.right);
break;
case at_call:
right = eval_call(env, ast->data.assignment.right);
break;
default:
break;
}
if(right == NULL) {
error_assign(NULL,"NULL",ast->data.assignment.id);
}
set_ast_to_id(env, ast->data.assignment.id, right);
}
void vm_exec(env_t* env, ast_t* ast) {
if(ast->type == at_statements) {
exec_statements(env,ast);
} else {
// TODO : -----
}
}