-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterp.c
201 lines (163 loc) · 5.34 KB
/
interp.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
/**
* a short explanation of the abstract syntax tree used mojo :
* in short, the code is parsed and is transformed in a bunch of
* s-expressions.
*
* For instance, the following code :
* MyClass :setValue o :setPosx 3 becomes :
* (MyClass
* (:setValue o
* (: setPosx 3)))
*/
#include "object.h"
#include "interp.h"
#include "tokens.h"
#include "list.h"
#include "error.h"
extern int yylex();
extern int yyleng;
extern FILE *yyin;
extern char *yytext;
int compile_image()
{
while(!feof(yyin)) {
struct Object* ast = create_list_object(); // clone_object(list_object);
struct Object* scope = create_list_object(); //clone_object(list_object);
expression(ast);
display_ast(ast, 0);
execute_branch(ast, scope);
free_object(ast);
}
return 0;
}
/*
void unput_token(void) {
int i;
for (i = yyleng - 1; i >= 0; --i )
unput(yytext[i]);
}
*/
int expression(struct Object *ast) {
int r = yylex();
struct Object *obj;
struct Object *params;
switch(r) {
case NUMBER:
obj = clone_object(number_object);
obj->value.i_value = atoi(yytext);
params = clone_object(list_object);
list_append(params, obj);
list_append(ast, params);
return message(params);
break;
case SYMBOL:
obj = clone_object(base_object);
obj->type = T_SYMBOL;
obj->name = strdup(yytext);
params = clone_object(list_object);
list_append(params, obj);
list_append(ast, params);
return message(params);
break;
default:
fail("Unexpected : %s", yytext);
return -1;
break;
}
}
int message(struct Object *ast) {
int r = yylex();
struct Object *obj;
if (r == SYMBOL) {
obj = clone_object(base_object);
obj->type = T_MESSAGE;
obj->name = strdup(yytext);
list_append(ast, obj);
expression(ast);
} else if (r == SEMICOLON) {
return 0;
} else {
fail("Expected symbol, got : %s", yytext);
return -1;
}
return -1;
}
void display_ast(struct Object *ast, int nindents) {
if (ast == NULL)
return;
if (ast->type != T_LIST) {
display_object(ast, nindents);
return;
}
int length = mojo_list_length(ast->value.l_value);
int i = 0;
for(i = 0; i < length; i++) {
struct Object *o = list_nth(ast, i);
display_object(o, nindents);
}
}
struct Object* execute_branch(struct Object *ast, struct Object *scope) {
if (ast == NULL || ast->type != T_LIST)
return NULL;
struct Object* stack = clone_object(list_object);
int length = mojo_list_length(ast->value.l_value);
if (length == 0)
return NULL;
// first, execute all the sub-expressions
int i = 0;
for (i = 0; i < length; i++) {
struct Object *obj = (struct Object*) list_nth(ast, i);
if(obj == NULL)
continue;
if(obj->type == T_LIST) {
list_append(stack, execute_branch(obj, scope));
} else {
list_append(stack, obj);
}
}
// now execute the expression itself
length = mojo_list_length(ast->value.l_value);
if (length == 0) {
return nil_object;
} else if (length == 1) {
return list_nth(stack, 1);
} else if (length > 1) {
struct Object *destObject = list_nth(stack, 0);
struct Object *methodName = list_nth(stack, 1);
if(destObject->type == T_SYMBOL && strcmp(methodName->name, ":=") == 0) {
// this is an assignment
puts("assignment");
struct Object *value = list_nth(stack, 2);
struct Object* var = clone_object(base_object);
var->type = T_SYMBOL;
var->name = strdup(destObject->name);
var->value.v_value = clone_object(value);
list_append(scope->symtab, var);
} else {
// this is a regular method call
struct Object *method = (struct Object*) lookup_method(destObject, methodName->name);
if(method == nil_object) {
fail("error : method %s not found in object %s", methodName->name, destObject->name);
}
/* we use the fact that if list_nth doesn't find an
* object because it doesn't exists at the index, it returns nil
*/
struct Object *param1 = list_nth(stack, 2),
*param2 = list_nth(stack, 3),
*param3 = list_nth(stack, 4),
*param4 = list_nth(stack, 5);
if (param1->type == T_SYMBOL) {
struct Object *o = lookup_variable(scope, param1->name);
if (o != nil_object)
param1 = o;
}
destObject = method->value.c_method(destObject, param1, param2, param3, param4);
}
puts("freeing stuff");
display_object(stack, 0);
printf(">>> stack len : %d addr : %x\n", mojo_list_length(stack->value.l_value), stack->value.l_value);
free_object(stack);
return destObject;
}
return nil_object;
}