-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisp.cpp
554 lines (482 loc) · 15.8 KB
/
lisp.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#include "lisp.hpp"
#include <sstream>
std::ostream& operator<<(std::ostream& os, const TokenPtr& s)
{
LispInterpreter::print_item(os, s);
return os;
}
TokenPtr Context::make_exception(const std::string err) {
TokenPtr t = std::make_shared<Token>();
t->type = Token::EXCEPTION;
t->sym = interp->find_symbol(err);
t->context = shared_from_this();
return t;
}
std::string Context::get_full_path(SymbolPtr name)
{
std::string s;
if (name) s = std::string(".") + std::string(name->as_stringview());
switch (type) {
case Token::OBJECT:
s = "object" + s;
break;
case Token::FUNC:
case Token::CLASS:
s = std::string(this->name->as_stringview()) + s;
break;
default:
if (parent) {
s = "local" + s;
} else {
return "global" + s;
}
}
return parent->get_full_path(0) + "." + s;
}
void Context::set_global(SymbolPtr s, TokenPtr t)
{
if (!parent) {
set(s, t);
} else {
parent->set_global(s, t);
}
}
ContextPtr Context::get_owner(SymbolPtr& name)
{
ContextPtr owner = shared_from_this();
while (name->next) {
TokenPtr t = owner->get_local(name);
if (!t) {
std::cout << "No such variable or context ";
name->print_item(std::cout);
std::cout << std::endl;
return 0;
}
if (t->type != Token::OBJECT && t->type != Token::CLASS) {
std::cout << "Variable ";
name->print_item(std::cout);
std::cout << " is not of type object or class\n";
return 0;
}
std::cout << "Owner of " << name << " is " << t->sym << std::endl;
owner = t->context;
name = name->next;
}
return owner;
}
TokenPtr Context::get_local(SymbolPtr name)
{
return vars.get(name);
}
TokenPtr Context::get(SymbolPtr& name, ContextPtr& owner)
{
owner = get_owner(name);
TokenPtr t = owner->vars.get(name);
if (t) return t;
if (!owner->parent) return 0;
if (owner->type == Token::OBJECT) {
ContextPtr dummy;
return owner->parent->get(name, dummy);
} else {
return owner->parent->get(name, owner);
}
}
SymbolPtr Interns::find(const char *p, int len)
{
int free_index = -1;
for (int i=0; i<syms.size(); i++) {
SymbolPtr s = syms[i].lock();
if (!s) {
free_index = i;
} else {
if (len == s->len && 0==memcmp(p, s->p, len)) return s;
}
}
if (free_index < 0) {
free_index = syms.size();
syms.resize(free_index+1);
}
SymbolPtr s = std::make_shared<Symbol>();
s->len = len;
s->p = new char[len];
s->index = free_index;
memcpy(s->p, p, len);
syms[free_index] = s;
return s;
}
TokenPtr Token::zero = Token::make_int(0);
TokenPtr Token::one = Token::make_int(1);
TokenPtr Token::negone = Token::make_int(-1);
TokenPtr Token::bool_true = Token::make_bool(true);
TokenPtr Token::bool_false = Token::make_bool(false);
TokenPtr Token::make_string(LispInterpreter *interp, const char *p, int len)
{
SymbolPtr s = interp->interns.find(p, len);
TokenPtr l = std::make_shared<Token>();
l->type = STR;
l->sym = s;
return l;
}
TokenPtr Token::as_number(TokenPtr item)
{
if (!item) return zero;
if (item->type == BOOL) return make_bool(item->ival);
if (item->type == INT || item->type == FLOAT) return item;
if (item->type == STR) {
Parsing p(item->sym->p, item->sym->len);
TokenPtr t = parse_number(p);
if (t->type == INT || t->type == FLOAT) return t;
}
return zero;
}
TokenPtr Token::as_bool(TokenPtr item)
{
if (!item) return bool_false;
if (item->type == BOOL) return item;
if (item->type == INT) return (item->ival ? bool_true : bool_false);
if (item->type == FLOAT) return (item->fval ? bool_true : bool_false);
if (item->type == STR) return (item->sym->len && item->sym->p[0]!='0') ? bool_true : bool_false;
if (item->type == SYM) return bool_true;
if (item->type == LIST || item->type == INFIX) return (item->list ? bool_true : bool_false);
return bool_false;
}
bool Token::bool_val(TokenPtr item)
{
return as_bool(item)->ival;
}
TokenPtr Token::as_int(TokenPtr item)
{
TokenPtr t = as_number(item);
if (t->type == INT) {
return t;
} else {
return make_int(t->fval);
}
}
TokenPtr Token::as_float(TokenPtr item)
{
TokenPtr t = as_number(item);
if (t->type == FLOAT) {
return t;
} else {
return make_float(t->ival);
}
}
float Token::float_val(TokenPtr item)
{
if (!item) return 0;
if (item->type == FLOAT) return item->fval;
if (item->type == INT || item->type == BOOL) return item->ival;
if (item->type == STR) {
Parsing p(item->sym->p, item->sym->len);
TokenPtr t = parse_number(p);
if (t->type == FLOAT) return t->fval;
if (t->type == INT) return t->ival;
}
return 0;
}
int Token::int_val(TokenPtr item)
{
if (!item) return 0;
if (item->type == FLOAT) return item->fval;
if (item->type == INT || item->type == BOOL) return item->ival;
if (item->type == STR) {
Parsing p(item->sym->p, item->sym->len);
TokenPtr t = parse_number(p);
if (t->type == FLOAT) return t->fval;
if (t->type == INT) return t->ival;
}
return 0;
}
TokenPtr Token::as_string(LispInterpreter *interp, TokenPtr item)
{
if (!item) return interp->empty_string;
if (item->type == STR) return item;
std::stringstream ss;
LispInterpreter::print_item(ss, item);
return make_string(interp, ss.str());
}
std::string Token::string_val(TokenPtr item)
{
if (!item) return "";
if (item->type == STR) return std::string(item->sym->p, item->sym->len);
std::stringstream ss;
LispInterpreter::print_item(ss, item);
return ss.str();
}
std::string Token::inspect(TokenPtr item)
{
if (!item) return "[null]";
std::stringstream ss;
LispInterpreter::print_item(ss, item);
return ss.str();
}
void LispInterpreter::addOperator(const std::string& name, built_in_f f, int precedence, int order)
{
SymbolPtr s = interns.find(name.data(), name.length());
TokenPtr t = std::make_shared<Token>();
t->type = Token::OPER;
t->oper = f;
t->sym = s;
t->precedence = precedence;
t->order = order;
globals->set(s, t);
}
void LispInterpreter::addFunction(TokenPtr list, ContextPtr context)
{
// Extract name and arg list
// XXX Check to make sure there's an args list
// XXX Check for nulls
if (list->type != Token::SYM) return;
TokenPtr t = std::make_shared<Token>();
t->type = Token::FUNC;
t->sym = list->sym;
t->list = list->next;
context->set(list->sym, t);
}
TokenPtr LispInterpreter::callFunction(SymbolPtr name, TokenPtr args, TokenPtr params, ContextPtr owner, ContextPtr caller)
{
std::cout << "Calling func " << name << ": ";
print_list(std::cout, params);
std::cout << std::endl;
TokenPtr body = params->next;
if (!body) return 0; // Empty body is okay, but we need to do no more work
if (params->type != Token::LIST) {
std::cout << "Function doesn't start with list of args\n";
return 0;
}
// Make a local variable context
owner = owner->make_child_function(name);
params = params->list;
while (args && params) {
// XXX handle wrong number of arguments
SymbolPtr param_name = params->sym;
if (params->quote) {
// Final element, gets rest of args as list
// This needs to make a list out of evaluated items
// XXX Evaluate each from the calling context
owner->set(param_name, Token::as_list(args));
break;
} else {
// Evaluate argument in the calling context and put the result into the callee context
owner->set(param_name, evaluate_item(args, caller));
}
params = params->next;
args = args->next;
}
return evaluate_list(body, owner);
}
// void LispInterpreter::setVariable(TokenPtr list, ContextPtr context)
// {
// if (list->type != Token::SYM) {
// printf("Not a symbol\n");
// return;
// }
// std::cout << "Set var: ";
// print_list(std::cout, list);
// std::cout << std::endl;
// context->set(list->sym, list->next);
// }
TokenPtr LispInterpreter::getVariable(TokenPtr name, ContextPtr context)
{
if (name->type != Token::SYM) {
return context->make_exception("Token " + Token::inspect(name) + " is not a symbol");
}
std::cout << "Get var: " << name->sym << std::endl;
return context->get(name->sym);
}
// Evaluate a single item
TokenPtr LispInterpreter::evaluate_item(TokenPtr item, ContextPtr caller)
{
if (!item) return 0;
std::cout << "Evaluating: ";
print_item(std::cout, item, caller);
std::cout << std::endl;
if (item->quote) return item;
if (!caller) caller = globals;
if (item->type == Token::LIST) {
// Unquoted list is function or operator call or list of calls
TokenPtr list = item->list;
TokenPtr ret;
while (list && list->type == Token::LIST) {
ret = evaluate_item(list, caller);
list = list->next;
}
if (!list) return ret;
if (list->type != Token::SYM) {
std::cout << "Function call must start with symbol\n";
return caller->make_exception("Function call must start with symbol: " + Token::inspect(list));
}
SymbolPtr name = list->sym;
TokenPtr args = list->next;
ContextPtr owner;
TokenPtr func = caller->get(name, owner);
if (!func) {
std::cout << "Function call failed\n";
return caller->make_exception("Function name not found: " + Token::inspect(list));
}
if (func->type == Token::CLASS) {
TokenPtr obj = std::make_shared<Token>();
obj->type = Token::OBJECT;
//obj->sym = name;
obj->context = func->context->make_child_object(); // Parent of object is class
// See if class has "_init" method
SymbolPtr _init_sym = find_symbol("_init");
TokenPtr _init_func = func->context->get_local(_init_sym);
if (_init_func) {
if (_init_func->type != Token::FUNC) {
return caller->make_exception("_init isn't a function: " + Token::inspect(list));
std::cout << "Error: _init must be function\n";
} else {
TokenPtr params_body = _init_func->list;
TokenPtr r = callFunction(_init_sym, args, params_body, obj->context, caller);
if (r->type == Token::EXCEPTION) return caller->make_exception(r);
}
}
return obj;
} else if (func->type == Token::OPER) {
return (*func->oper)(list->next, caller);
} else if (func->type == Token::FUNC) {
TokenPtr params_body = func->list;
return caller->check_exception(callFunction(name, args, params_body, owner, caller));
} else {
return caller->make_exception("Function call must start with valid function or operator: " + Token::inspect(list));
}
}
if (item->type == Token::SYM) {
// Unquoted symbol gets looked up
return getVariable(item, caller);
}
return item;
}
// Evaluates a function body -- rename to evaluate_rest
TokenPtr LispInterpreter::evaluate_list(TokenPtr list, ContextPtr context)
{
TokenPtr ret;
while (list) {
ret = evaluate_item(list, context);
list = list->next;
}
return ret;
}
// XXX rename this
TokenPtr LispInterpreter::evaluate_string(const std::string& s, ContextPtr context)
{
TokenPtr p = parse_string(s);
return evaluate_item(p, context);
}
// XXX do error checking
TokenPtr LispInterpreter::transform_infix(TokenPtr list)
{
std::vector<TokenPtr> stack, queue;
int p1, p2;
TokenPtr oper;
if (list->type != Token::INFIX) return list;
std::cout << "Infix list: ";
print_list(std::cout, list);
std::cout << std::endl;
TokenPtr item = list->list;
while (item) {
switch (item->type) {
case Token::SYM:
oper = globals->get(item->sym);
if (oper && oper->type == Token::OPER) {
item->precedence = oper->precedence;
item->order = oper->order;
p1 = item->precedence;
std::cout << "p1=" << p1 << std::endl;
while (stack.size()) {
TokenPtr o2 = stack.back();
p2 = o2->precedence;
std::cout << "p2=" << p2 << std::endl;
if ((o2->order == Token::LASSOC && p2 > p1) || (o2->order != Token::LASSOC && p2 >= p1)) break;
stack.pop_back();
queue.push_back(o2);
}
// Make sure this item is marked as an operator
if (!item->precedence) item->precedence = 1;
}
stack.push_back(item);
break;
case Token::INFIX:
queue.push_back(transform_infix(item->list));
break;
default:
queue.push_back(item);
break;
}
item = item->next;
}
while (stack.size()) {
TokenPtr o2 = stack.back();
stack.pop_back();
queue.push_back(o2);
}
std::cout << "Queue: ";
for (int i=0; i<queue.size(); i++) {
print_item(std::cout, queue[i]);
std::cout << " ";
}
std::cout << std::endl;
stack.clear();
if (queue.size() < 2) {
TokenPtr item = Token::make_string(this, std::string_view("identity"));
item->type = Token::SYM;
item->precedence = 1;
item->order = Token::UNARY;
queue.push_back(item);
}
for (int i=0; i<queue.size(); i++) {
TokenPtr c = queue[i];
if (c->type == Token::SYM && c->precedence) {
if (c->order == Token::UNARY) {
TokenPtr a = stack.back(); stack.pop_back();
c->next = a;
a->next = 0;
} else {
if (stack.size() < 2) return 0;
TokenPtr b = stack.back(); stack.pop_back();
TokenPtr a = stack.back(); stack.pop_back();
c->next = a;
a->next = b;
b->next = 0;
}
stack.push_back(Token::as_list(c));
} else {
stack.push_back(c);
}
}
std::cout << "Transformed list: ";
print_list(std::cout, stack.back());
std::cout << std::endl;
return stack.back();
}
/*
evaluate_each -- produce list of evaluated entries of a list
*/
#if 0
int main() {
// const char *floatStr = "3.14e2";
// int length = strlen(floatStr);
// float value = parse_float(floatStr, length);
//
// printf("Parsed value: %f\n", value);
LispInterpreter li;
for (std::string line; std::getline(std::cin, line);) {
TokenPtr p = li.evaluate_string(line);
li.print_list(std::cout, p);
std::cout << std::endl;
}
// const char *sample = "('(x y) 4 '3.14 3.14e3 \"string\033xxx\") 123";
//const char *sample = "(defun add4 (x) (+ x 4)) (add4 12)";
//const char *sample = "(defun myadd (x y) (+ x y)) (myadd 13 8)";
// const char *sample = "5";
// TokenPtr p = li.evaluate_string(sample);
// li.print_list(std::cout, p);
// std::cout << std::endl;
// // TokenPtr q = li.evaluate_list(p);
// // li.print_list(std::cout, q);
// std::cout << std::endl;
return 0;
}
#endif