-
Notifications
You must be signed in to change notification settings - Fork 0
/
lval.cpp
757 lines (703 loc) · 22.5 KB
/
lval.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
#include <iostream>
using namespace std;
#include <errno.h>
#include "lval.h"
#include "lenv.h"
#include "config.h"
#define NewLval Lval* lval = new Lval
#define LVAL_ASSERT(x, that, ...) if (!(x)) { Lval& err = Lval::lval_err(__VA_ARGS__); that.lval_delete(); return err; }
#define LVAL_ASSERT_NUM(func, x, expect) LVAL_ASSERT(x.cells->size() == expect, x, \
"Function '%s', Pass Invalid Count of Args. Expect %d, Got %d", func, expect, x.cells->size() \
)
#define LVAL_ASSERT_TYPE(func, x, index, expect) LVAL_ASSERT(expect == x.cells->at(index)->type, x, \
"Function '%s', Pass Invalid Type. Expect %s, Got %s", func, Lval::lval_type2name(expect), Lval::lval_type2name(x.cells->at(index)->type) \
)
#define LVAL_ASSERT_NOT_EMPTY(func, x, index) LVAL_ASSERT(0 != x.cells->at(index)->cells->size(), x, \
"Function '%s', Pass '{}'.", func \
)
#define LVAL_FATAL_ERROR(func, msg) cout << __func__ << "::" << func << msg << endl; exit(EXIT_FAILURE);
#pragma mark - Functions
Lval& lval_fast_copy(const Lval& from, Lval& to) {
to.type = from.type;
switch (to.type) {
case LVAL_TYPE_NUM:
to.num = from.num;
break;
case LVAL_TYPE_ERR:
to.error = from.error;
break;
case LVAL_TYPE_SYM:
to.symbol = from.symbol;
break;
case LVAL_TYPE_FUNC:
if (from.func) {
to.func = from.func;
} else {
to.func = NULL;
to.formals = &from.formals->lval_copy();
to.body = &from.body->lval_copy();
to.env = &from.env->lenv_copy();
}
break;
case LVAL_TYPE_QEXPR:
case LVAL_TYPE_SEXPR: {
to.cells = new Lval::Lvaldq;
for (Lval::Lvaldq::iterator it = from.cells->begin(); it != from.cells->end(); it++) {
Lval& lval = Lval::lval_sym("");
lval_fast_copy(*(*it), lval);
to.lval_add(lval);
}
}
break;
}
return to;
}
#pragma mark - Methods
Lval& Lval::lval_add(Lval& a) {
this->cells->push_back(&a);
return *this;
}
Lval& Lval::lval_pop(uint32_t index) {
Lval* node = cells->at(index);
cells->erase(cells->begin()+index);
return *node;
}
Lval& Lval::lval_pop_front() {
Lval* node = cells->front();
cells->pop_front();
return *node;
}
Lval& Lval::lval_pop_back() {
Lval* node = cells->back();
cells->pop_back();
return *node;
}
Lval& Lval::lval_take(uint32_t index) {
Lval& node = lval_pop(index);
cells->clear();
return node;
}
Lval& Lval::lval_copy() {
NewLval;
return lval_fast_copy(*this, *lval);
}
bool Lval::operator==(const Lval& other) {
if (type != other.type) return false;
switch (type) {
case LVAL_TYPE_ERR: return error == other.error;
case LVAL_TYPE_NUM: return num == other.num;
case LVAL_TYPE_SYM: return symbol == other.symbol;
case LVAL_TYPE_FUNC: {
if (func) {
return func == other.func;
} else {
if (formals->cells->size() != other.formals->cells->size()) return false;
if (body->cells->size() != other.cells->size()) return false;
Lvaldq::iterator this_it = formals->cells->begin();
Lvaldq::iterator other_it = other.formals->cells->begin();
Lval *thisObj, *otherObj;
for (int i = 0; i < formals->cells->size(); i++) {
thisObj = *(this_it + i);
otherObj = *(other_it + i);
if (!(thisObj == otherObj)) return false;
}
this_it = body->cells->begin();
other_it = other.body->cells->begin();
for (int i = 0; i < body->cells->size(); i++) {
thisObj = *(this_it + i);
otherObj = *(other_it + i);
if (!(thisObj == otherObj)) return false;
}
return true;
}
}
case LVAL_TYPE_QEXPR:
case LVAL_TYPE_SEXPR: {
if (cells->size() != other.cells->size()) return false;
Lvaldq::iterator this_it = cells->begin();
Lvaldq::iterator other_it = other.cells->begin();
Lval *thisObj, *otherObj;
for (int i = 0; i < cells->size(); i++) {
thisObj = *(this_it + i);
otherObj = *(other_it + i);
if (!(thisObj == otherObj)) return false;
}
return true;
}
default: {
LVAL_FATAL_ERROR("compare", " Unbound type: " + type);
}
}
}
Lval& Lval::buildin_op(Lenv&, string op) {
for (Lvaldq::iterator it = cells->begin(); it != cells->end(); it++) {
if (LVAL_TYPE_NUM != (*it)->type) {
Lval& err = Lval::lval_err(
"Operation must be number! Expect %s, got %s!",
Lval::lval_type2name(LVAL_TYPE_NUM),
Lval::lval_type2name((*it)->type)
);
this->lval_delete();
return err;
}
}
Lval* x = &lval_pop(0);
// .e.g. `- 3` / `- 5`
if (cells->size() == 0 && "-" == op) x->num = -x->num;
while (cells->size()) {
Lval& y = lval_pop(0);
if ("+" == op) x->num += y.num;
else if ("-" == op) x->num -= y.num;
else if ("*" == op) x->num *= y.num;
else if ("/" == op) {
if (0 == y.num) {
x->lval_delete();
y.lval_delete();
x = &lval_err("Division by zero!");
break;
}
x->num /= y.num;
}
y.lval_delete();
}
lval_delete();
return *x;
}
Lval& Lval::buildin_head(Lenv& env, Lval& expr) {
LVAL_ASSERT_NUM("head", (expr), 1);
LVAL_ASSERT_TYPE("head", (expr), 0, LVAL_TYPE_QEXPR);
LVAL_ASSERT_NOT_EMPTY("head", (expr), 0);
Lval& node = expr.lval_take(0);
expr.lval_delete();
while (node.cells->size() > 1) {
node.lval_pop_back().lval_delete();
}
return node;
}
Lval& Lval::buildin_tail(Lenv& env, Lval& expr) {
LVAL_ASSERT_NUM("tail", (expr), 1);
LVAL_ASSERT_TYPE("tail", (expr), 0, LVAL_TYPE_QEXPR);
LVAL_ASSERT_NOT_EMPTY("tail", (expr), 0);
Lval& node = expr.lval_take(0);
expr.lval_delete();
if (node.cells->size() > 1) node.lval_pop_front().lval_delete();
return node;
}
Lval& Lval::buildin_list(Lenv& env, Lval& expr) {
expr.type = LVAL_TYPE_QEXPR;
return expr;
}
Lval& Lval::buildin_eval(Lenv& env, Lval& expr) {
LVAL_ASSERT_NUM("eval", (expr), 1);
LVAL_ASSERT_TYPE("eval", (expr), 0, LVAL_TYPE_QEXPR);
LVAL_ASSERT_NOT_EMPTY("eval", (expr), 0);
Lval& node = expr.lval_take(0);
expr.lval_delete();
node.type = LVAL_TYPE_SEXPR;
return node.lval_eval(env);
}
Lval& Lval::buildin_concat(Lenv& env, Lval& expr) {
LVAL_ASSERT_NUM("concat", (expr), 2);
LVAL_ASSERT_TYPE("concat", (expr), 0, LVAL_TYPE_QEXPR);
LVAL_ASSERT_TYPE("concat", (expr), 1, LVAL_TYPE_QEXPR);
Lval& qexprX = expr.lval_pop(0);
Lval& qexprY = expr.lval_pop(0);
expr.lval_delete();
while (qexprY.cells->size()) {
Lval& a = qexprY.lval_pop(0);
qexprX.lval_add(a);
}
qexprY.lval_delete();
return qexprX;
}
Lval& Lval::buildin_add(Lenv& env, Lval& expr) {
return expr.buildin_op(env, "+");
}
Lval& Lval::buildin_sub(Lenv& env, Lval& expr) {
return expr.buildin_op(env, "-");
}
Lval& Lval::buildin_mul(Lenv& env, Lval& expr) {
return expr.buildin_op(env, "*");
}
Lval& Lval::buildin_div(Lenv& env, Lval& expr) {
return expr.buildin_op(env, "/");
}
Lval& Lval::buildin_var(Lenv& env, Lval& expr, string op) {
LVAL_ASSERT_TYPE(op.c_str(), (expr), 0, LVAL_TYPE_QEXPR);
LVAL_ASSERT_NOT_EMPTY(op.c_str(), (expr), 0);
Lval* qexpr = expr.cells->at(0);
Lval::Lvaldq::iterator qit = qexpr->cells->begin();
for (int i = 0; i < qexpr->cells->size(); i++) {
Lval* node = *(qit+i);
if (LVAL_TYPE_SYM != node->type) {
Lval& err = Lval::lval_err("Function '%s' pass invalid type within args: %s. at index %d.", op.c_str(), Lval::lval_type2name(node->type), i);
expr.lval_delete();
return err;
}
}
LVAL_ASSERT((
qexpr->cells->size() == (expr.cells->size()-1)),
expr,
"Function '%s' pass count of args not equal to vals. Args: %d, Vals: %d.",
op.c_str(),
qexpr->cells->size(),
(expr.cells->size()-1));
qit = qexpr->cells->begin();
Lval::Lvaldq::iterator sit = ++expr.cells->begin();
for (int i = 0; i < qexpr->cells->size(); i++) {
Lval* keyVal = *(qit+i);
Lval& valVal = (*(sit+i))->lval_copy();
env.lenv_def(keyVal->symbol, valVal);
if ("def" == op) env.lenv_def(keyVal->symbol, valVal);
else env.lenv_put(keyVal->symbol, valVal);
}
expr.lval_delete();
return Lval::lval_sexpr();
}
Lval& Lval::buildin_put(Lenv& env, Lval& expr) {
return buildin_var(env, (expr), "=");
}
Lval& Lval::buildin_def(Lenv& env, Lval& expr) {
return buildin_var(env, (expr), "def");
}
Lval& Lval::buildin_lambda(Lenv& env, Lval& expr) {
LVAL_ASSERT_NUM("\\", expr, 2);
LVAL_ASSERT_TYPE("\\", expr, 0, LVAL_TYPE_QEXPR);
LVAL_ASSERT_TYPE("\\", expr, 1, LVAL_TYPE_QEXPR);
Lval* formals = expr.cells->at(0);
Lval* body = expr.cells->at(1);
Lval& lambda = Lval::lval_lambda(formals->lval_copy(), body->lval_copy());
expr.lval_delete();
return lambda;
}
Lval& Lval::buildin_order(Lenv& env, Lval& expr, const char* op) {
LVAL_ASSERT_NUM(op, expr, 2);
LVAL_ASSERT_TYPE(op, expr, 0, LVAL_TYPE_NUM);
LVAL_ASSERT_TYPE(op, expr, 1, LVAL_TYPE_NUM);
Lval* a = expr.cells->at(0);
Lval* b = expr.cells->at(1);
Lval* x;
if (">" == op) {
x = &Lval::lval_num(a->num > b->num ? 1 : 0);
} else if (">=" == op) {
x = &Lval::lval_num(a->num >= b->num ? 1 : 0);
} else if ("<" == op) {
x = &Lval::lval_num(a->num < b->num ? 1 : 0);
} else if ("<=" == op) {
x = &Lval::lval_num(a->num <= b->num ? 1 : 0);
}
expr.lval_delete();
return *x;
}
Lval& Lval::buildin_gt(Lenv& env, Lval& expr) {
return buildin_order(env, expr, ">");
}
Lval& Lval::buildin_gte(Lenv& env, Lval& expr) {
return buildin_order(env, expr, ">=");
}
Lval& Lval::buildin_lt(Lenv& env, Lval& expr) {
return buildin_order(env, expr, "<");
}
Lval& Lval::buildin_lte(Lenv& env, Lval& expr) {
return buildin_order(env, expr, "<=");
}
Lval& Lval::buildin_compare(Lenv& env, Lval& expr, const char* op) {
LVAL_ASSERT_NUM(op, expr, 2);
Lval* a = expr.cells->at(0);
Lval* b = expr.cells->at(1);
bool ret = *a == *b;
Lval* res;
if ("==" == op) {
res = &Lval::lval_num(ret);
} else if ("!=" == op) {
res = &Lval::lval_num(!ret);
} else {
LVAL_FATAL_ERROR(op, "Not found type: " + string(op));
}
expr.lval_delete();
return *res;
}
Lval& Lval::buildin_eq(Lenv& env, Lval& expr) {
return buildin_compare(env, expr, "==");
}
Lval& Lval::buildin_neq(Lenv& env, Lval& expr) {
return buildin_compare(env, expr, "!=");
}
Lval& Lval::buildin_if(Lenv& env, Lval& expr) {
LVAL_ASSERT_NUM("if", expr, 3);
LVAL_ASSERT_TYPE("if", expr, 0, LVAL_TYPE_NUM);
LVAL_ASSERT_TYPE("if", expr, 1, LVAL_TYPE_QEXPR);
LVAL_ASSERT_TYPE("if", expr, 2, LVAL_TYPE_QEXPR);
Lval& numVal = expr.lval_pop(0);
int num = numVal.num;
numVal.lval_delete();
expr.cells->at(0)->type = LVAL_TYPE_SEXPR;
expr.cells->at(1)->type = LVAL_TYPE_SEXPR;
Lval* res;
if (num == 0) {
res = &expr.lval_pop(1).lval_eval(env);
} else {
res = &expr.lval_pop(0).lval_eval(env);
}
expr.lval_delete();
return *res;
}
Lval& Lval::buildin_print(Lenv& env, Lval& expr) {
Lvaldq::iterator it = expr.cells->begin();
int cell_size = expr.cells->size();
for (int i = 0; i < cell_size; i++) {
Lval* aptr = *(it+i);
aptr->lval_print();
if (i != (cell_size-1)) {
putchar(' ');
}
}
cout << endl;
expr.lval_delete();
return lval_sexpr();
}
Lval& Lval::buildin_load(Lenv& env, Lval& expr) {
LVAL_ASSERT_NUM("load", expr, 1);
LVAL_ASSERT_TYPE("load", expr, 0, LVAL_TYPE_STR);
Lval* strVal = expr.cells->front();
AStruct& program = AKCompiler::loadfile(strVal->str);
expr.lval_delete();
if (Ast_Type_Error == program.type) {
Lval& err = Lval::lval_err(program.error);
program.deleteNode();
return err;
} else {
Lval& sexpr = Lval::readAst(program);
program.deleteNode();
Lval* expr;
Lval* res;
while (sexpr.cells->size()) {
expr = &sexpr.lval_pop_front();
res = &expr->lval_eval(env);
if (LVAL_TYPE_ERR == res->type) {
res->lval_print();
break;
}
res->lval_println();
res->lval_delete();
}
sexpr.lval_delete();
}
return Lval::lval_sexpr();
}
Lval& Lval::lval_call(Lenv& env, Lval& op) {
if (op.func) {
// funvVal.*func is a pointer that canot run property
// Declare afunc type is `Lval_Func`, to prevent call a function pointer and leet to Exception
Lval_Func afunc = op.func;
return (op.*afunc)(env, *this);
}
int args_count = op.formals->cells->size();
int params_count = cells->size();
while (cells->size()) {
if (!op.formals->cells->size()) {
this->lval_delete();
return Lval::lval_err("Function '%s' pass params too much to args. Params %d, Args %d", "call", params_count, args_count);
}
Lval* keyVal = &op.formals->lval_pop_front();
if ("&" == keyVal->symbol) {
if (1 != op.formals->cells->size()) {
this->lval_delete();
return Lval::lval_err("Function '%s' symbol '&' must follow only one symbol!", "call");
}
keyVal = &op.formals->lval_pop_front();
Lval& qexpr = Lval::lval_qexpr();
Lvaldq::iterator it = cells->begin();
while (it != cells->end()) {
qexpr.lval_add((*it)->lval_copy());
it++;
}
op.env->lenv_put(keyVal->symbol, qexpr);
break;
}
Lval& valVal = lval_pop_front();
op.env->lenv_put(keyVal->symbol, valVal.lval_copy());
}
this->lval_delete();
if ((2 == op.formals->cells->size()) && ("&" == op.formals->cells->at(0)->symbol)) {
op.formals->lval_pop(0).lval_delete();
Lval& keyVal = op.formals->lval_pop(0);
Lval& emptyList = Lval::lval_qexpr();
op.env->lenv_put(keyVal.symbol, emptyList);
}
// args had fully used
if (!op.formals->cells->size()) {
op.env->parent = &env;
Lval& sexpr = Lval::lval_sexpr();
sexpr.lval_add(op.body->lval_copy());
return buildin_eval(*op.env, sexpr);
}
// or return a copy
return op.lval_copy();
}
Lval& Lval::lval_expr_eval(Lenv& env) {
Lvaldq::iterator it;
for (it = cells->begin(); it != cells->end(); it++) {
Lval* node = *it;
*it = &(node->lval_eval(env));
}
it = cells->begin();
for (int i = 0 ; i < cells->size(); i++) {
Lval* node = *it;
if (LVAL_TYPE_ERR == node->type) {
Lval& node = lval_pop(i);
this->lval_delete();
return node;
}
it++;
}
if (!cells->size()) return *this;
if (cells->size() == 1) return lval_take(0);
Lval& funcVal = lval_pop(0);
if (LVAL_TYPE_FUNC != funcVal.type) {
Lval& err = lval_err(
"SExpr must start by Function type! Expect %s, got %s",
Lval::lval_type2name(LVAL_TYPE_FUNC),
Lval::lval_type2name(funcVal.type)
);
this->lval_delete();
funcVal.lval_delete();
return err;
}
Lval& res = lval_call(env, funcVal);
funcVal.lval_delete();
return res;
}
Lval& Lval::lval_eval(Lenv& env) {
if (LVAL_TYPE_SYM == type) {
Lval& res = env.lenv_get(*this);
this->lval_delete();
return res;
}
if (LVAL_TYPE_SEXPR == type) return lval_expr_eval(env);
return *this;
}
void Lval::lval_expr_print(char open, char close) {
putchar(open);
for (Lvaldq::iterator it = cells->begin(); it != cells->end();) {
Lval*node = *it;
node->lval_print();
it++;
if (it != cells->end()) putchar(' ');
}
putchar(close);
}
void Lval::lval_print() {
switch (type) {
case LVAL_TYPE_ERR: {
cout << error;
return;
}
case LVAL_TYPE_NUM: {
cout << num;
return;
}
case LVAL_TYPE_SYM: {
cout << symbol;
return;
}
case LVAL_TYPE_STR: {
cout << "\""<< str << "\"";
return;
}
case LVAL_TYPE_SEXPR: {
return lval_expr_print('(', ')');
}
case LVAL_TYPE_QEXPR: {
return lval_expr_print('{', '}');
}
case LVAL_TYPE_FUNC: {
if (func) {
cout << Lval::lval_type2name(LVAL_TYPE_FUNC) << ":" << &func;
} else {
putchar('\\');
putchar(' ');
putchar('{');
int i, size;
Lvaldq::iterator it;
i = 0; size = formals->cells->size(); it = formals->cells->begin();
for (; i < size; i++) {
(*(it + i))->lval_print();
if (i != (size-1)) {
putchar(' ');
}
}
putchar('}');
putchar(' ');
putchar('{');
i = 0; size = body->cells->size(); it = body->cells->begin();
for (i = 0; i < size; i++) {
(*(it + i))->lval_print();
if (i != (size-1)) {
putchar(' ');
}
}
putchar('}');
}
return;
}
}
}
void Lval::lval_println() {
lval_print();
cout << endl;
}
void Lval::lval_expr_delete() {
for (Lvaldq::iterator it = cells->begin(); it != cells->end(); it++) {
Lval* node = *it;
node->lval_delete();
}
cells->clear();
if (cells) delete cells; cells = NULL;
}
void Lval::lval_delete() {
switch (type) {
case LVAL_TYPE_QEXPR:
case LVAL_TYPE_SEXPR:
return lval_expr_delete();
case LVAL_TYPE_FUNC:
if (func) {
func = NULL;
} else {
if (formals) formals->lval_delete();
if (body) body->lval_delete();
if (env) env->lenv_delete();
}
break;
case LVAL_TYPE_ERR:
case LVAL_TYPE_NUM:
case LVAL_TYPE_SYM:
case LVAL_TYPE_STR:
break;
default:
break;
}
delete this;
}
#pragma mark - Static
void readline(string& input) {
getline(cin, input);
}
void Lval::quick_start(void) {
printf("LVAL VERSION: %d.%d\n", APP_VERSION_MAJOR, APP_VERSION_MINOR);
#ifdef COMPILER_LIB_MODE
Lenv& env = Lenv::New_Lenv();
env.init_buildins();
while (true) {
cout << "> ";
string input;
readline(input);
AStruct &program = AKCompiler::compiler(input);
if (Ast_Type_Error == program.type) {
program.print();
program.deleteNode();
continue;
}
Lval& expr = Lval::readAst(program);
program.deleteNode();
Lval& res = expr.lval_eval(env);
res.lval_println();
res.lval_delete();
}
env.lenv_delete();
#else
printf("not implement compiler yet!\n");
return 1;
#endif
}
Lval& Lval::readAst(AStruct& t) {
if (Ast_Type_Number == t.type) return Lval::lval_check_num(t.content);
if (Ast_Type_Symbol == t.type) return Lval::lval_sym(t.content);
if (Ast_Type_String == t.type) return Lval::lval_check_string(t.content);
Lval *x;
if (Ast_Type_Program == t.type) x = &Lval::lval_sexpr();
else if (Ast_Type_SExpr == t.type) x = &Lval::lval_sexpr();
else if (Ast_Type_QExpr == t.type) x = &Lval::lval_qexpr();
for (ASTptrVector::iterator it = t.children->begin(); it != t.children->end(); it++) {
AStruct *child_t = *it;
if (Ast_Type_Semi == child_t->type) continue;
if (Ast_type_Quote == child_t->type) continue;
if (Ast_Type_Comment == child_t->type) continue;
Lval& child = readAst(*child_t);
x->lval_add(child);
}
return *x;
}
Lval& Lval::lval_err(string fmt, ...) {
NewLval;
lval->type = LVAL_TYPE_ERR;
static char buf[255] = {'\0'};
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt.c_str(), ap);
va_end(ap);
lval->error = string(buf);
return *lval;
}
Lval& Lval::lval_sym(string sym) {
NewLval;
lval->type = LVAL_TYPE_SYM;
lval->symbol = sym;
return *lval;
}
Lval& Lval::lval_check_num(std::string numStr) {
errno = 0;
int num = strtol(numStr.c_str(), NULL, 10);
return errno > 0 ? lval_err("Invalid number: %s", strerror(errno)) : lval_num(num);
}
Lval& Lval::lval_num(int num) {
NewLval;
lval->type = LVAL_TYPE_NUM;
lval->num = num;
return *lval;
}
Lval& Lval::lval_sexpr(void) {
NewLval;
lval->type = LVAL_TYPE_SEXPR;
lval->cells = new Lvaldq;
return *lval;
}
Lval& Lval::lval_qexpr(void) {
Lval& lval = Lval::lval_sexpr();
lval.type = LVAL_TYPE_QEXPR;
return lval;
}
Lval& Lval::lval_func(Lval_Func func) {
NewLval;
lval->type = LVAL_TYPE_FUNC;
lval->func = func;
return *lval;
}
Lval& Lval::lval_lambda(Lval& formals, Lval& body) {
Lval& lval = lval_func(nullptr);
lval.formals = &formals;
lval.body = &body;
lval.env = &Lenv::New_Lenv();
return lval;
}
Lval& Lval::lval_check_string(const string str) {
return lval_string(str);
}
Lval& Lval::lval_string(const string str) {
NewLval;
lval->type = LVAL_TYPE_STR;
lval->str = str;
return *lval;
}
char* Lval::lval_type2name(LVAL_TYPE type) {
switch (type) {
case LVAL_TYPE_ERR: return "Error";
case LVAL_TYPE_NUM: return "Number";
case LVAL_TYPE_SYM: return "Symbol";
case LVAL_TYPE_SEXPR: return "SEXPR";
case LVAL_TYPE_QEXPR: return "QEXPR";
case LVAL_TYPE_FUNC: return "Function";
case LVAL_TYPE_STR: return "String";
default: "Unknown Type: " + type;
}
}