-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisp.cc
1153 lines (1005 loc) · 34.7 KB
/
lisp.cc
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <core/auto.h>
#include <core/exception.h>
#include <core/format.h>
#include <core/range.h>
#include <lisp.h>
static bool is_integer(string_view s) {
if (s.size() > 0 && (s[0] == '-' || s[0] == '+')) s = s.substr(1);
if (s.empty()) return false;
if (s[0] == '0') return s.size() == 1;
for (char c : s)
if (c < '0' || c > '9') return false;
return true;
}
static long parse_long(string_view s) {
cent a = 0;
int i = 0;
if (s[0] == '-') {
i += 1;
while (i < s.size()) {
char c = s[i];
a = a * 10 + c - '0';
i += 1;
}
a = -a;
} else {
if (s[0] == '+') i += 1;
while (i < s.size()) {
char c = s[i];
a = a * 10 + c - '0';
i += 1;
}
}
return a;
}
struct Node;
unordered_map<string_view, const Node*> g_keywords;
struct Context {
unordered_map<string_view, const Node*> symbols;
unordered_map<const Node*, const Node*> variables;
};
static Context* g_context = nullptr;
bool equal(const Node*, const Node*);
size_t hash(const Node*);
enum class Type : uint8_t { List, Keyword, Symbol, String, Map };
struct MapHash {
auto operator()(const Node* a) const { return hash(a); }
};
struct MapEqual {
bool operator()(const Node* a, const Node* b) const { return equal(a, b); }
};
using Map = unordered_map<const Node*, const Node*, MapHash, MapEqual>;
struct Node {
private:
Type _type;
uint _size = 0;
void* _data = nullptr; // TODO allocate memory inline
static Node* alloc_raw(uint size) {
Node* n = new Node;
n->_size = size;
n->_data = malloc(size);
return n;
}
static Node* alloc_list(uint size) {
Node* n = new Node;
n->_type = Type::List;
n->_size = size;
n->_data = malloc(size * 8);
return n;
}
public:
static const Node* keyword(string_view a) {
Node* n = alloc_raw(a.size());
n->_type = Type::Keyword;
memcpy(n->_data, a.data(), a.size());
g_keywords.insert({n->symbol(), n});
return n;
}
static const Node* symbol(string_view a) {
auto it = g_context->symbols.find(a);
if (it != g_context->symbols.end()) return it->second;
Node* n = alloc_raw(a.size());
n->_type = Type::Symbol;
memcpy(n->_data, a.data(), a.size());
g_context->symbols.insert({n->symbol(), n});
return n;
}
static const Node* integer(long a);
static const Node* str(string_view a) {
Node* n = alloc_raw(a.size());
n->_type = Type::String;
memcpy(n->_data, a.data(), a.size());
return n;
}
static const Node* list(string_view a, string_view b) {
auto n = alloc_list(2);
n->set(0, symbol(a));
n->set(1, str(b));
return n;
}
static const Node* list(string_view a, string_view b, const Node* c) {
auto n = alloc_list(3);
n->set(0, symbol(a));
n->set(1, str(b));
n->set(2, c);
return n;
}
static const Node* list(const Node* a, string_view b) {
auto n = alloc_list(2);
n->set(0, a);
n->set(1, str(b));
return n;
}
static const Node* list(const Node* a, const Node* b) {
auto n = alloc_list(2);
n->set(0, a);
n->set(1, b);
return n;
}
static const Node* list(const Node* a, string_view b, const Node* c) {
auto n = alloc_list(3);
n->set(0, a);
n->set(1, str(b));
n->set(2, c);
return n;
}
static const Node* list(span<const Node*> elements) {
auto n = alloc_list(elements.size());
// TODO memcpy
for (uint i : range(elements.size())) n->set(i, elements[i]);
return n;
}
static const Node* list(std::initializer_list<const Node*> elements) {
auto n = alloc_list(elements.size());
// TODO memcpy
for (uint i : range(elements.size())) n->set(i, elements.begin()[i]);
return n;
}
auto type() const { return _type; }
bool is_symbol() const { return _type == Type::Symbol; }
bool is_keyword() const { return _type == Type::Keyword; }
string_view symbol() const { return string_view((const char*)_data, _size); }
bool is_integer() const { return reinterpret_cast<long>(this) & 1; }
long integer() const { return reinterpret_cast<long>(this) >> 1; }
bool is_string() const { return _type == Type::String; }
string_view str() const { return string_view((const char*)_data, _size); }
bool is_list() const { return _type == Type::List; }
uint size() const { return _size; }
const Node* first() const { return *(const Node**)_data; }
auto elements() const { return span<const Node*>((const Node**)_data, _size); }
auto args() const {
const Node** data = (const Node**)_data;
return span<const Node*>(data + 1, _size - 1);
}
const Node* get(uint index) const { return ((const Node**)_data)[index]; }
void set(uint index, const Node* n) {
const Node** data = (const Node**)_data;
data[index] = n;
}
// Map
static const Node* make_map() {
Node* n = alloc_raw(sizeof(Map));
n->_type = Type::Map;
new (n->_data) Map();
return n;
}
bool is_map() const { return _type == Type::Map; }
const Map* map() const { return reinterpret_cast<const Map*>(_data); }
};
#define K(N, S) const Node* N = Node::keyword(#S);
K(NUMBER, #)
K(ERROR, error)
K(DEF, def)
K(TRUE, true)
K(FALSE, false)
K(AND, and)
K(OR, or)
K(NOT, not)
K(AMP, &)
K(PIPE, |)
K(CAPPA, ^)
K(TILDA, ~)
K(EQUAL, =)
K(PLUS, +)
K(PLUS_EQUAL, +=)
K(MINUS, -)
K(MINUS_EQUAL, -=)
K(STAR, *)
K(STAR_EQUAL, *=)
K(DIV, /)
K(DIV_EQUAL, /=)
K(PERCENT, %)
K(PERCENT_EQUAL, %=)
K(LESS_LESS, <<)
K(GREATER_GREATER, >>)
K(IF, if)
K(WHILE, while)
K(BREAK, break)
K(CONTINUE, continue)
K(LESS, <)
K(LESS_EQUAL, <=)
K(GREATER, >)
K(GREATER_EQUAL, >=)
K(EQUAL_EQUAL, ==)
K(NOT_EQUAL, !=)
K(EQUAL_EQUAL_EQUAL, == =)
K(PRINT, print)
K(PRINTLN, println)
K(PARSE, parse)
K(EVAL, eval)
K(SIZE, size)
K(MAP, map)
K(IN, in)
K(GET, get)
K(SET, set)
const Node* DIV_DIV = Node::keyword("//");
const Node* DIV_DIV_EQUAL = Node::keyword("//=");
const Node* EMPTY = Node::keyword("()");
const Node* QUOTE = Node::keyword("'");
#undef K
const Node* N_BREAK = Node::list(ERROR, BREAK);
const Node* N_CONTINUE = Node::list(ERROR, CONTINUE);
#define FAIL(...) return Node::list(ERROR, ##__VA_ARGS__)
const Node* Node::integer(long a) {
constexpr long inline_max = numeric_limits<long>::max() >> 1;
constexpr long inline_min = numeric_limits<long>::min() >> 1;
if (a < inline_min || a > inline_max) FAIL("integer overflow");
long v = (a << 1) | 1;
return reinterpret_cast<const Node*>(v);
}
static bool space(char c) { return c == ' ' || c == '\t' || c == '\n'; }
static const Node* token_to_node(string_view s) {
if (is_integer(s))
// TODO return error if s can't fit into inline long
return Node::integer(parse_long(s));
auto it = g_keywords.find(s);
if (it != g_keywords.end()) return it->second;
return Node::symbol(s);
}
// TODO line no and column for syntax error
static const Node* parse(string_view code) {
int p = 0;
vector<const Node*> stack = {};
vector<uint> size = {0};
while (p < code.size()) {
char c = code[p];
if (space(c)) {
p += 1;
continue;
}
if (c == '(') {
size.push_back(0);
p += 1;
continue;
}
if (c == ')') {
if (size.size() == 1) FAIL("syntax: unexpected ')'");
uint s = size.back();
size.pop_back();
const Node* n = (s == 0) ? EMPTY : Node::list(span<const Node*>(stack.data() + stack.size() - s, s));
stack.resize(stack.size() - s);
stack.push_back(n);
size.back() += 1;
p += 1;
continue;
}
if (c == '"') {
p += 1;
int s = p;
while (p < code.size() && code[p] != '"') p += 1;
if (p == code.size()) FAIL("syntax: unterminated quote");
const Node* n = Node::str(code.substr(s, p - s));
stack.push_back(n);
size.back() += 1;
p += 1;
continue;
}
// anything else is atom
int s = p;
while (p < code.size() && !space(code[p]) && code[p] != '(' && code[p] != ')') p += 1;
stack.push_back(token_to_node(code.substr(s, p - s)));
size.back() += 1;
}
if (size.size() != 1) FAIL("syntax: unterminated list");
if (stack.size() == 0) FAIL("syntax: empty");
if (stack.size() > 1) FAIL("syntax: only one top level expression expected");
return stack[0];
}
int length(const Node* n) {
ASSERT_ALWAYS(n != nullptr);
if (n->is_integer()) {
int s = 0;
long c = n->integer();
if (c <= 0) {
s += 1;
c = -c;
}
while (c > 0) {
s += 1;
c /= 10;
}
return s;
}
if (n->is_string()) {
int s = 0;
for (char c : n->str())
if (c == '"' || c == '\\') s += 1;
return s + 2 + n->str().size();
}
if (n->is_symbol() || n->is_keyword()) return n->symbol().size();
if (n->is_map()) {
int s = 0;
for (const auto& p : *n->map()) s += length(p.first) + length(p.second);
return s + 5 + n->map()->size() * 2;
}
ASSERT_ALWAYS(n->is_list());
if (n->size() == 0) return 2;
int s = 0;
for (const Node* e : n->elements()) s += length(e);
return s + 1 + n->size();
}
void format_e(string& s, string_view spec, const Node* n);
void format_map(string& s, string_view spec, const Map& m) {
s += "(map";
for (const auto& p : m) {
s += ' ';
format_e(s, spec, p.first);
s += ' ';
format_e(s, spec, p.second);
}
s += ')';
}
void format_list(string& s, string_view spec, const Node* n) {
if (length(n) <= 120) {
s += '(';
bool first = true;
for (const Node* e : n->elements()) {
if (first)
first = false;
else
s += ' ';
format_e(s, spec, e);
}
s += ')';
return;
}
static int s_depth = 0;
for (int i : range(s_depth * 2)) s += ' ';
s += "(\n";
s_depth += 1;
for (const Node* e : n->elements()) {
ASSERT_ALWAYS(e != nullptr);
if (!e->is_list() || length(e) <= 120) {
for (int i : range(s_depth * 2)) s += ' ';
format_e(s, spec, e);
} else {
format_e(s, spec, e);
}
s += '\n';
}
s_depth -= 1;
for (int i : range(s_depth * 2)) s += ' ';
s += ")";
}
void format_e(string& s, string_view spec, const Node* n) {
ASSERT_ALWAYS(n != nullptr);
if (n->is_integer()) {
format_e(s, "%s", n->integer());
return;
}
switch (n->type()) {
case Type::Symbol:
case Type::Keyword:
s += n->symbol();
return;
case Type::String:
s += '"';
for (char c : n->str()) {
if (c == '"' || c == '\\') s += '\\';
s += c;
}
s += '"';
return;
case Type::Map:
format_map(s, spec, *n->map());
return;
case Type::List:
format_list(s, spec, n);
return;
}
}
static bool is_error(const Node* e) { return e->is_list() && e->size() > 0 && e->elements()[0] == ERROR; }
static const Node* eval(const Node* n);
const Node* eval_plus(span<const Node*> args) {
if (args.size() == 2) {
const Node* a = eval(args[0]);
const Node* b = eval(args[1]);
if (a->is_integer() && b->is_integer()) {
long av = a->integer();
long bv = b->integer();
if (add_overflow(av, bv)) return Node::integer(cent(av) + cent(bv));
return Node::integer(av + bv);
}
if (!a->is_integer() && is_error(a)) return a;
if (!b->is_integer() && is_error(b)) return b;
}
if (args.size() == 0) FAIL("(+) min 1 arg");
if (args.size() == 1) return eval(args[0]);
cent acc = 0;
for (const Node* e : args) {
e = eval(e);
if (!e->is_integer()) {
if (is_error(e)) return e;
FAIL("(+) arg not int", e);
}
acc += e->integer();
}
return Node::integer(acc);
}
const Node* eval_plus_equal(span<const Node*> args) {
if (args.size() != 2) FAIL("(+=) exactly 2 args");
const Node* a = args[0];
if (a->is_integer() || !a->is_symbol()) FAIL("(+=) lhs must be symbol", a);
auto it = g_context->variables.find(a);
if (it == g_context->variables.end()) FAIL("(+=) symbol not defined", a);
auto& av = it->second;
const Node* b = eval(args[1]);
if (!b->is_integer()) {
if (is_error(b)) return b;
FAIL("(+=) arg not int", b);
}
b = Node::integer(av->integer() + b->integer());
av = b;
return b;
}
const Node* eval_minus(span<const Node*> args) {
if (args.size() == 1) {
auto a = eval(args[0]);
if (!a->is_integer()) {
if (is_error(a)) return a;
FAIL("(-) arg not int", a);
}
return Node::integer(-a->integer());
}
if (args.size() == 2) {
auto a = eval(args[0]);
if (!a->is_integer()) {
if (is_error(a)) return a;
FAIL("(-) arg not int", a);
}
auto b = eval(args[1]);
if (!b->is_integer()) {
if (is_error(b)) return b;
FAIL("(-) arg not int", b);
}
return Node::integer(a->integer() - b->integer());
}
FAIL("(-) exactly 1 or 2 args");
}
const Node* eval_minus_equal(span<const Node*> args) {
if (args.size() != 2) FAIL("(-=) exactly 2 args");
const Node* a = args[0];
if (!a->is_symbol()) FAIL("(-=) lhs must be symbol", a);
auto it = g_context->variables.find(a);
if (it == g_context->variables.end()) FAIL("(+=) symbol not defined", a);
auto& av = it->second;
const Node* b = eval(args[1]);
if (is_error(b)) return b;
if (!b->is_integer()) FAIL("(-=) arg not int", b);
b = Node::integer(av->integer() - b->integer());
av = b;
return b;
}
const Node* eval_not(span<const Node*> args) {
if (args.size() != 1) FAIL("(not) exactly 1 arg");
auto a = eval(args[0]);
if (!a->is_integer() && is_error(a)) return a;
if (a == TRUE) return FALSE;
if (a == FALSE) return TRUE;
FAIL("(not) arg not bool", a);
}
const Node* eval_and(span<const Node*> args) {
if (args.size() == 0) FAIL("(and) min 1 arg");
for (auto e : args) {
e = eval(e);
if (!e->is_integer() && is_error(e)) return e;
if (e == FALSE) return FALSE;
if (e != TRUE) FAIL("(and) arg not bool", e);
}
return TRUE;
}
const Node* eval_or(span<const Node*> args) {
if (args.size() == 0) FAIL("(or) min 1 arg");
for (auto e : args) {
e = eval(e);
if (!e->is_integer() && is_error(e)) return e;
if (e == TRUE) return TRUE;
if (e != FALSE) FAIL("(or) arg not bool", e);
}
return FALSE;
}
const Node* eval_star(span<const Node*> args) {
if (args.size() < 2) FAIL("(*) min 2 args");
cent acc = 1;
for (auto e : args) {
e = eval(e);
if (is_error(e)) return e;
if (!e->is_integer()) FAIL("(*) arg not int", e);
acc *= e->integer();
}
return Node::integer(acc);
}
const Node* eval_star_equal(span<const Node*> args) {
if (args.size() != 2) FAIL("(*=) exactly 2 args");
const Node* a = args[0];
if (!a->is_symbol()) FAIL("(*=) lhs must be symbol", a);
auto it = g_context->variables.find(a);
if (it == g_context->variables.end()) FAIL("(*=) symbol not defined", a);
auto& av = it->second;
const Node* b = eval(args[1]);
if (is_error(b)) return b;
if (!b->is_integer()) FAIL("(*=) arg not int", b);
b = Node::integer(av->integer() * b->integer());
av = b;
return b;
}
const Node* eval_div_div(span<const Node*> args) {
if (args.size() != 2) FAIL("(//) exactly 2 args");
auto a = eval(args[0]);
if (is_error(a)) return a;
if (!a->is_integer()) FAIL("(//) arg not int", a);
auto b = eval(args[1]);
if (is_error(b)) return b;
if (!b->is_integer()) FAIL("(//) arg not int", b);
return Node::integer(a->integer() / b->integer());
}
const Node* eval_div_div_equal(span<const Node*> args) {
if (args.size() != 2) FAIL("(//=) exactly 2 args");
const Node* a = args[0];
if (!a->is_symbol()) FAIL("(//=) lhs must be symbol", a);
auto it = g_context->variables.find(a);
if (it == g_context->variables.end()) FAIL("(//=) symbol not defined", a);
auto& av = it->second;
const Node* b = eval(args[1]);
if (is_error(b)) return b;
if (!b->is_integer()) FAIL("(//=) arg not int", b);
b = Node::integer(av->integer() / b->integer());
av = b;
return b;
}
const Node* eval_percent(span<const Node*> args) {
if (args.size() != 2) FAIL("(%) exactly 2 args");
auto a = eval(args[0]);
if (is_error(a)) return a;
if (!a->is_integer()) FAIL("(%) arg not int", a);
auto b = eval(args[1]);
if (is_error(b)) return b;
if (!b->is_integer()) FAIL("(%) arg not int", b);
return Node::integer(a->integer() % b->integer());
}
const Node* eval_percent_equal(span<const Node*> args) {
if (args.size() != 2) FAIL("(%=) exactly 2 args");
const Node* a = args[0];
if (!a->is_symbol()) FAIL("(%=) lhs must be symbol", a);
auto it = g_context->variables.find(a);
if (it == g_context->variables.end()) FAIL("(%=) symbol not defined", a);
auto& av = it->second;
const Node* b = eval(args[1]);
if (is_error(b)) return b;
if (!b->is_integer()) FAIL("(%=) arg not int", b);
b = Node::integer(av->integer() % b->integer());
av = b;
return b;
}
const Node* eval_less_less(span<const Node*> args) {
if (args.size() != 2) FAIL("(<<) exactly 2 args");
auto a = eval(args[0]);
if (is_error(a)) return a;
if (!a->is_integer()) FAIL("(<<) arg not int", a);
auto b = eval(args[1]);
if (is_error(b)) return b;
if (!b->is_integer()) FAIL("(<<) arg not int", b);
return Node::integer(a->integer() << b->integer());
}
const Node* eval_greater_greater(span<const Node*> args) {
if (args.size() != 2) FAIL("(>>) exactly 2 args");
auto a = eval(args[0]);
if (is_error(a)) return a;
if (!a->is_integer()) FAIL("(>>) arg not int", a);
auto b = eval(args[1]);
if (is_error(b)) return b;
if (!b->is_integer()) FAIL("(>>) arg not int", b);
return Node::integer(a->integer() >> b->integer());
}
#define COMPARE(OP) \
if (args.size() != 2) FAIL("(" #OP ") exactly 2 args"); \
const Node* a = eval(args[0]); \
const Node* b = eval(args[1]); \
if (a->is_integer() && b->is_integer()) return a->integer() OP b->integer() ? TRUE : FALSE; \
if (!a->is_integer()) { \
if (is_error(a)) return a; \
FAIL("(" #OP ") arg not int", a); \
} \
if (!b->is_integer()) { \
if (is_error(b)) return b; \
FAIL("(" #OP ") arg not int", b); \
} \
THROW(runtime_error);
const Node* eval_less(span<const Node*> args) { COMPARE(<) }
const Node* eval_less_equal(span<const Node*> args) { COMPARE(<=) }
const Node* eval_greater(span<const Node*> args) { COMPARE(>) }
const Node* eval_greater_equal(span<const Node*> args) { COMPARE(>=) }
static bool raw_equal(const Node* a, const Node* b);
size_t hash(const Node* a) {
if (a->is_integer()) return a->integer();
size_t h = 0;
switch (a->type()) {
case Type::String:
return a->str().size(); // TODO horible!
case Type::Symbol:
case Type::Keyword:
return reinterpret_cast<size_t>(a);
case Type::Map:
for (const auto& p : *a->map()) h += hash(p.first) + hash(p.second);
return h;
case Type::List:
for (auto e : a->elements()) h += hash(e);
return h; // TODO horible!
}
}
bool equal(const Node* a, const Node* b) {
if (a == b) return true;
if (a->is_integer() || b->is_integer()) return false;
return raw_equal(a, b);
}
static bool maps_equal(const Map& a, const Map& b) {
if (a.size() != b.size()) return false;
for (const auto& p : a) {
auto q = b.find(p.first);
if (q == b.end() || !equal(p.second, q->second)) return false;
}
return true;
}
// assumes a and b are not integers
static bool raw_equal(const Node* a, const Node* b) {
if (a->type() != b->type()) return false;
switch (a->type()) {
case Type::String:
return a->str() == b->str();
case Type::Symbol:
return a == b;
case Type::Keyword:
return a == b;
case Type::Map:
return maps_equal(*a->map(), *b->map());
case Type::List:
if (a->size() != b->size()) return false;
for (auto i : range(a->size()))
if (!equal(a->get(i), b->get(i))) return false;
return true;
}
THROW(runtime_error);
}
const Node* eval_equal_equal(span<const Node*> args) {
if (args.size() != 2) FAIL("(==) exactly 2 args");
auto a = eval(args[0]);
if (a->is_integer()) {
auto b = eval(args[1]);
if (b->is_integer()) return a == b ? TRUE : FALSE;
if (is_error(b)) return b;
return FALSE;
} else {
if (is_error(a)) return a;
auto b = eval(args[1]);
if (b->is_integer()) return FALSE;
return raw_equal(a, b) ? TRUE : FALSE;
}
}
const Node* eval_not_equal(span<const Node*> args) {
if (args.size() != 2) FAIL("(!=) exactly 2 args");
auto a = eval(args[0]);
if (a->is_integer()) {
auto b = eval(args[1]);
if (b->is_integer()) return a != b ? TRUE : FALSE;
if (is_error(b)) return b;
return FALSE;
} else {
if (is_error(a)) return a;
auto b = eval(args[1]);
if (b->is_integer()) return FALSE;
return !raw_equal(a, b) ? TRUE : FALSE;
}
}
const Node* eval_print(span<const Node*> args, bool endl) {
for (auto e : args) {
e = eval(e);
if (e->is_integer()) {
print("%s", e->integer());
continue;
}
if (is_error(e)) return e;
if (e->is_string())
print("%s", e->str());
else
print("%s", e);
}
if (endl) print("\n");
return EMPTY;
}
const Node* eval_if(span<const Node*> args) {
if (args.size() != 2 && args.size() != 3) FAIL("(if) exactly 2 or 3 args");
const Node* a = eval(args[0]);
if (a == TRUE) return eval(args[1]);
if (a == FALSE) {
if (args.size() == 3) return eval(args[2]);
return EMPTY;
}
if (!a->is_integer() && is_error(a)) return a;
FAIL("(if) condition not bool", a);
}
// TODO return last expression from while's body
const Node* eval_while(span<const Node*> args) {
if (args.size() < 1) FAIL("(while) min 1 arg");
auto cond = args[0];
auto body = args.subspan(1);
while (true) {
const Node* a = eval(cond);
if (a == FALSE) return EMPTY;
if (a != TRUE) {
if (!a->is_integer() && is_error(a)) return a;
FAIL("(while) condition not bool", a);
}
// execute body
for (auto e : body) {
e = eval(e);
if (e == N_BREAK) return EMPTY;
if (e == N_CONTINUE) break;
if (!e->is_integer() && is_error(e)) return e;
}
}
}
const Node* eval_equal(span<const Node*> args) {
if (args.size() != 2) FAIL("(=) exactly 2 args");
const Node* a = args[0];
if (a->is_integer() || !a->is_symbol()) FAIL("(=) lhs must be symbol", a);
const Node* b = eval(args[1]);
if (!b->is_integer() && is_error(b)) return b;
g_context->variables[a] = b;
return b;
}
const Node* eval_def(const Node* n) {
auto args = n->args();
if (args.size() < 2) FAIL("(def) min 2 args");
const Node* name = args[0];
if (!name->is_symbol()) FAIL("(def) function name must be symbol", name);
if (g_context->variables.count(name) != 0) FAIL("(def) function already defined", name);
const Node* f_args = args[1];
if (!f_args->is_list()) FAIL("(def) function args must be list of symbols", f_args);
for (auto e : f_args->elements())
if (!e->is_symbol()) FAIL("(def) function arg must be symbol", e);
g_context->variables[name] = n; // TODO convert to lambda instead
return n;
}
const Node* eval_invoke(const Node* def, span<const Node*> args) {
print("eval_invoke!\n");
// TODO check that number of args match the definition
// install args as variables
// execute function body
// (check for early return)
// restore variables that were overwritten by args
// return function result
return EMPTY;
}
static const Node* eval(const Node* n) {
if (n->is_integer() || n->is_string() || n == TRUE || n == FALSE || n == EMPTY) return n;
if (n->is_keyword()) {
if (n == BREAK) return N_BREAK;
if (n == CONTINUE) return N_CONTINUE;
FAIL("can't eval keyword", n);
}
if (n->is_symbol()) {
auto it = g_context->variables.find(n);
if (it != g_context->variables.end()) return it->second;
FAIL("undefined symbol", n);
}
ASSERT_ALWAYS(n->is_list());
auto op = n->first();
if (op->is_integer() || op->is_string() || op == TRUE || op == FALSE || op == EMPTY) return n;
// TODO make these symbols into constexpr to be able to use switch
auto args = n->args();
if (op == EQUAL) return eval_equal(args);
if (op == NOT) return eval_not(args);
if (op == AND) return eval_and(args);
if (op == OR) return eval_or(args);
if (op == IF) return eval_if(args);
if (op == WHILE) return eval_while(n->args());
if (op == PLUS) return eval_plus(n->args());
if (op == PLUS_EQUAL) return eval_plus_equal(n->args());
if (op == MINUS) return eval_minus(n->args());
if (op == MINUS_EQUAL) return eval_minus_equal(n->args());
if (op == STAR) return eval_star(n->args());
if (op == STAR_EQUAL) return eval_star_equal(n->args());
if (op == DIV_DIV) return eval_div_div(n->args());
if (op == DIV_DIV_EQUAL) return eval_div_div_equal(n->args());
if (op == PERCENT) return eval_percent(n->args());
if (op == PERCENT_EQUAL) return eval_percent_equal(n->args());
if (op == LESS_LESS) return eval_less_less(n->args());
if (op == GREATER_GREATER) return eval_greater_greater(n->args());
if (op == LESS) return eval_less(n->args());
if (op == LESS_EQUAL) return eval_less_equal(n->args());
if (op == GREATER) return eval_greater(n->args());
if (op == GREATER_EQUAL) return eval_greater_equal(n->args());
if (op == EQUAL_EQUAL) return eval_equal_equal(n->args());
if (op == NOT_EQUAL) return eval_not_equal(n->args());
if (op == PRINT) return eval_print(n->args(), false);
if (op == PRINTLN) return eval_print(n->args(), true);
if (op == DEF) return eval_def(n);
if (op == NUMBER) return EMPTY;
if (op == PARSE) {
if (args.size() != 1) FAIL("(parse) exactly 1 arg");
auto a = eval(args[0]);
if (a->is_integer()) FAIL("(parse) arg not string", a);
if (is_error(a)) return a;
return parse(a->str());
}
if (op == EVAL) {
if (args.size() != 1) FAIL("(eval) exactly 1 arg");
auto a = eval(args[0]);
if (a->is_integer() || is_error(a)) return a;
return eval(a);
}
if (op == QUOTE) {
if (args.size() == 0) return EMPTY;
if (args.size() == 1) return args[0];
return Node::list(args);
}
if (op == ERROR) return n;
if (op->is_list()) {
auto res = EMPTY;
for (auto e : n->elements()) {
auto a = eval(e);
if (!a->is_integer() && is_error(a)) return a;
res = a;
}
return res;
}
if (op == EQUAL_EQUAL_EQUAL) {
if (args.size() != 2) FAIL("(===) exactly 2 args");
auto a = eval(args[0]);
if (!a->is_integer() && is_error(a)) return a;
auto b = eval(args[1]);
if (!b->is_integer() && is_error(b)) return b;
return a == b ? TRUE : FALSE;
}
if (op == SIZE) {
if (args.size() != 1) FAIL("(size) exactly 1 arg");
auto a = eval(args[0]);
if (a->is_integer()) FAIL("(size) arg not string, symbol or list", a);
if (is_error(a)) return a;
if (a->is_string()) return Node::integer(a->str().size());
if (a->is_map()) return Node::integer(a->map()->size());
if (a->is_symbol()) return Node::integer(a->symbol().size());
if (a == EMPTY) return Node::integer(0);
if (a->is_list()) return Node::integer(a->size());
FAIL("(size) arg not string, symbol, list or map", a);
}
if (op == MAP) {
if (args.size() % 2 != 0) FAIL("(map) even numbers of args");
auto n = Node::make_map();
auto& m = *const_cast<Map*>(n->map());