summaryrefslogtreecommitdiff
path: root/src/eval.c
blob: 63305b4c1aa297213a052e7e3e2e2cb866ebb082 (plain)
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
#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) {
			/* TODO: float */
			return NULL;
		}
		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 */
			/* TODO: scope */
			pair_t p = hashtable_get_pair_recurse(parentScope, (char*)symbol->name);
			if (p.key != NULL) {
				sym_t* nextSym = (sym_t*)p.value;
				/* add params */
				sym_t** addedArgs = malloc(100 * sizeof(sym_t*));
				for (int i = 0; i < symbol->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(symbol->args[i], parentScope);
					addedArgs[i] = argV;
					pair_t argPair = init_pair(keyName, argV);
					free(keyName);
					hashtable_add_pair(newScope, argPair);
				}
				returnV = eval(nextSym, newScope);
				/* clean up args */
				for (int i = 0; i < symbol->argCount && i < 100; i++) {
					destroy_sym(addedArgs[i]);
				}
				free(addedArgs);
			}
			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);
	}
}
/* 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("store")) {
		/* first arg is value, remaining args are names */
		/* TODO: recursion for multiple pairs */
		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_add_pair(symbolScope->parent, p);
		return init_sym_int(1);
	}
	else if (hash == get_hash("update")) {
		/* first arg is value, remaining args are names */
		/* TODO: recursion for multiple pairs */
		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);
	}
	/* TODO: release()/free() method? */
	else if (hash == get_hash("eq")) {
		litType_t type = 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 (type == NO_TYPE) {
				type = v->litType;
				if (type == INT) {
					firstInt = symtoi(v);
				}
				else if (type == FLOAT) {
					firstFloat = symtof(v);
				}
				else if (type == STRING) {
					firstString = symtos(v);
				}
				destroy_sym(v);
			}
			else {
				/* interpret type */
				if (type == INT) {
					int si = symtoi(v);
					destroy_sym(v);
					if (si != firstInt) {
						return init_sym_int(0);
					}
				}
				else if (type == FLOAT) {
					float sf = symtof(v);
					destroy_sym(v);
					if (sf != firstFloat) {
						return init_sym_int(0);
					}
				}
				else if (type == STRING) {
					char* ss = symtos(v);
					if (strcmp(ss, firstString) != 0) {
						destroy_sym(v);
						free(firstString);
						return init_sym_int(0);
					}
					destroy_sym(v);
				}
			}
		}
		if (type == 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);
		}
		/* TODO: free other args if condition fails */
		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;
		for (int i  = 0; i < symbol->argCount; i++) {
			sym_t* v = eval(symbol->args[i], symbolScope);
			sum += symtoi(v);
			destroy_sym(v);
		}
		return init_sym_int(sum);
	}
	else if (hash == get_hash("sub")) {
		sym_t* v1 = eval(symbol->args[0], symbolScope);
		int sum = symtoi(v1);
		destroy_sym(v1);
		for (int i = 1; i < symbol->argCount; i++) {
			sym_t* v = eval(symbol->args[i], symbolScope);
			sum -= symtoi(v);
			destroy_sym(v);
		}
		return init_sym_int(sum);
	}
	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) {
		}
		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;
	}
}