-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
1479 lines (1269 loc) · 36.5 KB
/
parse.c
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
// (c) 2020 shdown
// This code is licensed under MIT license (see LICENSE.MIT for details)
#include "parse.h"
#include "lexer.h"
#include "dasm.h"
#include "xht.h"
#include "hash.h"
#include "number.h"
#include "str.h"
#define SWAP(X_, Y_) \
do { \
__typeof__(X_) swap_tmp_ = (X_); \
(X_) = (Y_); \
(Y_) = swap_tmp_; \
} while (0)
// Stacks
enum {
S_IF,
S_BREAK,
S_CONTINUE,
S__LAST,
};
typedef struct {
Instr instr;
size_t line;
} TaggedInstr;
typedef struct {
TaggedInstr *data;
size_t size;
size_t capacity;
} Program;
typedef struct {
size_t *data;
size_t size;
size_t capacity;
} Stack;
static inline void stack_push(Stack *x, size_t v)
{
if (x->size == x->capacity) {
x->data = uu_x2realloc(x->data, &x->capacity, sizeof(size_t));
}
x->data[x->size++] = v;
}
static inline size_t stack_peek(Stack *x)
{
return x->data[x->size - 1];
}
static inline size_t stack_pop(Stack *x)
{
return x->data[--x->size];
}
typedef struct {
size_t scope_index;
size_t begin;
size_t end;
} Range;
typedef struct {
Range *data;
size_t size;
size_t capacity;
} RangeStack;
typedef struct {
xHt *data;
size_t size;
size_t capacity;
} ScopeStack;
typedef struct {
Value *data;
size_t size;
size_t capacity;
} ConstList;
typedef struct {
Shape *data;
size_t size;
size_t capacity;
} ShapeList;
typedef struct {
const char *start;
size_t size;
} Ident;
typedef struct {
Ident *data;
size_t size;
size_t capacity;
} IdentList;
typedef struct {
Lexer *lexer;
State *state;
Lexeme cur;
Program prog;
ConstList consts;
ShapeList shapes;
ScopeStack scopes;
RangeStack ranges;
IdentList idents;
Stack stacks[S__LAST];
ParseError err;
jmp_buf err_handler;
} Parser;
static Parser *parser_new(const char *source, size_t nsource, State *state)
{
Parser *p = uu_xmalloc(sizeof(Parser), 1);
*p = (Parser) {
.lexer = lexer_new(source, nsource),
.state = state,
.prog = {NULL, 0, 0},
.consts = {NULL, 0, 0},
.shapes = {NULL, 0, 0},
.scopes = {NULL, 0, 0},
.ranges = {NULL, 0, 0},
.idents = {NULL, 0, 0},
.stacks = {[0 ... S__LAST - 1] = {NULL, 0, 0}},
.err = {.msg = NULL},
};
return p;
}
static void parser_destroy(Parser *p)
{
lexer_destroy(p->lexer);
free(p->prog.data);
for (size_t i = 0; i < p->consts.size; ++i)
value_unref(p->consts.data[i]);
free(p->consts.data);
free(p->shapes.data);
for (size_t i = 0; i < p->scopes.size; ++i)
xht_destroy(&p->scopes.data[i]);
free(p->scopes.data);
free(p->ranges.data);
free(p->idents.data);
for (int i = 0; i < S__LAST; ++i)
free(p->stacks[i].data);
free(p->err.msg);
free(p);
}
static __attribute__((noreturn))
void throw_error(Parser *p, const char *msg)
{
p->err = (ParseError) {
.size = -1,
.msg = uu_xstrdup(msg),
.need_more = false,
};
longjmp(p->err_handler, 1);
}
static __attribute__((noreturn))
void throw_error_at(Parser *p, const char *msg, Lexeme at)
{
p->err = (ParseError) {
.pos = at.pos,
.size = at.size,
.msg = uu_xstrdup(msg),
.need_more = (at.kind == LK_EOF),
};
longjmp(p->err_handler, 1);
}
static __attribute__((noreturn))
void throw_error_precise(Parser *p, const char *msg, Position pos, size_t size)
{
p->err = (ParseError) {
.pos = pos,
.size = size,
.msg = uu_xstrdup(msg),
.need_more = false,
};
longjmp(p->err_handler, 1);
}
static uint32_t add_shape(Parser *p)
{
ShapeList *x = &p->shapes;
if (x->size == x->capacity) {
x->data = uu_x2realloc(x->data, &x->capacity, sizeof(Shape));
}
uint32_t i = x->size++;
if (UU_UNLIKELY(i == UINT32_MAX)) {
throw_error(p, "too many functions");
}
x->data[i] = (Shape) {0};
return i;
}
static uint32_t add_const_steal(Parser *p, Value v)
{
ConstList *x = &p->consts;
if (x->size == x->capacity) {
x->data = uu_x2realloc(x->data, &x->capacity, sizeof(Value));
}
uint32_t i = x->size++;
if (UU_UNLIKELY(i == UINT32_MAX)) {
throw_error(p, "too many constants");
}
x->data[i] = v;
return i;
}
static inline void push_range(Parser *p, Range v)
{
RangeStack *x = &p->ranges;
if (x->size == x->capacity) {
x->data = uu_x2realloc(x->data, &x->capacity, sizeof(Range));
}
x->data[x->size++] = v;
}
static inline void emit_at_line(Parser *p, Instr instr, size_t line)
{
Program *x = &p->prog;
if (x->size == x->capacity) {
x->data = uu_x2realloc(x->data, &x->capacity, sizeof(TaggedInstr));
}
x->data[x->size++] = (TaggedInstr) {instr, line};
}
static inline Instr unemit(Parser *p)
{
Program *x = &p->prog;
TaggedInstr ti = x->data[--x->size];
return ti.instr;
}
static inline void emit(Parser *p, Instr instr)
{
emit_at_line(p, instr, -1);
}
static inline void emit_at(Parser *p, Instr instr, Lexeme at)
{
emit_at_line(p, instr, at.pos.line);
}
static inline void advance(Parser *p)
{
p->cur = lexer_next(p->lexer);
if (UU_UNLIKELY(p->cur.kind == LK_ERROR))
throw_error_at(p, lexer_error_msg(p->lexer), p->cur);
}
static inline void slurp(Parser *p, LexemeKind k, const char *err_msg)
{
if (UU_UNLIKELY(p->cur.kind != k))
throw_error_at(p, err_msg, p->cur);
advance(p);
}
static inline size_t here(Parser *p)
{
return p->prog.size;
}
static inline void fixup_last_range_end(Parser *p, size_t new_end)
{
Range *range = &p->ranges.data[p->ranges.size - 1];
range->end = new_end;
}
static inline Instr load(Parser *p, Lexeme ident)
{
IdentList *x = &p->idents;
if (x->size == x->capacity) {
x->data = uu_x2realloc(x->data, &x->capacity, sizeof(Ident));
}
uint32_t i = x->size++;
if (UU_UNLIKELY(i == UINT32_MAX)) {
throw_error(p, "too many identifiers");
}
x->data[i] = (Ident) {ident.start, ident.size};
return (Instr) {OP_LOAD_SYMBOLIC, 0, 0, i};
}
static Instr load_to_store(Parser *p, Instr instr, bool local, Lexeme scapegoat)
{
switch (instr.opcode) {
case OP_LOAD_SYMBOLIC:
if (local) {
Ident ident = p->idents.data[instr.c];
xHt *locals = &p->scopes.data[p->scopes.size - 1];
uint32_t idx = *xht_put_int(
locals,
ident.start, ident.size, hash_str(ident.start, ident.size),
xht_size(locals));
return (Instr) {OP_STORE_LOCAL, 0, 0, idx};
} else {
return (Instr) {OP_STORE_SYMBOLIC, 0, 0, instr.c};
}
case OP_LOAD_AT:
if (UU_UNLIKELY(local))
goto error;
return (Instr) {OP_STORE_AT, 0, 0, 0};
default:
goto error;
}
error:
throw_error_at(p, "invalid assignment", scapegoat);
}
static Instr load_to_modify(Parser *p, Instr instr, uint8_t aop, Lexeme scapegoat)
{
switch (instr.opcode) {
case OP_LOAD_SYMBOLIC:
return (Instr) {OP_MODIFY_SYMBOLIC, aop, 0, instr.c};
case OP_LOAD_AT:
return (Instr) {OP_MODIFY_AT, aop, 0, 0};
default:
throw_error_at(p, "invalid compound assignment", scapegoat);
}
}
static void open_scope(Parser *p)
{
size_t pos = here(p);
if (p->ranges.size)
fixup_last_range_end(p, pos);
ScopeStack *x = &p->scopes;
if (x->size == x->capacity) {
x->data = uu_x2realloc(x->data, &x->capacity, sizeof(xHt));
}
size_t i = x->size++;
x->data[i] = xht_new(0);
Range range = {i, pos, -1};
push_range(p, range);
}
static inline Instr resolve_symbolic(Parser *p, xHt *locals, Instr instr, uint8_t op_local, uint8_t op_global)
{
Ident ident = p->idents.data[instr.c];
uint32_t local_idx = xht_get_int(
locals,
ident.start, ident.size, hash_str(ident.start, ident.size),
-1);
if (local_idx != (uint32_t) -1) {
return (Instr) {op_local, instr.a, 0, local_idx};
}
uint32_t global_idx = state_intern_global(p->state, ident.start, ident.size);
return (Instr) {op_global, instr.a, 0, global_idx};
}
static inline size_t nranges_for_scope_index(RangeStack *x, size_t scope_index)
{
size_t n = x->size;
while (n && x->data[n - 1].scope_index == scope_index)
--n;
return x->size - n;
}
static void close_scope(Parser *p, size_t *out_maxstack, uint32_t *out_nlocals)
{
size_t pos = here(p);
fixup_last_range_end(p, pos);
size_t scope_idx = p->scopes.size - 1;
xHt *locals = &p->scopes.data[scope_idx];
static const int8_t actions[] = {
[OP_LOAD_CONST] = 1,
[OP_LOAD_LOCAL] = 1,
[OP_LOAD_AT] = -1,
[OP_LOAD_GLOBAL] = 1,
[OP_MODIFY_LOCAL] = -1,
[OP_MODIFY_AT] = -3,
[OP_MODIFY_GLOBAL] = -1,
[OP_STORE_LOCAL] = -1,
[OP_STORE_AT] = -3,
[OP_STORE_GLOBAL] = -1,
[OP_PRINT] = -1,
[OP_RETURN] = -1,
[OP_JUMP] = 0,
[OP_JUMP_UNLESS] = -1,
[OP_CALL] = 0,
[OP_FUNCTION] = 1,
[OP_NEG] = 0,
[OP_NOT] = 0,
[OP_AOP] = -1,
[OP_CMP_2WAY] = -1,
[OP_CMP_3WAY] = -1,
[OP_LIST] = 1,
[OP_DICT] = 1,
[OP_LEN] = 0,
[OP_LOAD_SYMBOLIC] = 1,
[OP_MODIFY_SYMBOLIC] = -1,
[OP_STORE_SYMBOLIC] = -1,
};
size_t curstack = 0;
size_t maxstack = 0;
TaggedInstr *code = p->prog.data;
size_t nranges_ours = nranges_for_scope_index(&p->ranges, scope_idx);
for (size_t i = p->ranges.size - nranges_ours; i < p->ranges.size; ++i) {
Range range = p->ranges.data[i];
for (size_t j = range.begin; j < range.end; ++j) {
Instr instr = code[j].instr;
curstack += actions[instr.opcode];
switch (instr.opcode) {
case OP_LOAD_SYMBOLIC:
code[j].instr = resolve_symbolic(p, locals, instr, OP_LOAD_LOCAL, OP_LOAD_GLOBAL);
break;
case OP_STORE_SYMBOLIC:
code[j].instr = resolve_symbolic(p, locals, instr, OP_STORE_LOCAL, OP_STORE_GLOBAL);
break;
case OP_MODIFY_SYMBOLIC:
code[j].instr = resolve_symbolic(p, locals, instr, OP_MODIFY_LOCAL, OP_MODIFY_GLOBAL);
break;
case OP_CALL:
case OP_LIST:
curstack -= instr.c;
break;
case OP_DICT:
curstack -= 2 * (size_t) instr.c;
break;
}
if (maxstack < curstack) {
maxstack = curstack;
}
}
}
p->ranges.size -= nranges_ours;
uint32_t nlocals = xht_size(locals);
if (UU_UNLIKELY(maxstack > SIZE_MAX / 2))
throw_error(p, "program is too big");
if (UU_UNLIKELY(nlocals > UINT32_MAX / 2))
throw_error(p, "too many locals");
xht_destroy(locals);
--p->scopes.size;
if (p->scopes.size) {
Range range = {p->scopes.size - 1, pos, -1};
push_range(p, range);
}
*out_maxstack = maxstack;
*out_nlocals = nlocals;
}
static inline Shape *fun_shape_ptr(Parser *p, size_t begin_pos)
{
Instr instr = p->prog.data[begin_pos].instr;
assert(instr.opcode == OP_FUNCTION);
return &p->shapes.data[instr.c];
}
static size_t fun_begin(Parser *p)
{
size_t pos = here(p);
uint32_t shape_idx = add_shape(p);
Instr instr = {OP_FUNCTION, 0, 0, shape_idx};
emit(p, instr);
open_scope(p);
for (int i = 0; i < S__LAST; ++i)
stack_push(&p->stacks[i], -1);
return pos;
}
static void fun_param(Parser *p, size_t begin_pos, Lexeme name, bool gather)
{
xHt *locals = &p->scopes.data[p->scopes.size - 1];
uint32_t old_size = xht_size(locals);
uint32_t idx = *xht_put_int(
locals,
name.start, name.size, hash_str(name.start, name.size),
old_size);
if (UU_UNLIKELY(idx != old_size))
throw_error_at(p, "duplicate parameter", name);
Shape *shape = fun_shape_ptr(p, begin_pos);
if (gather) {
shape->nargs_encoded = ~shape->nargs_encoded;
} else {
++shape->nargs_encoded;
}
}
static void fun_end(Parser *p, size_t begin_pos)
{
Instr push_nil = {OP_LOAD_CONST, 0, 0, add_const_steal(p, mk_nil())};
emit(p, push_nil);
Instr ret = {OP_RETURN, 0, 0, 0};
emit(p, ret);
size_t pos = here(p);
Shape *shape = fun_shape_ptr(p, begin_pos);
close_scope(p, &shape->maxstack, &shape->nlocals);
size_t offset = pos - begin_pos;
if (UU_UNLIKELY(offset > UINT32_MAX / 2)) {
throw_error(p, "function body is too long");
}
shape->offset = offset;
for (int i = 0; i < S__LAST; ++i)
stack_pop(&p->stacks[i]);
}
static inline void this_is_expr(Parser *p, bool expect_expr)
{
if (UU_UNLIKELY(!expect_expr))
throw_error_at(p, "unexpected expression", p->cur);
}
static inline void after_expr(Parser *p, bool expect_expr)
{
if (UU_UNLIKELY(expect_expr))
throw_error_at(p, "expected expression", p->cur);
}
// forward declaration
static inline void expr(Parser *p, int8_t min_priority);
static void unary_operator(Parser *p)
{
typedef struct {
uint8_t op;
int8_t priority;
} UnaryOpProps;
static const UnaryOpProps table[LK__LAST] = {
[LK_MINUS] = {OP_NEG, 50},
[LK_BANG] = {OP_NOT, 50},
[LK_AT] = {OP_LEN, 60},
};
Lexeme cur = p->cur;
UnaryOpProps props = table[cur.kind];
if (UU_UNLIKELY(!props.priority))
throw_error_at(p, "syntax error", cur);
advance(p);
expr(p, props.priority);
Instr instr = {props.op, 0, 0, 0};
emit_at(p, instr, cur);
}
static bool binary_operator(Parser *p, int8_t min_priority)
{
typedef struct {
uint8_t op;
uint8_t a;
int8_t priority;
int8_t is_left_assoc;
} BinaryOpProps;
enum {
LEFT_TO_RIGHT = 1,
RIGHT_TO_LEFT = 0,
};
static const BinaryOpProps table[LK__LAST] = {
[LK_TILDE] = {OP_AOP, AOP_CONCAT, 10, LEFT_TO_RIGHT},
[LK_OR_OR] = {OP_AOP, AOP_OR, 11, LEFT_TO_RIGHT},
[LK_AND_AND] = {OP_AOP, AOP_AND, 12, LEFT_TO_RIGHT},
[LK_OR] = {OP_AOP, AOP_BIT_OR, 13, LEFT_TO_RIGHT},
[LK_HAT] = {OP_AOP, AOP_BIT_XOR, 14, LEFT_TO_RIGHT},
[LK_AND] = {OP_AOP, AOP_BIT_AND, 15, LEFT_TO_RIGHT},
[LK_BANG_EQ] = {OP_CMP_2WAY, 0, 16, LEFT_TO_RIGHT},
[LK_EQ_EQ] = {OP_CMP_2WAY, COMPARE_EQ, 16, LEFT_TO_RIGHT},
[LK_GREATER_EQ] = {OP_CMP_3WAY, COMPARE_GREATER | COMPARE_EQ, 17, LEFT_TO_RIGHT},
[LK_GREATER] = {OP_CMP_3WAY, COMPARE_GREATER, 17, LEFT_TO_RIGHT},
[LK_LESS_EQ] = {OP_CMP_3WAY, COMPARE_LESS | COMPARE_EQ, 17, LEFT_TO_RIGHT},
[LK_LESS] = {OP_CMP_3WAY, COMPARE_LESS, 17, LEFT_TO_RIGHT},
[LK_GREATER_GREATER] = {OP_AOP, AOP_RSHIFT, 18, LEFT_TO_RIGHT},
[LK_LESS_LESS] = {OP_AOP, AOP_LSHIFT, 18, LEFT_TO_RIGHT},
[LK_MINUS] = {OP_AOP, AOP_SUB, 19, LEFT_TO_RIGHT},
[LK_PLUS] = {OP_AOP, AOP_ADD, 19, LEFT_TO_RIGHT},
[LK_PERCENT] = {OP_AOP, AOP_MOD, 20, LEFT_TO_RIGHT},
[LK_SLASH] = {OP_AOP, AOP_DIV, 20, LEFT_TO_RIGHT},
[LK_SLASH_SLASH] = {OP_AOP, AOP_IDIV, 20, LEFT_TO_RIGHT},
[LK_STAR] = {OP_AOP, AOP_MUL, 20, LEFT_TO_RIGHT},
[LK_STAR_STAR] = {OP_AOP, AOP_POW, 21, RIGHT_TO_LEFT},
};
Lexeme cur = p->cur;
BinaryOpProps props = table[cur.kind];
if (UU_UNLIKELY(!props.priority))
throw_error_at(p, "syntax error", cur);
if (props.priority < min_priority)
return false;
advance(p);
expr(p, props.priority + props.is_left_assoc);
Instr instr = {props.op, props.a, 0, 0};
emit_at(p, instr, cur);
return true;
}
static uint32_t funcall(Parser *p, bool *is_scatter)
{
*is_scatter = false;
advance(p);
if (p->cur.kind == LK_RPAREN) {
advance(p);
return 0;
}
uint32_t nargs = 1;
for (;;) {
if (p->cur.kind == LK_STAR) {
*is_scatter = true;
advance(p);
}
expr(p, -1);
switch (p->cur.kind) {
case LK_RPAREN:
advance(p);
return nargs;
case LK_COMMA:
advance(p);
if (UU_UNLIKELY(*is_scatter))
throw_error_at(p, "scatter argument is not the last one", p->cur);
if (UU_UNLIKELY(nargs == UINT32_MAX))
throw_error_at(p, "too many arguments", p->cur);
++nargs;
break;
default:
throw_error_at(p, "expected ',' or ')'", p->cur);
}
}
}
static uint32_t newlist(Parser *p)
{
advance(p);
if (p->cur.kind == LK_RBRACKET) {
advance(p);
return 0;
}
uint32_t nelems = 1;
for (;;) {
expr(p, -1);
switch (p->cur.kind) {
case LK_RBRACKET:
advance(p);
return nelems;
case LK_COMMA:
advance(p);
if (UU_UNLIKELY(nelems == UINT32_MAX))
throw_error_at(p, "too many list elements", p->cur);
++nelems;
break;
default:
throw_error_at(p, "expected ',' or ']'", p->cur);
}
}
}
static uint32_t newdict(Parser *p)
{
advance(p);
if (p->cur.kind == LK_RBRACE) {
advance(p);
return 0;
}
uint32_t nelems = 1;
for (;;) {
expr(p, -1);
slurp(p, LK_COLON, "expected ':'");
expr(p, -1);
switch (p->cur.kind) {
case LK_RBRACE:
advance(p);
return nelems;
case LK_COMMA:
advance(p);
if (UU_UNLIKELY(nelems == UINT32_MAX))
throw_error_at(p, "too many dict entries", p->cur);
++nelems;
break;
default:
throw_error_at(p, "expected ',' or '}'", p->cur);
}
}
}
static uint32_t add_number_const(Parser *p, Lexeme token)
{
const char *begin = token.start;
const char *end = begin + token.size;
return add_const_steal(p, (Value) number_parse(begin, end));
}
static inline int decode_hex(char c)
{
switch (c) {
case '0' ... '9':
return c - '0';
case 'a' ... 'f':
return c - 'a' + 10;
case 'A' ... 'F':
return c - 'A' + 10;
default:
return -1;
}
}
static int unescape(const char **s)
{
char c = **s;
++*s;
switch (c) {
case '\\': return '\\';
case 'a': return '\a';
case 'b': return '\b';
case 'e': return '\033';
case 'f': return '\f';
case 'n': return '\n';
case 'r': return '\r';
case 't': return '\t';
case 'v': return '\v';
case '"': return '"';
case '0': return '\0';
case 'x':
{
int hi = decode_hex(**s);
++*s;
if (UU_UNLIKELY(hi < 0))
goto error;
int lo = decode_hex(**s);
++*s;
if (UU_UNLIKELY(lo < 0))
goto error;
return (hi << 4) | lo;
}
default:
goto error;
}
error:
return -1;
}
static uint32_t add_string_const(Parser *p, Lexeme token)
{
const char *ptr = token.start + 1;
const char *end = token.start + token.size - 1;
String *s = (String *) string_new_with_capacity(NULL, 0, /*capacity=*/end - ptr);
while (ptr != end) {
const char *esc = memchr(ptr, '\\', end - ptr);
if (!esc) {
s = string_append(s, ptr, end - ptr);
break;
}
s = string_append(s, ptr, esc - ptr);
++esc;
int v = unescape(&esc);
if (UU_UNLIKELY(v < 0)) {
string_destroy(s);
--esc;
Position pos = {
.line = token.pos.line,
.column = token.pos.column + (esc - token.start),
};
throw_error_precise(p, "invalid escape", pos, 1);
}
char c = v;
s = string_append(s, &c, 1);
ptr = esc;
}
return add_const_steal(p, (Value) s);
}
static inline void expr(Parser *p, int8_t min_priority)
{
bool expect_expr = true;
for (;;) {
Lexeme cur = p->cur;
switch (cur.kind) {
case LK_NUMBER:
this_is_expr(p, expect_expr);
emit_at(
p,
(Instr) {OP_LOAD_CONST, 0, 0, add_number_const(p, cur)},
cur);
expect_expr = false;
advance(p);
break;
case LK_TRUE:
this_is_expr(p, expect_expr);
emit_at(
p,
(Instr) {OP_LOAD_CONST, 0, 0, add_const_steal(p, mk_flag(true))},
cur);
expect_expr = false;
advance(p);
break;
case LK_FALSE:
this_is_expr(p, expect_expr);
emit_at(
p,
(Instr) {OP_LOAD_CONST, 0, 0, add_const_steal(p, mk_flag(false))},
cur);
expect_expr = false;
advance(p);
break;
case LK_NIL:
this_is_expr(p, expect_expr);
emit_at(
p,
(Instr) {OP_LOAD_CONST, 0, 0, add_const_steal(p, mk_nil())},
cur);
expect_expr = false;
advance(p);
break;
case LK_STRING:
this_is_expr(p, expect_expr);
emit_at(
p,
(Instr) {OP_LOAD_CONST, 0, 0, add_string_const(p, cur)},
cur);
expect_expr = false;
advance(p);
break;
case LK_IDENT:
this_is_expr(p, expect_expr);
emit_at(
p,
load(p, cur),
cur);
expect_expr = false;
advance(p);
break;
case LK_LBRACKET:
if (expect_expr) {
uint32_t nelems = newlist(p);
emit_at(
p,
(Instr) {OP_LIST, 0, 0, nelems},
cur);
expect_expr = false;
} else {
advance(p);
expr(p, -1);
slurp(p, LK_RBRACKET, "expected ']'");
emit_at(
p,
(Instr) {OP_LOAD_AT, 0, 0, 0},
cur);
}
break;
case LK_LBRACE:
{
this_is_expr(p, expect_expr);
uint32_t nentries = newdict(p);
emit_at(
p,
(Instr) {OP_DICT, 0, 0, nentries},
cur);
expect_expr = false;
}
break;
case LK_LPAREN:
if (expect_expr) {
advance(p);
expr(p, -1);
slurp(p, LK_RPAREN, "expected ')'");
expect_expr = false;
} else {
bool is_scatter;
uint32_t nargs = funcall(p, &is_scatter);
emit_at(
p,
(Instr) {OP_CALL, is_scatter, 0, nargs},
cur);
}
break;
case LK_DOT:
{
after_expr(p, expect_expr);
advance(p);
Lexeme field = p->cur;
if (UU_UNLIKELY(field.kind != LK_IDENT))
throw_error_at(p, "expected identifier (field name)", field);
uint32_t const_idx = add_const_steal(p, (Value) string_new(field.start, field.size));
emit_at(
p,
(Instr) {OP_LOAD_CONST, 0, 0, const_idx},
field);
emit_at(
p,
(Instr) {OP_LOAD_AT, 0, 0, 0},
field);
advance(p);
}
break;
case LK_AND_AND_EQ:
case LK_AND_EQ:
case LK_COMMA:
case LK_EOF:
case LK_EQ:
case LK_COLON:
case LK_HAT_EQ:
case LK_MINUS_EQ:
case LK_OR_EQ:
case LK_OR_OR_EQ:
case LK_PERCENT_EQ:
case LK_PLUS_EQ:
case LK_SEMICOLON:
case LK_SLASH_EQ:
case LK_SLASH_SLASH_EQ:
case LK_STAR_EQ:
case LK_STAR_STAR_EQ:
case LK_TILDE_EQ:
case LK_COLON_EQ:
case LK_RPAREN:
case LK_RBRACKET:
case LK_RBRACE:
case LK_GREATER_GREATER_EQ:
case LK_LESS_LESS_EQ:
after_expr(p, expect_expr);
return;