summaryrefslogtreecommitdiff
path: root/interpreter.c
blob: fec9087e07a81fef0155341f77fd4c362ae6d75f (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "interpreter.h"
#include "inter_builtins.h"
#include "util.h"


int namehash(const char *name,int mod){
	int h=0;
	for(int i=0;name[i];i++)h+=name[i];
	return h%mod;
}


#define VMAP_HASHSZ (61)

typedef struct Vllist{
	char *name;
	AST *value;
	struct Vllist *next;
} Vllist;

typedef struct Scope{
	Vllist *vmap[VMAP_HASHSZ];
	struct Scope *next;
} Scope;


typedef struct Symbolstore{
	int sz,len;
	char **syms;
} Symbolstore;


struct InterState{
	Scope *scope;
	Symbolstore ss;
};


Scope* scope_make(void){
	Scope *scope=malloc(1,Scope);
	memset(scope->vmap,0,VMAP_HASHSZ*sizeof(Vllist*));
	scope->next=NULL;
	return scope;
}


InterState* inter_make(void){
	InterState *is=malloc(1,InterState);
	is->scope=scope_make();
	is->ss.sz=16;
	is->ss.len=0;
	is->ss.syms=malloc(is->ss.sz,char*);
	return is;
}

static void scope_destroy(Scope *scope,bool recursive){
	do {
		for(int i=0;i<VMAP_HASHSZ;i++){
			while(scope->vmap[i]){
				Vllist *ll=scope->vmap[i];
				free(ll->name);
				ast_free(ll->value);
				scope->vmap[i]=ll->next;
				free(ll);
			}
		}
		Scope *next=scope->next;
		free(scope);
		scope=next;
	} while(recursive&&scope);
}

void inter_destroy(InterState *is){
	assert(is);
	scope_destroy(is->scope,true);
	for(int i=0;i<is->ss.len;i++)free(is->ss.syms[i]);
	free(is->ss.syms);
}


static void intern_symbols(InterState *is,AST *ast){
	switch(ast->type){
		case AST_LIST:
			for(int i=0;i<ast->li.len;i++)intern_symbols(is,ast->li.nodes[i]);
			break;

		case AST_SYMBOL:{
			if(ast->sy.symid>=0&&ast->sy.symid<is->ss.len&&strcmp(ast->sy.name,is->ss.syms[ast->sy.symid])==0){
				break;
			}
			int i;
			for(i=0;i<is->ss.len;i++){
				if(strcmp(is->ss.syms[i],ast->sy.name)==0)break;
			}
			if(i<is->ss.len){
				ast->sy.symid=i;
				break;
			}
			if(is->ss.len==is->ss.sz){
				is->ss.sz*=2;
				is->ss.syms=realloc(is->ss.syms,is->ss.sz,char*);
			}
			is->ss.syms[is->ss.len++]=copystring(ast->sy.name);
			break;
		}

		case AST_QUOTED:
			intern_symbols(is,ast->qu.ast);
			break;

		case AST_WORD:
		case AST_NUMBER:
		case AST_STRING:
			break;

		default:
			assert(false);
	}
}

static const AST* find_var(const InterState *is,const char *name){
	int h=namehash(name,VMAP_HASHSZ);
	for(const Scope *scope=is->scope;scope;scope=scope->next){
		for(const Vllist *ll=scope->vmap[h];ll;ll=ll->next){
			if(strcmp(name,ll->name)==0)return ll->value;
		}
	}
	return NULL;
}

InterRet ir_ast(AST *ast){
	InterRet ir={ast,NULL};
	return ir;
}

InterRet ir_err(char *errstr){
	InterRet ir={NULL,errstr};
	return ir;
}

InterRet ir_err_c(const char *errstr){
	return ir_err(copystring(errstr));
}

static InterRet interpret(InterState *is,const AST *ast){
	switch(ast->type){
		case AST_LIST:{
			if(ast->li.len==0){
				return ir_ast(ast_copy(ast));
			}
			assert(ast->li.len>0);
			AST *nodes[ast->li.len];
			for(int i=0;i<ast->li.len;i++){
				InterRet ir=interpret(is,ast->li.nodes[i]);;
				if(ir.errstr){
					while(i-->0)ast_free(nodes[i]);
					return ir;
				}
				nodes[i]=ir.ast;
			}

			if(nodes[0]->type!=AST_LAMBDA){
				fprintf(stderr,"type = %d\n",nodes[0]->type);
				return ir_err_c("First node in evaluated list not a function");
			}

#define NOT_IMPLEMENTED false
			if(nodes[0]->la.body)assert(NOT_IMPLEMENTED);

			InterRet ir=nodes[0]->la.cfunc(is,ast->li.len-1,nodes+1);
			for(int i=0;i<ast->li.len;i++)ast_free(nodes[i]);
			return ir;
		}

		case AST_SYMBOL:
			return ir_ast(ast_copy(ast));

		case AST_WORD:{
			const AST *cv=find_var(is,ast->wo.word);
			if(cv)return ir_ast(ast_copy(cv));
			char *errstr;
			asprintf(&errstr,"Unknown variable '%s'",ast->wo.word);
			if(!errstr)outofmem();
			return ir_err(errstr);
		}

		case AST_NUMBER:
			return ir_ast(ast_copy(ast));

		case AST_STRING:
			return ir_ast(ast_copy(ast));

		case AST_QUOTED:
			return ir_ast(ast_copy(ast->qu.ast));

		default:
			assert(false);
	}
}

void inter_assign(InterState *is,const char *name,AST *ast){
	assert(is->scope);
	int h=namehash(name,VMAP_HASHSZ);
	Vllist *ll=malloc(1,Vllist);
	ll->name=copystring(name);
	ll->value=ast;
	ll->next=is->scope->vmap[h];
	is->scope->vmap[h]=ll;
}

void inter_register(InterState *is,const char *name,lambdafunc_t cfunc){
	inter_assign(is,name,ast_lambda(cfunc,NULL));
}

void inter_register_prelude(InterState *is){
	inter_register(is,"do",builtin_do);
	inter_register(is,"print",builtin_print);
	inter_register(is,"+",builtin_sum);
	inter_register(is,"-",builtin_difference);
	inter_register(is,"*",builtin_product);
	inter_register(is,"/",builtin_quotient);
	inter_register(is,"%",builtin_remainder);
	inter_register(is,"sum",builtin_sum);
	inter_register(is,"difference",builtin_difference);
	inter_register(is,"product",builtin_product);
	inter_register(is,"quotient",builtin_quotient);
	inter_register(is,"remainder",builtin_remainder);
	inter_register(is,"define",builtin_define);
}

InterRet inter_runcode(InterState *is,AST *ast){
	intern_symbols(is,ast);
	return interpret(is,ast);
}