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
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "eval.h"
#include "hashtable.h"
#include "parse.h"
sym_t* eval(sym_t* symbol, hashtable_t* parentScope) {
if (symbol->province == LITERAL) {
if (symbol->litType == INT) {
return init_sym_int(*((int*)symbol->name));
}
else if (symbol->litType == FLOAT) {
return init_sym_float(*((float*)symbol->name));
}
else if (symbol->litType == STRING) {
return init_sym_str((char*)symbol->name);
}
else {
return NULL;
}
}
else if (symbol->province == KEY_REF) {
sym_t* returnV = NULL;
/* entering new scope, so create a new table */
hashtable_t* newScope = init_hashtable(50, parentScope);
sym_t* ir = try_eval_internal(symbol, newScope);
if (ir != NULL) {
returnV = ir;
}
else {
/* not an internal function, search hashtable for
next symbol */
pair_t p = hashtable_get_pair_recurse(parentScope, (char*)symbol->name);
if (p.key != NULL) {
sym_t* nextSym = (sym_t*)p.value;
if (symbol->argCount > 0) {
returnV = eval_with_args(nextSym, symbol->args, symbol->argCount, newScope);
}
else {
returnV = eval(nextSym, newScope);
}
}
else {
printf("no matching pair in hashtable\n");
}
}
/* clear new scope, then return */
destroy_hashtable(newScope);
return returnV;
}
else {
printf("no symbol province\n");
return NULL;
}
}
void exec_all(sym_t** symbols, int count, hashtable_t* symbolScope) {
for (int i = 0; i < count; i++) {
sym_t* sym = eval(symbols[i], symbolScope);
destroy_sym(sym);
}
}
sym_t* eval_with_args(sym_t* symbol, sym_t** args, int argCount, hashtable_t* symbolScope) {
sym_t** addedArgs = malloc(100 * sizeof(sym_t*));
for (int i = 0; i < argCount && i < 100; i++) {
char* keyName = malloc(6 * sizeof(char));
strcpy(keyName, "arg");
char index[3];
snprintf(index, 3, "%d", i);
strcat(keyName, index);
sym_t* argV = eval(args[i], symbolScope->parent);
addedArgs[i] = argV;
pair_t argPair = init_pair(keyName, argV);
free(keyName);
hashtable_add_pair(symbolScope, argPair);
}
sym_t* v = eval(symbol, symbolScope);
/* clean up args */
for (int i = 0; i < argCount && i < 100; i++) {
destroy_sym(addedArgs[i]);
}
free(addedArgs);
return v;
}
/* internal functions?
this function has side-effects */
sym_t* try_eval_internal(sym_t* symbol, hashtable_t* symbolScope) {
unsigned int hash = get_hash((char*)symbol->name);
if (hash == get_hash("root")) {
/* execute all args as roots except for the last */
/* return last arg, that's the function return line */
exec_all(symbol->args, symbol->argCount - 1, symbolScope);
return eval(symbol->args[symbol->argCount - 1], symbolScope);
}
else if (hash == get_hash("ref")) {
return symbol->args[0];
}
else if (hash == get_hash("eval")) {
sym_t* v;
if (symbol->argCount > 1) {
v = eval_with_args(eval(symbol->args[0], symbolScope), symbol->args + 1, symbol->argCount - 1, symbolScope);
}
else {
v = eval(eval(symbol->args[0], symbolScope), symbolScope);
}
return v;
}
else if (hash == get_hash("store")) {
/* first arg is value, second arg is key */
sym_t* v = eval(symbol->args[1], symbolScope);
char* k = symtos(v);
destroy_sym(v);
sym_t* value = eval(symbol->args[0], symbolScope);
pair_t p = init_pair(k, (void*)value);
free(k);
hashtable_add_pair(symbolScope->parent, p);
return init_sym_int(1);
}
else if (hash == get_hash("update")) {
/* first arg is value, second arg is key */
sym_t* v = eval(symbol->args[1], symbolScope);
char* k = symtos(v);
destroy_sym(v);
sym_t* value = symbol->args[0];
pair_t p = init_pair(k, (void*)value);
free(k);
hashtable_update_pair_recurse(symbolScope->parent, p);
return init_sym_int(1);
}
else if (hash == get_hash("eq")) {
litType_t firstType = NO_TYPE;
int firstInt = 0;
float firstFloat = 0;
char* firstString;
for (int i = 0; i < symbol->argCount; i++) {
sym_t* v = eval(symbol->args[i], symbolScope);
/* initialize */
if (firstType == NO_TYPE) {
firstType = v->litType;
if (firstType == INT) {
firstInt = symtoi(v);
}
else if (firstType == FLOAT) {
firstFloat = symtof(v);
}
else if (firstType == STRING) {
firstString = symtos(v);
}
destroy_sym(v);
}
else {
/* interpret type */
if (v->litType == INT) {
int si = symtoi(v);
destroy_sym(v);
if (si != (firstType == FLOAT ? firstFloat : firstInt)) {
return init_sym_int(0);
}
}
else if (v->litType == FLOAT) {
float sf = symtof(v);
destroy_sym(v);
if (sf != (firstType == FLOAT ? firstFloat : firstInt)) {
return init_sym_int(0);
}
}
else if (v->litType == STRING) {
char* ss = symtos(v);
if (strcmp(ss, firstString) != 0) {
destroy_sym(v);
free(firstString);
return init_sym_int(0);
}
destroy_sym(v);
}
}
}
if (firstType == STRING) {
free(firstString);
}
return init_sym_int(1);
}
else if (hash == get_hash("if")) {
sym_t* v = eval(symbol->args[0], symbolScope);
int si = symtoi(v);
destroy_sym(v);
if (si != 0) {
exec_all(symbol->args + 1, symbol->argCount - 1, symbolScope);
return init_sym_int(1);
}
return init_sym_int(0);
}
else if (hash == get_hash("not")) {
sym_t* v = eval(symbol->args[0], symbolScope);
int b = symtoi(v);
destroy_sym(v);
return init_sym_int(!b);
}
else if (hash == get_hash("sum")) {
int sum = 0;
float fSum = 0;
char isFloat = 0;
for (int i = 0; i < symbol->argCount; i++) {
sym_t* v = eval(symbol->args[i], symbolScope);
if (v->litType == INT) {
sum += symtoi(v);
}
else if (v->litType == FLOAT) {
fSum += symtof(v);
isFloat = 1;
}
destroy_sym(v);
}
if (!isFloat) {
return init_sym_int(sum);
}
else {
return init_sym_float(sum + fSum);
}
}
else if (hash == get_hash("sub")) {
sym_t* v1 = eval(symbol->args[0], symbolScope);
int sum = 0;
float fSum = 0;
char isFloat = 0;
if (v1->litType == INT) {
sum = symtoi(v1);
}
else if (v1->litType == FLOAT) {
fSum = symtof(v1);
isFloat = 1;
}
destroy_sym(v1);
for (int i = 1; i < symbol->argCount; i++) {
sym_t* v = eval(symbol->args[i], symbolScope);
if (v->litType == INT) {
sum -= symtoi(v);
}
else if (v->litType == FLOAT) {
fSum -= symtof(v);
isFloat = 1;
}
destroy_sym(v);
}
if (!isFloat) {
return init_sym_int(sum);
}
else {
return init_sym_float(sum + fSum);
}
}
else if (hash == get_hash("print")) {
sym_t* v = eval(symbol->args[0], symbolScope);
if (v->litType == INT) {
printf("%d\n", symtoi(v));
}
else if (v->litType == FLOAT) {
printf("%.5f\n", symtof(v));
}
else if (v->litType == STRING) {
char* s = symtos(v);
printf("%s\n", s);
free(s);
}
destroy_sym(v);
return init_sym_int(1);
}
else {
return NULL;
}
}
|