-
Notifications
You must be signed in to change notification settings - Fork 0
/
rho.c
1680 lines (1529 loc) · 35.4 KB
/
rho.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
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "list.h"
#include "rho.h"
#define PMAX 10
#define tag(p) ((p)->tag)
#define bits32(x) (32 - __builtin_clz(x))
#define max2(a, b) ((a) > (b) ? (a) : (b))
#define header(p) ((rho_header *)((char *)(p) - sizeof(rho_header)))
#define len(p) rho_len(p)
#define cap(p) rho_cap(p)
#define toint(p) ((p)->u.i)
#define tofloat(p) ((p)->u.f)
#define toptr(p) ((p)->u.ptr)
#define toany(p, t) ((t)toptr(p))
#define tocproto(p) toany(p, rho_cproto)
#define toproto(p) toany(p, rho_proto *)
#define toclosure(p) toany(p, rho_closure *)
#define binop(ctx, op, sp) \
do { \
rho_value *p = (sp)--; \
switch (tag(sp)) { \
case RHO_INT: \
toint(sp) op## = rho_toint(rho_cast(ctx, p, RHO_INT)); \
break; \
case RHO_FLOAT: \
tofloat(sp) op## = rho_tofloat(rho_cast(ctx, p, RHO_FLOAT)); \
break; \
default: \
rho_panic(ctx, "runtime error: invalid operand(s)"); \
} \
} while (0)
#define binop_cmp(ctx, op, sp) \
do { \
rho_value *p = (sp)--; \
switch (tag(sp)) { \
case RHO_INT: \
toint(sp) = toint(sp) op rho_toint(rho_cast(ctx, p, RHO_INT)); \
break; \
case RHO_FLOAT: \
tofloat(sp) = tofloat(sp) op rho_tofloat(rho_cast(ctx, p, RHO_FLOAT)); \
break; \
default: \
rho_panic(ctx, "runtime error: invalid operand(s)"); \
} \
} while (0)
#define binop_bit(ctx, op, sp) \
do { \
rho_value *p = (sp)--; \
switch (tag(sp)) { \
case RHO_INT: \
toint(sp) op## = rho_toint(rho_cast(ctx, p, RHO_INT)); \
break; \
default: \
rho_panic(ctx, "runtime error: invalid operand(s)"); \
} \
} while (0)
typedef rho_byte byte;
typedef enum Op Op;
typedef enum Tk Tk;
enum Op {
NOP,
PSHC,
PSHR,
PSH,
POPR,
POP,
BOP,
UOP,
CALL,
RET,
J,
JB,
JZ,
MAKE,
ATTR,
};
static char *OP[] = {"nop", "pshc", "pshr", "psh", "popr",
"pop", "bop", "uop", "call", "ret",
"j", "jb", "jz", "make", "attr"};
enum Tk {
EOT, /* end of token */
CMT, /* // */
INT, /* 114151 */
FLT, /* 3.141592 */
STR, /* "hello" */
ID, /* a */
INC, /* ++ */
DEC, /* -- */
REV, /* ~ */
NOT, /* ! */
AT, /* @ */
_bop,
ADD, /* + */
SUB, /* - */
MUL, /* * */
DIV, /* / */
MOD, /* % */
POW, /* ** */
AND, /* & */
OR, /* | */
XOR, /* ^ */
LAND, /* && */
LOR, /* || */
SHL, /* << */
SHR, /* >> */
EQ, /* == */
NEQ, /* != */
LSS, /* < */
LEQ, /* <= */
GRT, /* > */
GEQ, /* >= */
_bopend,
ASS, /* = */
PARL, /* ( */
PARR, /* ) */
BRKL, /* [ */
BRKR, /* ] */
BRCL, /* { */
BRCR, /* } */
COL, /* : */
SEM, /* ; */
DOT, /* . */
COM, /* , */
_kw,
IF, /* if */
ELSE, /* else*/
FOR, /* for */
BRK, /* break */
CTN, /* continue */
VAR, /* var */
FN, /* fn */
STRT, /* struct */
_kwend,
};
static char *TK[] = {
"EOT", "//", "INT", "FLT", "STR", "ID", "++", "--",
"~", "!", "@", "_bop", "+", "-", "*", "/",
"%", "**", "&", "|", "^", "&&", "||", "<<",
">>", "==", "!=", "<", "<=", ">", ">=", "_bopend",
"=", "(", ")", "[", "]", "{", "}", ":",
";", ".", ",", "_kw", "if", "else", "for", "break",
"continue", "var", "fn", "struct", "_kwend"};
static char *TG[] = {"int", "float", "bool", "pointer",
"c string", "proto", "c proto", "closure"};
static int precedence(int tk) {
switch (tk) {
case ADD:
case SUB:
return 11;
case MUL:
case DIV:
case MOD:
case POW:
return 12;
case OR:
return 5;
case XOR:
return 6;
case AND:
return 7;
case LAND:
return 4;
case LOR:
return 3;
case SHL:
case SHR:
return 10;
case LSS:
case LEQ:
case GRT:
case GEQ:
return 9;
case EQ:
case NEQ:
return 8;
case ASS:
return 1;
default:
return 0;
}
}
struct rho_header {
rho_header *next;
int refs;
int esize;
int size;
int avail;
void *ptr;
};
struct rho_runtime {
struct list_head ctx;
rho_header *free[PMAX];
rho_allocator alloc;
int len;
};
struct rho_context {
rho_runtime *r;
rho_value *top;
rho_value *sp;
rho_ref *openrefs;
rho_type *types;
struct list_head link;
};
struct rho_ref {
rho_ref *next;
rho_value *vp;
rho_value v;
};
struct rho_var {
rho_type *type;
rho_string name;
int isconst : 1;
int ispub : 1;
int scope;
int idx; /* index into proto::vars of the parent function if ::islocal,
otherwise index into closure::refs of the parent function. */
};
struct rho_type {
rho_string name;
rho_var *attrs;
int callable : 1;
int alias;
};
struct rho_proto {
rho_proto *prev;
rho_proto **p;
rho_var *vars; /* arguments and local variables. */
rho_var *refs; /* closure variables. */
rho_value *consts; /* constants */
byte *code;
int nin; /* the number of arguments. */
int nout; /* the number of values returned. */
};
struct rho_closure {
rho_proto *p;
rho_ref **refs;
};
struct token {
int kind;
int iskw : 1;
int line;
byte *linep;
rho_string s;
};
/* An LL(1) parser */
struct rho_parser {
rho_context *ctx;
rho_proto *p;
struct token t;
struct token ahead;
byte *src;
byte *curp;
byte *linep;
int line;
};
static int precedence(int);
static void next(rho_parser *);
static void peek(rho_parser *);
static void number(rho_parser *);
static void literal(rho_parser *);
static int ident(rho_parser *);
static void block(rho_parser *);
static void stmt(rho_parser *);
static void stmtlist(rho_parser *, Tk);
static void structdecl(rho_parser *);
static void vardecl(rho_parser *);
static void assign(rho_parser *);
static rho_type *arglist(rho_parser *, bool);
static void uexpr(rho_parser *);
static void expr(rho_parser *, int);
static void exprlist(rho_parser *);
static void ifexpr(rho_parser *);
static void forexpr(rho_parser *);
static void scan(rho_parser *, struct token *);
static void emit(rho_parser *, byte);
static void traceback(rho_context *);
static rho_var *findvar(rho_parser *, struct token *);
static void closerefs(rho_context *, rho_value *);
static rho_ref *findref(rho_context *, rho_value *);
static rho_closure *makeclosure(rho_context *, rho_proto *, rho_ref **,
rho_value *);
rho_runtime *rho_new(rho_allocator alloc) {
rho_runtime *r;
rho_assert(alloc);
if (!(r = alloc(0, sizeof(*r))))
return NULL;
r->alloc = alloc;
r->len = 0;
list_head_init(&r->ctx);
return r;
}
static void inittypes(rho_context *ctx) {
rho_type *tp;
int usz, sz;
usz = sizeof(struct rho_type);
sz = 3 * usz;
tp = rho_callocgc(ctx, 3, usz);
memset(tp, 0, sz);
tp[0].name.p = (byte *)TG[RHO_INT];
tp[0].name.len = 3;
tp[1].name.p = (byte *)TG[RHO_FLOAT];
tp[1].name.len = 5;
tp[2].name.p = (byte *)TG[RHO_BOOL];
tp[2].name.len = 4;
header(tp)->avail -= sz;
ctx->types = tp;
}
rho_context *rho_open(rho_runtime *r, int size) {
rho_context *ctx;
rho_assert(r);
if (!(ctx = r->alloc(0, size * sizeof(rho_value) + sizeof(*ctx))))
return NULL;
ctx->sp = (rho_value *)(ctx + 1);
ctx->top = ctx->sp + size;
ctx->openrefs = NULL;
ctx->r = r;
inittypes(ctx);
rho_lock(ctx);
list_add(&ctx->link, &r->ctx);
r->len++;
rho_unlock(ctx);
return ctx;
}
void rho_close(rho_context *ctx) {
rho_runtime *r;
r = ctx->r;
rho_lock(ctx);
list_del(&ctx->link);
r->len--;
rho_unlock(ctx);
// r->alloc(ctx->types, 0);
r->alloc(ctx, 0);
}
/*
Given a Rho context and the number of arguments pushed
onto the stack, calls TOS-n, and then returns the number
of values the callee pushes onto the stack.
*/
int rho_call(rho_context *ctx, int n) {
rho_closure *cls;
rho_value *sp, *bp, *ap;
byte *pc, a;
int k;
sp = ctx->sp;
bp = sp - n;
ap = bp + 1;
/* See if the callee is a C function. */
if (tag(bp) == RHO_CPROTO)
return tocproto(bp)(ctx, n);
/* Make sure that the number of arguments given by caller
is the same or more than that the callee needs if it
is a Rho function. */
cls = toclosure(bp);
if (n < cls->p->nin)
rho_panic(ctx, "runtime error: expected at least %d arguments",
cls->p->nin);
/* Allocate stack for local variables. */
sp += len(cls->p->vars) - n;
if (sp > ctx->top)
rho_panic(ctx, "runtime error: stack overflow");
/* Jump to the callee's bytecode. */
pc = cls->p->code;
/* Execution loop */
for (;;) {
switch (*pc++) {
case CALL:
ctx->sp = sp;
k = rho_call(ctx, *pc++);
sp = ctx->sp;
if (sp - k < bp + n + k)
rho_panic(ctx, "runtime error: expected more local variables");
break;
case RET:
if (ctx->openrefs)
closerefs(ctx, ap);
k = *pc++;
memmove(bp, sp - k + 1, k * sizeof(*sp));
ctx->sp = bp + k - 1;
return k;
case MAKE:
a = *pc++;
switch (a) {
case RHO_CLOSURE:
*sp = rho_closure(makeclosure(ctx, toproto(sp), cls->refs, ap));
break;
default:
rho_panic(ctx, "runtime error: bad object %d to make", a);
}
break;
case PSHC:
*++sp = cls->p->consts[*pc++];
break;
case PSH:
*++sp = ap[*pc++];
break;
case POP:
ap[*pc++] = *sp--;
break;
case BOP:
a = *pc++;
switch (a) {
case ADD:
binop(ctx, +, sp);
break;
case SUB:
binop(ctx, -, sp);
break;
case MUL:
binop(ctx, *, sp);
break;
case DIV:
binop(ctx, /, sp);
break;
case MOD:
binop_bit(ctx, %, sp);
break;
case AND:
binop_bit(ctx, &, sp);
break;
case OR:
binop_bit(ctx, |, sp);
break;
case XOR:
binop_bit(ctx, ^, sp);
break;
case SHL:
binop_bit(ctx, <<, sp);
break;
case SHR:
binop_bit(ctx, >>, sp);
break;
case EQ:
binop_cmp(ctx, >=, sp);
break;
case NEQ:
binop_cmp(ctx, >=, sp);
break;
case LSS:
binop_cmp(ctx, <, sp);
break;
case LEQ:
binop_cmp(ctx, <=, sp);
break;
case GRT:
binop_cmp(ctx, >, sp);
break;
case GEQ:
binop_cmp(ctx, >=, sp);
break;
}
break;
case UOP:
a = *pc++;
switch (a) {
case NOT:
sp->u.i = !sp->u.i;
break;
case REV:
sp->u.i = ~sp->u.i;
break;
case INC:
sp->u.i++;
break;
case DEC:
sp->u.i--;
break;
case AT:
rho_println(ctx, sp);
break;
}
break;
case J: /* Jump to pc+a */
a = *pc++;
pc += a;
break;
case JB: /* Jump to pc-a */
a = *pc++;
pc -= a;
break;
case JZ: /* Jump to pc+a if TOS is falsy */
a = *pc++;
if (!(sp->u.i))
pc += a;
sp--; /* drop the value used for comparison */
break;
case NOP:
break;
default:
rho_panic(ctx, "runtime error: bad opcode %s", OP[*(pc - 1)]);
}
}
}
void rho_push(rho_context *ctx, rho_value v) { *++ctx->sp = v; }
rho_value rho_pop(rho_context *ctx) { return *ctx->sp--; }
static rho_closure *makeclosure(rho_context *ctx, rho_proto *p, rho_ref **encp,
rho_value *arg) {
rho_closure *cls;
rho_var *ref;
int i, nrefs;
nrefs = len(p->refs);
cls = rho_allocex(ctx, rho_closure, nrefs);
rho_assert(cls);
cls->p = p;
cls->refs = (rho_ref **)(cls + 1);
for (i = 0; i < nrefs; i++) {
ref = p->refs + i;
if (ref->scope <= 1)
cls->refs[i] = findref(ctx, arg + ref->idx);
else
cls->refs[i] = encp[ref->idx];
}
return cls;
}
static rho_ref *findref(rho_context *ctx, rho_value *arg) {
rho_ref *p, **pp;
pp = &ctx->openrefs;
while ((p = *pp) && p->vp >= arg) {
if (p->vp == arg)
return p;
pp = &p->next;
}
p = rho_alloc(ctx, struct rho_ref);
p->vp = arg;
p->next = *pp;
*pp = p;
return p;
}
static void closerefs(rho_context *ctx, rho_value *arg) {
rho_ref *p;
p = ctx->openrefs;
while (p && p->vp >= arg) {
p->v = *p->vp;
p->vp = &p->v;
p = p->next;
ctx->openrefs = p;
}
}
noreturn static void syntaxerror(rho_parser *ps, const char *fmt, ...) {
va_list ap;
struct token *t;
char *p, *end, *bp, b[64];
int n, i;
t = &ps->t;
p = (char *)t->linep;
end = (char *)t->s.p + t->s.len + 16;
n = t->s.p - t->linep + 6;
bp = b;
bp += sprintf(bp, "%-2d | ", t->line);
while (p != end && *p != '\n') {
bp += sprintf(bp, "%c", *p);
p++;
}
*bp++ = '\n';
for (i = 0; i < n; i++)
*bp++ = ' ';
*bp++ = '^';
for (i = 0; i < t->s.len - 1; i++)
*bp++ = '~';
*bp++ = '\n';
for (i = 0; i < n; i++)
*bp++ = ' ';
*bp++ = '|';
*bp++ = '\n';
*bp++ = '\0';
fprintf(stderr, b);
fprintf(stderr, "syntax error: ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
putc('\n', stderr);
exit(1);
}
#define expect(ps, tk) \
do { \
if (ps->t.kind != tk) \
syntaxerror(ps, "expect token '%s'", TK[tk]); \
} while (0)
static void stmtlist(rho_parser *ps, Tk end) {
Tk tk;
tk = ps->t.kind;
while (tk != end && tk != EOT) {
stmt(ps);
tk = ps->t.kind;
}
}
static void stmt(rho_parser *ps) {
Tk tk, ah;
tk = ps->t.kind;
switch (tk) {
case BRK:
emit(ps, J);
emit(ps, NOP);
/* TODO: save */
next(ps);
return;
case CMT:
next(ps);
return;
case VAR:
vardecl(ps);
return;
case FN:
/* TODO: function declaration*/
return;
case STRT:
structdecl(ps);
return;
case ID:
/* TODO: */
peek(ps);
ah = ps->ahead.kind;
if (ah == ASS || ah == COM) {
assign(ps);
return;
}
/* otherwise fall back to expr */
default:
expr(ps, 0); /* expression */
}
}
static void block(rho_parser *ps) {
expect(ps, BRCL);
next(ps);
stmtlist(ps, BRCR);
expect(ps, BRCR);
next(ps);
}
/*
ifexpr := 'if' expr block [ 'else' block ]
s0 s0
if s1 { s1
s2 jz -> s3
} else { s2
s3 j -> s4
} s3
s4 s4
*/
static void ifexpr(rho_parser *ps) {
byte **pp;
int n, s2, s3;
pp = &ps->p->code;
expect(ps, IF);
next(ps);
expr(ps, 0); /* s1 */
emit(ps, JZ);
emit(ps, NOP);
s2 = len(*pp);
block(ps); /* s2 */
if (ps->t.kind == ELSE) {
emit(ps, J);
emit(ps, NOP);
next(ps);
s3 = len(*pp);
block(ps); /* s3 */
n = len(*pp);
(*pp)[s3 - 1] = n - s3;
}
(*pp)[s2 - 1] = s3 - s2;
}
/*
forexpr := 'for' [ stmt ] ';' [ expr ] ';' [ stmt ] block
s0 s0
for s1; s2; s3 { s1
s4 s2
} jz -> s5
s5 j -> s4
s3
j -> s2
s4
j -> s3
s5
*/
static void forexpr(rho_parser *ps) {
byte **pp;
int j5, j4, j2, j3, s2, s3;
pp = &ps->p->code;
expect(ps, FOR);
next(ps);
if (ps->t.kind != SEM)
stmt(ps); /* s1 */
expect(ps, SEM);
next(ps);
s2 = len(*pp);
if (ps->t.kind != SEM)
expr(ps, 0); /* s2 */
emit(ps, JZ);
emit(ps, NOP);
j5 = len(*pp);
emit(ps, J);
emit(ps, NOP);
j4 = len(*pp);
expect(ps, SEM);
next(ps);
s3 = len(*pp);
if (ps->t.kind != BRCL)
stmt(ps); /* s3 */
emit(ps, JB);
emit(ps, NOP);
j2 = len(*pp);
block(ps); /* s4 */
emit(ps, JB);
emit(ps, NOP);
j3 = len(*pp);
(*pp)[j5 - 1] = j3 - j5;
(*pp)[j4 - 1] = j2 - s3;
(*pp)[j2 - 1] = j2 - s2;
(*pp)[j3 - 1] = j3 - s3;
}
/* vardecl := 'var' arglist */
static void vardecl(rho_parser *ps) {
expect(ps, VAR);
next(ps);
arglist(ps, false);
next(ps);
}
/* arglist := ID [ ',' ID ] type */
static rho_type *arglist(rho_parser *ps, bool isconst) {
rho_var v, *vp, **vpp;
rho_type *tp;
int n, k, i;
expect(ps, ID);
n = len(ps->p->vars);
for (k = 0; k < n; k++) {
vp = ps->p->vars + k;
if (rho_strcmp(&vp->name, &ps->t.s) == 0)
syntaxerror(ps, "redundant variable declaration");
}
v.idx = k;
v.isconst = isconst;
v.name = ps->t.s;
v.scope = 0;
vpp = &ps->p->vars;
*vpp = rho_append(ps->ctx, *vpp, &v, 1, rho_var);
next(ps);
switch (ps->t.kind) {
case ID:
n = len(ps->ctx->types);
for (i = 0; i < n; i++) {
tp = ps->ctx->types + i;
if (rho_strcmp(&tp->name, &ps->t.s) == 0) {
(*vpp)[k].type = tp;
return tp;
}
}
syntaxerror(ps, "undefined type");
case COM:
next(ps);
tp = arglist(ps, isconst);
if (tp)
(*vpp)[k].type = tp;
return tp;
default:
syntaxerror(ps, "unexpected token");
}
}
static rho_type *findtype(rho_parser *ps, struct token *t) {
rho_type *tp;
int n, i;
n = len(ps->ctx->types);
for (i = 0; i < n; i++) {
tp = ps->ctx->types + i;
if (rho_strcmp(&tp->name, &t->s) == 0)
return tp;
}
return NULL;
}
static void structdecl(rho_parser *ps) {
rho_type t, *tp, **tpp, *ap;
rho_var v;
int i;
expect(ps, STRT);
next(ps);
tp = findtype(ps, &ps->t);
if (tp)
syntaxerror(ps, "redundant type definition");
t.name = ps->t.s;
t.attrs = NULL;
/* we must append t before moving to its attributes who's
type can also be t */
tpp = &ps->ctx->types;
*tpp = rho_append(ps->ctx, *tpp, &t, 1, rho_type);
tp = *tpp + len(*tpp) - 1;
next(ps);
expect(ps, BRCL);
next(ps);
i = 0;
while (ps->t.kind != BRCR) {
expect(ps, ID);
v.name = ps->t.s;
next(ps);
expect(ps, ID);
ap = findtype(ps, &ps->t);
if (!ap)
syntaxerror(ps, "undefined type");
v.type = ap;
v.idx = i;
tp->attrs = rho_append(ps->ctx, tp->attrs, &v, 1, rho_var);
next(ps);
i++;
}
next(ps);
}
/* assign := ID [ ',' ID ]+ [ bop ] '=' exprlist */
static void assign(rho_parser *ps) {
rho_var *vp;
expect(ps, ID);
vp = findvar(ps, &ps->t);
next(ps);
switch (ps->t.kind) {
case ASS:
next(ps);
exprlist(ps);
goto end;
case COM:
next(ps);
assign(ps);
goto end;
default:
syntaxerror(ps, "unexpected token");
}
end:
/* TODO: statistic analysis for types */
emit(ps, vp->scope == 0 ? POP : POPR);
emit(ps, vp->idx);
}
static void exprlist(rho_parser *ps) {
expr(ps, 0);
while (ps->t.kind == COM) {
next(ps);
expr(ps, 0);
}
}
/* Top-down expression parsing. */
static void expr(rho_parser *ps, int plv) {
Tk tk;
int lv;
tk = ps->t.kind;
if (tk == EOT)
return;
uexpr(ps); /* left branch */
tk = ps->t.kind;
lv = precedence(tk);
while (tk && plv < lv) {
next(ps);
expr(ps, lv); /* right branch */
emit(ps, BOP);
emit(ps, tk);
tk = ps->t.kind;
lv = precedence(tk);
}
}
static void uexpr(rho_parser *ps) {
Tk tk;
int n;
tk = ps->t.kind;
switch (tk) {
case INT:
case FLT:
number(ps);
return;
case STR:
literal(ps);
return;
case ID:
/* TODO */
n = ident(ps);
tk = ps->t.kind;
switch (tk) {
case INC:
case DEC:
emit(ps, UOP);
emit(ps, tk);
emit(ps, POP);
emit(ps, n);
next(ps);
break;
case BRCL:
break;
}
return;
case NOT:
case REV:
case AT:
next(ps);
expr(ps, 0);
emit(ps, UOP);
emit(ps, tk);
return;
case IF:
ifexpr(ps);
return;
case FOR:
forexpr(ps);
return;
case PARL:
next(ps);
expr(ps, 0);