-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgclisp.cpp
executable file
·487 lines (471 loc) · 12.5 KB
/
gclisp.cpp
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#include "alloc.h"
#include <stdio.h>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <climits>
using std::cout;
using std::endl;
element Eval(element in);
extern element NIL;
/*element C(element s_exp){
return car(s_exp);
}*/
element interp_function(element s_exp) {
return Eval(car(s_exp));
}
/*element A(element s_exp){
return cdr(s_exp);
}*/
element interp_lambda(element s_exp) {
return s_exp;
}
element interp_car(element s_exp){
return car(Eval(car(s_exp)));
//return C(Eval(C(s_exp)));
}
element interp_cdr(element s_exp){
return cdr(Eval(car(s_exp)));
//return A(Eval(C(s_exp)));
}
element interp_cons(element s_exp){
//element i=Eval(C(s_exp));
//element t=Eval(C(A(s_exp)));
element i=Eval(car(s_exp));
element t=Eval(car(cdr(s_exp)));
return cons(i, t);
}
element interp_if(element s_exp){
return Eval(car(cdr((Eval(car(s_exp))==NIL)?cdr(s_exp):s_exp)));
}
bool L(element i, element s){
//cout << "L:" << i << " vs. " << s ;
if (BoxIsInteger(i) && BoxIsInteger(s))
return !equal_data(i, s);
if (BoxIsDouble(i) && BoxIsDouble(s))
return !equal_data(i, s);
if (BoxIsDouble(i) && (BoxIsDouble(s) || BoxIsInteger(s))
|| BoxIsDouble(s) && (BoxIsDouble(i) || BoxIsInteger(i)))
{
double id = BoxIsDouble(i) ?i.num : IntFromBox(i);
double sd = BoxIsDouble(s) ?s.num : IntFromBox(s);
return id != sd;
}
if (BoxIsDouble(i)||BoxIsInteger(i)||BoxIsDouble(s)||BoxIsInteger(s))
{
cout << __FUNCTION__ << " NEVER EVER " << i << ' '<< s <<endl;
}
bool r = !equal_data(i, s);
//cout << " -> " << r << endl;
return r;
}
element interp_equal(element s_exp){
return L(Eval(car(s_exp)),Eval(car(cdr(s_exp))))?NIL:symbol_create("t");
}
element interp_less(element s_exp){
element left = Eval(car(s_exp));
element right = Eval(car(cdr(s_exp)));
bool lt = false;
if (BoxIsInteger(left) && BoxIsInteger(right))
lt = IntFromBox(left) <IntFromBox(right);
if (BoxIsInteger(left) && !isnan(right.num))
lt = IntFromBox(left) < right.num;
else if (!isnan(left.num) && BoxIsInteger(right))
lt = left.num < IntFromBox(right);
else if (!isnan(left.num) && !isnan(right.num))
lt = left.num < right.num;
return lt ? symbol_create("t"):NIL;
}
element interp_add(element s_exp){
element left = Eval(car(s_exp));
element right = Eval(car(cdr(s_exp)));
if (BoxIsInteger(left) && BoxIsInteger(right))
return BoxFromInt(IntFromBox(left)+IntFromBox(right));
double d;
element r;
if (BoxIsInteger(left) && !isnan(right.num))
d = IntFromBox(left) + right.num;
else if (!isnan(left.num) && BoxIsInteger(right))
d = left.num + IntFromBox(right);
else if (!isnan(left.num) && !isnan(right.num))
d = left.num + right.num;
else
return NIL;
r.num = d;
return r;
}
element interp_sub(element s_exp){
element left = Eval(car(s_exp));
element right = Eval(car(cdr(s_exp)));
if (BoxIsInteger(left) && BoxIsInteger(right))
return BoxFromInt(IntFromBox(left)-IntFromBox(right));
double d;
element r;
if (BoxIsInteger(left) && !isnan(right.num))
d = IntFromBox(left) - right.num;
else if (!isnan(left.num) && BoxIsInteger(right))
d = left.num - IntFromBox(right);
else if (!isnan(left.num) && !isnan(right.num))
d = left.num - right.num;
else
return NIL;
r.num = d;
return r;
}
element interp_mul(element s_exp){
element left = Eval(car(s_exp));
element right = Eval(car(cdr(s_exp)));
if (BoxIsInteger(left) && BoxIsInteger(right))
return BoxFromInt(IntFromBox(left)*IntFromBox(right));
double d;
element r;
if (BoxIsInteger(left) && !isnan(right.num))
d = IntFromBox(left) * right.num;
else if (!isnan(left.num) && BoxIsInteger(right))
d = left.num * IntFromBox(right);
else if (!isnan(left.num) && !isnan(right.num))
d = left.num * right.num;
else
return NIL;
r.num = d;
return r;
}
element table;
bool builtins_loaded = false;
void enter(element n, element v)
{
Rooter n_r(n);
Rooter v_r(v);
table = array_append_element(table, cons(n, v));
}
void enter(element n, element (*f)(element))
{
Rooter n_r(n);
table = array_append_element(table, cons(n, BoxFromBuiltIn(f)));
}
element find(element n)
{
for (int i=IntFromBox(array_get_size(table)); i>0; --i)
{
element pair = array_get_element(table, BoxFromInt(i-1));
//cout << __FUNCTION__ << " n " << n << " vs. " << pair << " :" <<__FILE__<<':'<<__LINE__<<endl;
element ifirst = car(pair);
if (ifirst == n)
return cdr(pair);
}
return NIL;
}
element defun(element s_exp){
element e=cdr(s_exp);
element n=car(s_exp);
std::cout << "Entering e "<<e<<" n "<<n <<std::endl;
enter(n,e);
return n;
}
struct bi { const char* name; element (*fn)(element); };
bi builtins[] = {
"function", interp_function,
"quote", car,
"lambda", interp_lambda,
"defun", defun,
"if", interp_if,
"equal", interp_equal,
"<" , interp_less,
"+", interp_add,
"-", interp_sub,
"*", interp_mul,
"car", interp_car,
"cdr", interp_cdr,
"cons", interp_cons,
0,0
};
void setup()
{
if (!builtins_loaded) {
builtins_loaded = true;
table = array_create();
gc_add_root(&table); // This is permanent by the way.
for (bi* b=builtins; b->name!=0; ++b) {
enter(symbol_create(b->name), b->fn);
}
enter(symbol_create("t"),symbol_create("t"));
cout << table << endl;
cout << __FUNCTION__ << ' ' << "Built-ins installed" << " :" << __FILE__ << ':' << __LINE__ << endl;
}
}
void check_setup()
{
for (int i=IntFromBox(array_get_size(table)); i>0; --i)
{
element pair = array_get_element(table, BoxFromInt(i-1));
if (!BoxIsList(pair))
{
cout << __FUNCTION__ << " Element " << i << " of the environment is not a pair :" <<__FILE__<<':'<<__LINE__<<endl;
throw "bad setup";
}
//cout << __FUNCTION__ << " n " << n << " vs. " << pair << " :" <<__FILE__<<':'<<__LINE__<<endl;
element name = car(pair);
if (!BoxIsSymbol(name))
{
cout << __FUNCTION__ << " Element " << i << " car is not a symbol :" <<__FILE__<<':'<<__LINE__<<endl;
throw "bad setup";
}
// The names in the environment cannot be limited to a particular type.
}
}
element Eval(element in)
{
//Rooter in_r(in);
setup();
//check_setup();
cout << "eval |" << in << '|' << endl;
if (BoxIsInteger(in))
return in;
if (!isnan(in.num))
return in;
if (in == NIL)
return in;
#if 1
if (BoxIsSymbol(in)) {
element x = find(in);
if (BoxIsBuiltin(x))
return in;
if (x == NIL)
cout << __FUNCTION__ << " Lookup of " << in << " returned "<< x << " " << __FILE__ << ':' << __LINE__ <<endl;
return x;
}
#endif
if (!BoxIsList(in))
return in;
//std::cout << "Is List "<< in << std::endl;
//std::cout << "car: " << car(in) << std::endl;
//std::cout << "cdr: " << cdr(in) << std::endl;
element op = car(in);
if (BoxIsList(op))
op = Eval(op);
//std::cout << "after refinement "<< op << std::endl;
element x = find(op);
Rooter x_r(x);
if (BoxIsBuiltin(x))
{
Builtin f = BuiltinFromBox(x);
element r = f(cdr(in));
cout << __FUNCTION__ << " Result of built-in function " << r << " " << __FILE__ << ':' << __LINE__ <<endl;
return r;
}
element lambda = x;
Rooter lambda_r(lambda);
element formals = car(lambda);
Rooter formals_r(formals);
element actuals = cdr(in);
Rooter actuals_r(actuals);
//cout << "About to eval to environment" << endl;
element top = array_get_size(table);
while (formals != NIL && actuals != NIL) {
element formal = car(formals);
Rooter formal_r(formal);
element actual = Eval(car(actuals));
//cout << "Entering "<< formal << " " << actual << endl;
Rooter actual_r(actual);
enter(formal, actual);
formals = cdr(formals);
actuals = cdr(actuals);
}
//cout <<"Body image: lambda " << lambda << endl; cout <<" cdr(lambda) " << cdr(lambda) << endl; cout <<" car(cdr(lambda)) "<< car(cdr(lambda)) << endl;
element body = car(cdr(lambda));
//std::cout << "Evaluating "<< body <<" with the following:"<<std::endl;
//for (int i=top; i<table.size(); ++i) {
// std::cout << table[i].first << " " << table[i].second.get() << std::endl;
//}
element rv = Eval(body);
table=array_set_size(table, top);
//cout << "Result is " << rv << endl;
return rv;
}
static element atom;
void chartoatom(int ch)
{
string_append_char(atom, BoxFromInt(ch));
}
int peek_char(FILE* fp)
{
int ch = getc(fp);
ungetc(ch, fp);
return ch;
}
int check_delim(FILE* fp)
{
int ch = peek_char(fp);
if (!isspace(ch) && ch!='('&&ch!=')'&&ch!=';'&&ch!='"')
{
fprintf(stderr,"Improper delimiter for literal\n");
return 0;
}
return 1;
}
int skip_white(FILE* fp)
{
int ch;
eat_space:
for (ch=getc(fp); isspace(ch); ch=getc(fp))
;
if (ch==';')
{
while (ch != '\n' && ch!=EOF)
ch = getc(fp);
goto eat_space;
}
return ch;
}
element read_obj(FILE* fp);
element read_pair(FILE* fp)
{
int ch;
element car_obj;
element cdr_obj=NIL;
ch = skip_white(fp);
if (ch==')')
return NIL;
ungetc(ch, fp);
car_obj = read_obj(fp);
ch = skip_white(fp);
if (ch == '.')
{
//printf("read_pair %d\n", __LINE__);
cdr_obj = read_pair(fp);
return cons(car_obj,cdr_obj);
}
else if (ch == ')')
return cons(car_obj,cdr_obj);
ungetc(ch, fp);
//printf("read_pair %d\n", __LINE__);
cdr_obj = read_pair(fp);
return cons(car_obj,cdr_obj);
}
element read_obj(FILE* fp)
{
int ch;
ch = skip_white(fp);
#if 0
if (ch=='#')
{
// some sort of literal
ch = getc(fp);
if (ch=='(') // vector
{
int* elements = 0;
int nElements = 0;
int nElementCapy = 0;
ch = skip_white(fp);
while (ch != ')')
{
ungetc(ch, fp);
int element = read_obj(fp);
if (nElements+1 > nElementCapy) {
if (nElementCapy==0)
nElementCapy = 8;
else
nElementCapy = 6*nElementCapy/5+1;
elements = (int*)realloc(elements, nElementCapy*sizeof(int));
}
elements[nElements++] = element;
ch = skip_white(fp);
}
elements = (int*)realloc(elements, (nElements*4+11)&~7);
memmove(elements+1,elements,nElements*4);
elements[0] = nElements;
return VECTOR+(int)elements;
}
if (ch=='\\') // character literal
{
ch = getc(fp);
if (!check_delim(fp))
{
fprintf(stderr,"Improper delimiter for literal\n");
return NIL;
}
return ch*256+CHAR_LITERAL;
}
if (ch=='t')
return TRUE_LITERAL;
if (ch=='f')
return FALSE_LITERAL;
return NIL;
}
#endif
if (ch=='(')
{
element elt = read_pair(fp);
cout << "read_pair returned " << elt << std::endl;
return elt;
}
if (ch=='"')
{
atom = newstr();
ch = getc(fp);
while (ch != '"')
{
if (ch == '\\')
chartoatom(getc(fp));
else
chartoatom(ch);
ch = getc(fp);
}
return atom;
}
atom = newstr();
for (; ch!=EOF&&!isspace(ch)&&ch!='('&&ch!=')'&&ch!='#'&&ch!=';'; ch=getc(fp))
{
chartoatom(ch);
}
ungetc(ch,fp);
std::ostringstream os; os << atom;
const char* ip = os.str().c_str();
char* tail=0;
long lval = strtol(ip, &tail, 10);
if (tail[0]==0 && tail != ip && lval<INT_MAX && lval>INT_MIN)
return BoxFromInt(lval);
double dval = strtod(ip, &tail);
if (tail[0]==0 && tail!= ip)
atom.num = dval;
return symbol_from_string(atom);
}
#include <cstring>
extern element* alloc;
int main(int argc, char** argv)
{
init_heap();
if (argc==2 && strcmp(argv[1], "-t")==0)
{
setup();
extern element symbols;
cout << symbols << endl;
cout << table << endl;
element n = symbol_create("n");
element nm1 = cons(symbol_create("-"), cons(n,cons(BoxFromInt(1),NIL)));
element fnm1 = cons(symbol_create("fact"), cons(nm1,NIL));
element times = cons(symbol_create("*"), cons(n, cons(fnm1,NIL)));
element one = BoxFromInt(1);
element test = cons(symbol_create("equal"), cons(n, cons(BoxFromInt(0),NIL)));
element ifs = cons(symbol_create("if"), cons(test, cons(one, cons(times,NIL))));
element e = cons(symbol_create("defun"), cons(symbol_create("fact"), cons(cons(n,NIL),cons(ifs,NIL))));
cout << "Test expr " << e << endl;
element e2 = cons(symbol_create("fact"), cons(BoxFromDouble(50.0), NIL));
cout << "Test expr " << e2 << endl;
cout << Eval(e) << endl;
cout << Eval(e2) << endl;
return 0;
}
while (!feof(stdin))
{
cout << '[' << alloc << ']' << "> " << std::flush;
element e = read_obj(stdin);
cout << __FUNCTION__ << " The s-expr: " << e << endl;
element r = Eval(e);
cout << __FUNCTION__ << " Its value: " << r << endl;
cout << endl;
dump_heap();
}
return 0;
}