-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.c
264 lines (229 loc) · 5.9 KB
/
eval.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
#include "jscheme.h"
#include <stdlib.h>
#define INITIAL_STACK_SIZE 128
OBJ *jStack;
int SP = 0;
int AP = 0;
int BP = 0;
OBJ RETVAL;
int stackLimit;
void
initJStack(){
stackLimit = INITIAL_STACK_SIZE;
jStack = (OBJ *)(malloc( sizeof(OBJ) * INITIAL_STACK_SIZE));
SP = 0;
AP = 0;
BP = 0;
}
static OBJ
evalCons(OBJ env, OBJ expr){
OBJ functionSlot = CAR(expr);
OBJ evaluatedFunctionSlot = js_eval(env, functionSlot);
OBJ argList = CDR(expr);
OBJ restArgs;
switch( TAG(evaluatedFunctionSlot) ){
case T_BUILTINFUNCTION:
{
#ifdef DEBUG
// PRINT TRACE
if( EVAL_TRACE->state){
printIndent(indentLevel);
fprintf(stdout, MAG "ARGS FOR BUILTINFUNC\n" RESET);
}
#endif
int numArgs = 0;
restArgs = argList;
while( restArgs != js_nil) {
OBJ theArg = CAR(restArgs);
OBJ evaluatedArg;
restArgs = CDR(restArgs);
evaluatedArg = js_eval(env, theArg);
PUSH(evaluatedArg);
numArgs++;
}
return evaluatedFunctionSlot->u.builtinFunction.theCode(numArgs);
}
case T_BUILTINSYNTAX:
{
return evaluatedFunctionSlot->u.builtinSyntax.theCode(env, argList);
}
case T_USERDEFINEDFUNCTION:
{
// 1. check if the udfs expected numArgs is equal to the given args
// 2. put the given args in a new env and bind them to the expected formal args
// 3. eval every body in bodylist in the new env
// 4. return last bodies value
//
// given:
// evaluatedFunctionslot -> theUDF
// argList -> the non formal args passed to the udf
// this is can be an overhead since we dont expect that case to happen often
// -> refactor to checking args number while building env
int numFormalArgs = evaluatedFunctionSlot->u.userDefinedFunction.numArgs;
int numArgs = length(argList);
int numLocals = evaluatedFunctionSlot->u.userDefinedFunction.numLocals;
OBJ home = evaluatedFunctionSlot->u.userDefinedFunction.home;
if( numFormalArgs != numArgs){
js_function_error("(lambda): function expects %d args given %d", evaluatedFunctionSlot, numFormalArgs, numArgs);
}
// fill new environment
OBJ newEnv = newEnvironment((numFormalArgs + numLocals), home);
OBJ restFormalArgs = evaluatedFunctionSlot->u.userDefinedFunction.argList;
restArgs = argList;
//int slotIndex = 0;
#ifdef DEBUG
// PRINT TRACE
if( EVAL_TRACE->state){
printIndent(indentLevel);
fprintf(stdout, RED"EVAL UDF"RESET);
fprintf(stdout, "\n");
printIndent(indentLevel);
fprintf(stdout, MAG "PREPARE NEW ENV" YEL " (%p)" RESET, newEnv);
fprintf(stdout, " numFArgs: %d numLocals: %d\n", numFormalArgs, numLocals);
}
#endif
while( restFormalArgs != js_nil){
// expected formal arguments
OBJ nextFormalArg = CAR(restFormalArgs);
restFormalArgs = CDR(restFormalArgs);
// given arguments
OBJ unevaluatedArg = CAR(restArgs);
restArgs = CDR(restArgs);
#ifdef DEBUG
// PRINT TRACE
if( EVAL_TRACE->state){
printIndent(indentLevel);
fprintf(stdout, "binding " GRN); js_print(stdout, nextFormalArg,1);
fprintf(stdout, RESET " to -> ");
oldIndentLevel = indentLevel;
indentLevel = 0;
if( ISCONS(unevaluatedArg)) PAUSE_INDENT_FLAG = 1;
}
#endif
// put evaluated arg in new env
OBJ evaluatedArg = js_eval(env, unevaluatedArg);
environmentPut(newEnv, nextFormalArg, evaluatedArg);
//newEnv->u.environment.slots[slotIndex].key = nextFormalArg;
//newEnv->u.environment.slots[slotIndex].value = evaluatedArg;
//slotIndex++;
#ifdef DEBUG
// PRINT TRACE
if( EVAL_TRACE->state){
indentLevel = oldIndentLevel;
oldIndentLevel = 0;
}
#endif
}
// eval bodylist
OBJ restBodyList = evaluatedFunctionSlot->u.userDefinedFunction.bodyList;
#ifdef DEBUG
int numBodies = length(restBodyList);
int currentBody = 0;
#endif
OBJ lastValue;
while( restBodyList != js_nil) {
// bodies to evaluate
OBJ nextBody = CAR(restBodyList);
restBodyList = CDR(restBodyList);
#ifdef DEBUG
// PRINT TRACE
if( EVAL_TRACE->state){
fprintf(stdout, "\n");
printIndent(indentLevel);
fprintf(stdout, MAG "EVAL BODY %d of %d\n" RESET, ++currentBody, numBodies);
}
#endif
lastValue = js_eval(newEnv, nextBody);
}
return lastValue;
}
default:
js_error("Eval: not a function: ", evaluatedFunctionSlot);
}
// NOT REACHED
return js_nil;
}
OBJ
js_eval(OBJ env, OBJ expr){
#ifdef DEBUG
// PRINT TRACE
if( EVAL_TRACE->state){
printIndent(indentLevel);
if( TAG(env) == T_GLOBALENVIRONMENT){
printf("eval in " CYN "GLOBAL" RESET " (%p): ", env);
}else{
printf("eval in " YEL "LOCAL" RESET " (%p): ", env);
}
js_print(stdout, expr,1);
if(PAUSE_INDENT_FLAG){
indentLevel += oldIndentLevel;
PAUSE_INDENT_FLAG = 0;
}
}
#endif
OBJ value;
switch(TAG(expr)){
default:
// T_INTEGER
// T_NIL
// T_TRUE
// T_FALSE
// T_STRING
// T_FILESTREAM
// T_STRINGSTREAM
#ifdef DEBUG
// PRINT TRACE
if( EVAL_TRACE->state){
fprintf(stdout, " -> " GRN);
js_print(stdout, expr,1);
fprintf(stdout,"\n" RESET);
}
#endif
return expr;
case T_SYMBOL:
//printf("Address of symbol: %p\n", exp
value = environmentGet(env, expr);
if(value == NULL){
js_error("undefined variable", expr);
}
#ifdef DEBUG
// PRINT TRACE
if( EVAL_TRACE->state){
fprintf(stdout, " -> " GRN);
js_print(stdout, value,1);
fprintf(stdout, "\n" RESET);
}
#endif
return value;
case T_CONS:
#ifdef DEBUG
// PRINT TRACE
if( EVAL_TRACE->state){
indentLevel++;
fprintf(stdout,"\n");
if( (TAG(env) == T_LOCALENVIRONMENT) &&
(!ISCONS( CAR(expr))) &&
(!isDefine(expr)) &&
(!isLambda(expr))
)
{
fprintf(stdout,"\n");
printIndent(indentLevel);
fprintf(stdout,"ARGS in");
printLocalEnv(env);
}
}
value = evalCons(env, expr);
if( EVAL_TRACE->state){
printIndent(indentLevel);
fprintf(stdout, "<< ");
js_print(stdout, value,1);
fprintf(stdout, "\n");
indentLevel--;
}
#else
value = evalCons(env, expr);
#endif
return value;
}
}