|
|
@@ -8,6 +8,7 @@
|
|
8
|
8
|
#include "list.h"
|
|
9
|
9
|
|
|
10
|
10
|
TOKEN* lookup_token(char *s);
|
|
|
11
|
+int asprintf(char **strp, const char *fmt, ...);
|
|
11
|
12
|
|
|
12
|
13
|
/*
|
|
13
|
14
|
* Existing problems/todo:
|
|
|
@@ -111,19 +112,19 @@ void add_function_to_env(NODE *tree, ENV *env_ptr) {
|
|
111
|
112
|
BIND* existing;
|
|
112
|
113
|
NODE* func_name = tree->left->right->left->left;
|
|
113
|
114
|
TOKEN* name_token = (TOKEN*) func_name;
|
|
|
115
|
+ if (tree->left->right->right == NULL) {
|
|
|
116
|
+ printf("%s has no arguments\n", name_token->lexeme);
|
|
|
117
|
+ }
|
|
114
|
118
|
if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
|
|
115
|
|
- printf("Added function name %s to environment with value: \n", name_token->lexeme);
|
|
116
|
|
- print_tree(tree);
|
|
|
119
|
+ /* printf("Added function name %s to environment with value: \n", name_token->lexeme); */
|
|
|
120
|
+ /* print_tree(tree); */
|
|
117
|
121
|
if (env_ptr->bindings == NULL) {
|
|
118
|
122
|
env_ptr->bindings = create_list(create_binding(name_token, tree, env_ptr), NULL);
|
|
119
|
123
|
} else {
|
|
120
|
124
|
append_list(env_ptr->bindings, create_binding(name_token, tree, env_ptr));
|
|
121
|
125
|
}
|
|
122
|
126
|
} else {
|
|
123
|
|
- printf("Updating function name %s with value: \n", name_token->lexeme);
|
|
124
|
|
- print_tree(tree);
|
|
125
|
|
- existing->tree = tree;
|
|
126
|
|
- existing->env = env_ptr;
|
|
|
127
|
+ printf("Error: redefinition of function with name %s\n", name_token->lexeme);
|
|
127
|
128
|
}
|
|
128
|
129
|
}
|
|
129
|
130
|
|
|
|
@@ -136,14 +137,14 @@ void add_var_to_env(NODE *tree, ENV *env_ptr) {
|
|
136
|
137
|
tok->value = recursive_interpret(tree->right, env_ptr);
|
|
137
|
138
|
asprintf(&tok->lexeme, "%d", tok->value);
|
|
138
|
139
|
if ((existing = find_name_in_env(name_token, env_ptr)) == NULL) {
|
|
139
|
|
- printf("Added variable name %s to environment with value: %s\n", name_token->lexeme, tok->lexeme);
|
|
|
140
|
+ /* printf("Added variable name %s to environment with value: %s\n", name_token->lexeme, tok->lexeme); */
|
|
140
|
141
|
if (env_ptr->bindings == NULL) {
|
|
141
|
142
|
env_ptr->bindings = create_list(create_binding(name_token, (NODE*) tok, NULL), NULL);
|
|
142
|
143
|
} else {
|
|
143
|
144
|
append_list(env_ptr->bindings, create_binding(name_token, (NODE*) tok, NULL));
|
|
144
|
145
|
}
|
|
145
|
146
|
} else {
|
|
146
|
|
- printf("Updating variable name %s with value: %s\n", name_token->lexeme, tok->lexeme);
|
|
|
147
|
+ /* printf("Updating variable name %s with value: %s\n", name_token->lexeme, tok->lexeme); */
|
|
147
|
148
|
existing->tree = (NODE *) tok;
|
|
148
|
149
|
}
|
|
149
|
150
|
}
|
|
|
@@ -157,7 +158,7 @@ int recursive_interpret(NODE *tree, ENV *env_ptr) {
|
|
157
|
158
|
printf("Not implemented\n");
|
|
158
|
159
|
exit(1);
|
|
159
|
160
|
} else if (tree->left->type == INT || tree->left->type == FUNCTION) {
|
|
160
|
|
- // do nothing we dont care about types
|
|
|
161
|
+ // do nothing we dont care about types for now
|
|
161
|
162
|
return 0;
|
|
162
|
163
|
} else {
|
|
163
|
164
|
// an identifier
|
|
|
@@ -253,6 +254,12 @@ int recursive_interpret(NODE *tree, ENV *env_ptr) {
|
|
253
|
254
|
if (tree->type == RETURN) {
|
|
254
|
255
|
return recursive_interpret(tree->left, env_ptr);
|
|
255
|
256
|
}
|
|
|
257
|
+ if (tree->type == WHILE) {
|
|
|
258
|
+ while (recursive_interpret(tree->left, env_ptr)) {
|
|
|
259
|
+ recursive_interpret(tree->right, env_ptr);
|
|
|
260
|
+ }
|
|
|
261
|
+ return 0;
|
|
|
262
|
+ }
|
|
256
|
263
|
recursive_interpret(tree->left, env_ptr);
|
|
257
|
264
|
return recursive_interpret(tree->right, env_ptr);
|
|
258
|
265
|
}
|