|
|
@@ -169,8 +169,6 @@ void add_function_to_env(NODE *tree, ENV *env_ptr) {
|
|
169
|
169
|
NODE* func_name = tree->left->right->left->left;
|
|
170
|
170
|
TOKEN* name_token = (TOKEN*) func_name;
|
|
171
|
171
|
if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
|
|
172
|
|
- /* printf("Added function name %s to environment with value: \n", name_token->lexeme); */
|
|
173
|
|
- /* print_tree(tree); */
|
|
174
|
172
|
ENV* new_func_env = create_new_function_env(env_ptr);
|
|
175
|
173
|
if (tree->left->right->right != NULL) {
|
|
176
|
174
|
add_args_to_env(tree->left->right->right, new_func_env);
|
|
|
@@ -210,7 +208,7 @@ TOKEN** get_val_list(NODE *tree) {
|
|
210
|
208
|
void bind_arg(TOKEN* val, TOKEN* arg_name, ENV *env_ptr) {
|
|
211
|
209
|
BIND* existing = find_name_in_list(arg_name, env_ptr->bindings);
|
|
212
|
210
|
if (existing == NULL) {
|
|
213
|
|
- printf("fatal error\n");
|
|
|
211
|
+ printf("Attempted to bind arg that doesn't exist, the compiler is broken\n");
|
|
214
|
212
|
exit(1);
|
|
215
|
213
|
}
|
|
216
|
214
|
existing->tree = (NODE*) val;
|
|
|
@@ -224,14 +222,12 @@ void add_var_to_env(NODE *tree, ENV *env_ptr) {
|
|
224
|
222
|
tok->value = recursive_interpret(tree->right, env_ptr);
|
|
225
|
223
|
asprintf(&tok->lexeme, "%d", tok->value);
|
|
226
|
224
|
if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
|
|
227
|
|
- /* printf("Added variable name %s to environment with value: %s\n", name_token->lexeme, tok->lexeme); */
|
|
228
|
225
|
if (env_ptr->bindings == NULL) {
|
|
229
|
226
|
env_ptr->bindings = create_list(create_binding(name_token, (NODE*) tok, NULL), NULL);
|
|
230
|
227
|
} else {
|
|
231
|
228
|
append_list(env_ptr->bindings, create_binding(name_token, (NODE*) tok, NULL));
|
|
232
|
229
|
}
|
|
233
|
230
|
} else {
|
|
234
|
|
- /* printf("Updating variable name %s with value: %s\n", name_token->lexeme, tok->lexeme); */
|
|
235
|
231
|
existing->tree = (NODE *) tok;
|
|
236
|
232
|
}
|
|
237
|
233
|
}
|
|
|
@@ -282,10 +278,10 @@ int recursive_interpret(NODE *tree, ENV *env_ptr) {
|
|
282
|
278
|
exit(1);
|
|
283
|
279
|
}
|
|
284
|
280
|
if (tree->right != NULL) {
|
|
285
|
|
- TOKEN** arg_list;
|
|
286
|
|
- TOKEN** val_list;
|
|
287
|
|
- int exp_args = count_args(func->tree->left->right->right);
|
|
288
|
|
- int num_args = count_args(tree->right);
|
|
|
281
|
+ TOKEN **arg_list, **val_list;
|
|
|
282
|
+ int exp_args, num_args;
|
|
|
283
|
+ exp_args = count_args(func->tree->left->right->right);
|
|
|
284
|
+ num_args = count_args(tree->right);
|
|
289
|
285
|
arg_list = get_argument_list(func->tree);
|
|
290
|
286
|
val_list = get_val_list(tree->right);
|
|
291
|
287
|
if (exp_args == num_args) {
|
|
|
@@ -293,7 +289,7 @@ int recursive_interpret(NODE *tree, ENV *env_ptr) {
|
|
293
|
289
|
bind_arg(val_list[i], arg_list[i], func->env);
|
|
294
|
290
|
}
|
|
295
|
291
|
} else {
|
|
296
|
|
- printf("Error! Arguments not right!!\n");
|
|
|
292
|
+ printf("Incorrect arguments passed to function %s, expected %d got %d\n", func_name->lexeme, exp_args, num_args);
|
|
297
|
293
|
exit(1);
|
|
298
|
294
|
}
|
|
299
|
295
|
}
|
|
|
@@ -301,14 +297,16 @@ int recursive_interpret(NODE *tree, ENV *env_ptr) {
|
|
301
|
297
|
}
|
|
302
|
298
|
if (tree->type == IF) {
|
|
303
|
299
|
if (recursive_interpret(tree->left, env_ptr) == 1) {
|
|
|
300
|
+ ENV* scope_env = create_new_function_env(env_ptr);
|
|
304
|
301
|
if (tree->right->type == ELSE) {
|
|
305
|
|
- return recursive_interpret(tree->right->left, env_ptr);
|
|
|
302
|
+ return recursive_interpret(tree->right->left, scope_env);
|
|
306
|
303
|
} else {
|
|
307
|
|
- return recursive_interpret(tree->right, env_ptr);
|
|
|
304
|
+ return recursive_interpret(tree->right, scope_env);
|
|
308
|
305
|
}
|
|
309
|
306
|
} else {
|
|
310
|
307
|
if (tree->right->type == ELSE) {
|
|
311
|
|
- return recursive_interpret(tree->right->right, env_ptr);
|
|
|
308
|
+ ENV* scope_env = create_new_function_env(env_ptr);
|
|
|
309
|
+ return recursive_interpret(tree->right->right, scope_env);
|
|
312
|
310
|
}
|
|
313
|
311
|
return 0;
|
|
314
|
312
|
}
|
|
|
@@ -358,8 +356,9 @@ int recursive_interpret(NODE *tree, ENV *env_ptr) {
|
|
358
|
356
|
return recursive_interpret(tree->left, env_ptr);
|
|
359
|
357
|
}
|
|
360
|
358
|
if (tree->type == WHILE) {
|
|
|
359
|
+ ENV* scope_env = create_new_function_env(env_ptr);
|
|
361
|
360
|
while (recursive_interpret(tree->left, env_ptr)) {
|
|
362
|
|
- recursive_interpret(tree->right, env_ptr);
|
|
|
361
|
+ recursive_interpret(tree->right, scope_env);
|
|
363
|
362
|
}
|
|
364
|
363
|
return 0;
|
|
365
|
364
|
}
|