-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathparser.coh
1094 lines (889 loc) · 32.2 KB
/
parser.coh
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
# Important: this is not a recursive descent parser (because Cowgol doesn't
# support recursion). It reads one statement at a time, using an explicit stack
# to store data, and Dijksta's Shunting Yard Algorithm for parsing recursive
# structures like types or expressions. Make sure not to call any routine
# reentrantly!
sub Parse() is
const PARSE_STACK_SIZE := 128;
var stack: [void][PARSE_STACK_SIZE];
var sp: @indexof stack;
var opstack: uint8[PARSE_STACK_SIZE];
var opsp: @indexof opstack;
var exit_label: LabelRef := nil;
var true_label: LabelRef := nil;
var false_label: LabelRef := nil;
var false_generated: uint8 := 0;
var break_label: LabelRef := 0;
var continue_label: LabelRef := 0;
var type_mode: uint8;
const UNARY_FLAG := 0x80;
sub SyntaxError(token: uint8) is
StartError();
print("syntax error: unexpected ");
PrintTokenName(token);
EndError();
end sub;
sub BadStructureError(token: uint8) is
SimpleError("mismatched structure");
end sub;
sub Push(p: [void]) is
stack[sp] := p;
sp := sp + 1;
end sub;
sub Pop(): (val: [void]) is
if sp == 0 then
SimpleError("opstack underflow");
end if;
sp := sp - 1;
val := stack[sp];
end sub;
sub PushSymbol(p: [Symbol]) is
Push(p as [void]);
end sub;
sub PushType(t: [Type]) is
Push(t as [void]);
end sub;
sub PushNode(n: [Node]) is
Push(n as [void]);
end sub;
sub PushLabelRef(ref: LabelRef) is
Push(ref as intptr as [void]);
end sub;
sub PushInt(i: uint8) is
Push(i as intptr as [void]);
end sub;
sub PushSubroutine(subr: [Subroutine]) is
Push(subr as [void]);
end sub;
sub PopType(): (t: [Type]) is
t := Pop() as [Type];
end sub;
sub PopNode(): (n: [Node]) is
n := Pop() as [Node];
end sub;
sub PopLabelRef(): (ref: LabelRef) is
ref := Pop() as intptr as LabelRef;
end sub;
sub PopInt(): (n: uint8) is
n := Pop() as intptr as uint8;
end sub;
sub PopSubroutine(): (subr: [Subroutine]) is
subr := Pop() as [Subroutine];
end sub;
sub PushIfData() is
PushLabelRef(exit_label);
PushLabelRef(true_label);
PushLabelRef(false_label);
PushInt(false_generated);
PushInt(IF);
end sub;
sub PopIfData() is
if PopInt() != IF then
BadStructureError(IF);
end if;
false_generated := PopInt();
false_label := PopLabelRef();
true_label := PopLabelRef();
exit_label := PopLabelRef();
end sub;
sub PushLoopData() is
PushLabelRef(break_label);
PushLabelRef(continue_label);
PushInt(LOOP);
end sub;
sub PopLoopData() is
if PopInt() != LOOP then
BadStructureError(LOOP);
end if;
continue_label := PopLabelRef();
break_label := PopLabelRef();
end sub;
sub PushSubData() is
PushSubroutine(current_subr);
PushIfData();
PushLoopData();
PushInt(SUB);
end sub;
sub PopSubData() is
if PopInt() != SUB then
BadStructureError(SUB);
end if;
PopLoopData();
PopIfData();
current_subr := PopSubroutine();
end sub;
sub CheckStructure(t: uint8) is
if (sp == 0) or (stack[sp-1] as intptr as uint8 != t) then
BadStructureError(t);
end if;
end sub;
sub PrintOpStack() is
var i: uint8 := 0;
print("[");
while i != opsp loop
if i != 0 then
print_char(' ');
end if;
var op := opstack[i];
if (op & UNARY_FLAG) != 0 then
print("U+");
end if;
PrintTokenName(op & 0x7f);
i := i + 1;
end loop;
print("]\n");
end sub;
sub PushOp(op: uint8) is
print("pushop: ");
print_i8(op);
print_nl();
opstack[opsp] := op;
opsp := opsp + 1;
PrintOpStack();
end sub;
sub PopOp(): (op: uint8) is
if opsp == 0 then
SimpleError("opstack underflow");
end if;
opsp := opsp - 1;
op := opstack[opsp];
print("popop: ");
print_i8(op);
print_nl();
end sub;
sub RequireToken(t: uint8) is
if token != t then
StartError();
print("expected token ");
PrintTokenName(t);
print(", but got ");
PrintTokenName(token);
EndError();
end if;
LexerGetToken();
end sub;
sub GrabTokenBuffer(): (news: string) is
news := InternalStrDup(&token_buffer[0]);
end sub;
sub _check_for_identifier() is
if token != ID then
SimpleError("expected identifier");
end if;
end sub;
sub ParseNewSymbol(subr: [Subroutine]): (sym: [Symbol]) is
_check_for_identifier();
sym := AddSymbol(&subr.namespace, GrabTokenBuffer());
LexerGetToken();
end sub;
sub ParseExistingSymbol(): (sym: [Symbol]) is
_check_for_identifier();
sym := LookupSymbol(0 as [Namespace], &token_buffer[0]);
if sym == (0 as [Symbol]) then
StartError();
print("symbol '");
print(&token_buffer[0]);
print("' not found");
EndError();
end if;
LexerGetToken();
end sub;
sub ParseNewOrExistingSymbol(): (sym: [Symbol]) is
_check_for_identifier();
var name := GrabTokenBuffer();
sym := LookupSymbol(0 as [Namespace], name);
if sym == (0 as [Symbol]) then
sym := AddSymbol(0 as [Namespace], name);
else
Free(name);
end if;
LexerGetToken();
end sub;
sub ToConstant(node: [Node]): (value: int32) is
if node.op != MIDCODE_CONSTANT then
SimpleError("non-constant value provided");
end if;
value := node.constant.value;
end sub;
sub Negate(node: [Node]) is
node.beq.negated := node.beq.negated ^ 1;
end sub;
sub ConditionalEq(lhs: [Node], rhs: [Node], negated: uint8): (result: [Node]) is
CondSimple(lhs, rhs);
var truelabel := AllocLabel();
var falselabel := AllocLabel();
var w := NodeWidth(lhs);
result := MidBeq(w, lhs, rhs, truelabel, falselabel, 0, negated);
end sub;
sub ConditionalLt(lhs: [Node], rhs: [Node], negated: uint8): (result: [Node]) is
CondSimple(lhs, rhs);
var truelabel := AllocLabel();
var falselabel := AllocLabel();
var w := NodeWidth(lhs);
if IsSNum(lhs.type) != 0 then
result := MidBlts(w, lhs, rhs, truelabel, falselabel, 0, negated);
else
result := MidBltu(w, lhs, rhs, truelabel, falselabel, 0, negated);
end if;
end sub;
sub ParseRaw() is
var startsp := sp;
var startopsp := opsp;
sub peekop(): (op: uint8) is
if opsp == startopsp then
SimpleError("unterminated expression");
end if;
op := opstack[opsp-1];
end sub;
sub precedenceof(op: uint8): (p: uint8) is
if (op & UNARY_FLAG) != 0 then
p := (token_flags[op] >> 4) + 16;
else
p := token_flags[op] & 0x0f;
end if;
end sub;
sub applyvalueop() is
var type: [Type];
var op := PopOp();
print("applyvalue ");
if (op & UNARY_FLAG) != 0 then
print("unary ");
end if;
print_i8(op & 0x7f);
print(": ");
PrintTokenName(op);
print_nl();
var rhs: [Node];
if (op & UNARY_FLAG) == 0 then
# Binary
rhs := PopNode();
end if;
var lhs := PopNode();
case op is
when OPENPAREN:
# infix OPENPAREN is function application
# do nothing
when OPENPAREN|UNARY_FLAG:
# unary OPENPAREN is grouping
# do nothing
when OPENSQ|UNARY_FLAG:
if IsPtr(lhs.type) == 0 then
SimpleError("cannot dereference non-pointers");
end if;
lhs := MakeLValue(lhs);
when PLUS:
lhs := ExprAdd(lhs, rhs);
when PLUS|UNARY_FLAG:
# do nothing
when MINUS:
lhs := ExprSub(lhs, rhs);
when MINUS|UNARY_FLAG:
lhs := Expr1Simple(MIDCODE_NEG0, lhs);
when STAR:
lhs := Expr2Simple(MIDCODE_MUL0, MIDCODE_MUL0, lhs, rhs);
when SLASH:
lhs := Expr2Simple(MIDCODE_DIVS0, MIDCODE_DIVU0, lhs, rhs);
when PERCENT:
lhs := Expr2Simple(MIDCODE_REMS0, MIDCODE_REMU0, lhs, rhs);
when UNARY_FLAG|TILDE:
lhs := Expr1Simple(MIDCODE_NOT0, lhs);
when CARET:
lhs := Expr2Simple(MIDCODE_EOR0, MIDCODE_EOR0, lhs, rhs);
when AMPERSAND:
lhs := Expr2Simple(MIDCODE_AND0, MIDCODE_AND0, lhs, rhs);
when UNARY_FLAG|AMPERSAND:
lhs := UndoLValue(lhs);
if lhs.op == MIDCODE_ADDRESS then
var sym := lhs.address.sym;
if IsScalar(sym.vardata.type) != 0 then
SimpleError("you cannot take the address of scalar variables");
end if;
end if;
when PIPE:
lhs := Expr2Simple(MIDCODE_OR0, MIDCODE_OR0, lhs, rhs);
when LSHIFT:
lhs := ExprShift(MIDCODE_LSHIFT0, MIDCODE_LSHIFT0, lhs, rhs);
when RSHIFT:
lhs := ExprShift(MIDCODE_RSHIFTS0, MIDCODE_RSHIFTU0, lhs, rhs);
when NOT:
Negate(lhs);
when AND:
lhs := MidBand(lhs, rhs, 0, 0, 0, 0);
when OR:
lhs := MidBor(lhs, rhs, 0, 0, 0, 0);
when EQOP:
lhs := ConditionalEq(lhs, rhs, 0);
when NEOP:
lhs := ConditionalEq(lhs, rhs, 1);
when LTOP:
lhs := ConditionalLt(lhs, rhs, 0);
when GEOP:
lhs := ConditionalLt(lhs, rhs, 0);
when GTOP:
lhs := ConditionalLt(rhs, lhs, 0);
when LEOP:
lhs := ConditionalLt(rhs, lhs, 1);
when COMMA:
lhs := MidPair(rhs, lhs);
when OPENSQ:
type := lhs.type;
lhs := MaybeUndoLValue(lhs);
if IsArray(type) == 0 then
StartError();
print("you can only index an array, not a ");
print(type.symbol.name);
EndError();
end if;
CheckExpressionType(rhs, type.arraytype.indextype);
if IsNum(rhs.type) == 0 then
SimpleError("array indices must be numbers");
end if;
var elementtype := type.arraytype.element;
var w := intptr_type.width as uint8;
var displacement := MidC2Op(MIDCODE_MUL0, w,
MidCCast(intptr_type.width as uint8, rhs, 0),
MidConstant(elementtype.stride as int32));
displacement.type := intptr_type;
var adjustedaddress := MidC2Op(MIDCODE_ADD0, w, lhs, displacement);
adjustedaddress.type := MakePointerType(elementtype);
lhs := MakeLValue(adjustedaddress);
when OPENSQ_TYPE:
# Bit of a hack here --- the LHS is really a type.
var length := ToConstant(rhs);
type := MakeArrayType(lhs as [Type], length as uint16);
PushType(type);
type_mode := 1;
return;
when else:
StartError();
print("unhandled value op ");
if (opstack[opsp] & UNARY_FLAG) != 0 then
print("unary ");
end if;
PrintTokenName(opstack[opsp] & 0x7f);
EndError();
end case;
PushNode(lhs);
end sub;
sub applytypeop() is
var op := PopOp();
print("applytype ");
if (op & UNARY_FLAG) != 0 then
print("unary ");
end if;
print_i8(op & 0x7f);
print(": ");
PrintTokenName(op);
print_nl();
case op is
when OPENSQ|UNARY_FLAG:
PushType(MakePointerType(PopType()));
when else:
StartError();
print("unhandled type op ");
if (opstack[opsp] & UNARY_FLAG) != 0 then
print("unary ");
end if;
PrintTokenName(opstack[opsp] & 0x7f);
EndError();
end case;
end sub;
sub applyop() is
PrintOpStack();
if type_mode == 0 then
applyvalueop();
else
applytypeop();
end if;
end sub;
sub applyops(p: uint8) is
while opsp != 0 loop
var topopp := precedenceof(opstack[opsp-1]);
if topopp < p then
break;
end if;
applyop();
end loop;
end sub;
sub pushop(op: uint8) is
print("pushop ");
print_i8(op);
print(": ");
PrintTokenName(op);
print_nl();
applyops(precedenceof(op));
PushOp(op);
end sub;
sub bad_context(kind: string) is
StartError();
print("can't use ");
PrintTokenName(token);
print(" in a ");
print(kind);
print(" context");
EndError();
end sub;
sub not_allowed_in_type() is
if type_mode != 0 then
bad_context("type");
end if;
end sub;
sub parse() is
var f: uint8;
var sym: [Symbol];
var node: [Node];
var op: uint8;
loop
# Terms and prefix operators.
f := 0;
if token < @sizeof token_flags then
f := token_flags[token];
end if;
print("seen ");
PrintTokenName(token & 0x7f);
print("; type_mode=");
print_i8(type_mode);
print_nl();
print("prefix\n");
case token is
when COMMA: return;
when ASSIGN: return;
when SEMICOLON: return;
when OPENPAREN:
not_allowed_in_type();
PushOp(OPENPAREN|UNARY_FLAG);
LexerGetToken();
continue;
when OPENSQ:
PushOp(OPENSQ|UNARY_FLAG);
LexerGetToken();
continue;
when NUMBER:
not_allowed_in_type();
print("push const ");
print_i32(token_value as uint32);
print_nl();
PushNode(MidConstant(token_value));
LexerGetToken();
when ID:
sym := ParseExistingSymbol();
sub not_a(kind: string) is
StartError();
print(sym.name);
print(" is not a ");
print(kind);
EndError();
end sub;
if type_mode == 0 then
# Parse as expression.
case sym.kind is
when CONST:
node := MidConstant(sym.constant);
when VAR:
node := MidAddress(sym, 0);
node.type := MakePointerType(sym.vardata.type);
node := MakeLValue(node);
when TYPE:
# Subroutine instances are passed around as their type, even if it
# does not *really* make sense. Every type must have a symbol, so
# by using the type as a subroutine literal, we save on having to
# create an extra symbol for it.
if sym.typedata.kind == TYPE_SUBROUTINE then
node := MidSubref(sym.typedata.subrtype.subr);
node.type := sym.typedata.subrtype.subr.intfsubr.type;
else
not_a("value");
end if;
when else:
not_a("value");
end case;
PushNode(node);
else
# Parse as type.
if sym.kind == 0 then
var type := AllocNewType();
type.kind := TYPE_PARTIAL;
sym.kind := TYPE;
sym.typedata := type;
type.symbol := sym;
end if;
if sym.kind != TYPE then
not_a("type");
end if;
PushType(sym.typedata);
end if;
when else:
if (f & 0xf0) != 0 then
pushop(UNARY_FLAG | token);
LexerGetToken();
continue; # next token is another term
else
StartError();
print("invalid token ");
PrintTokenName(token);
print(" in expression");
EndError();
end if;
end case;
loop
f := 0;
if token < @sizeof token_flags then
f := token_flags[token];
end if;
# Infix.
print("infix\n");
case token is
when CLOSEPAREN:
print("closing parenthesis\n");
if opsp == startopsp then
return;
end if;
while opsp != startopsp loop
op := peekop();
applyop();
if op == (UNARY_FLAG|OPENPAREN) then
break;
end if;
end loop;
LexerGetToken();
when CLOSESQ:
print("closing square bracket\n");
while opsp != startopsp loop
op := peekop();
applyop();
if (op == OPENSQ) or (op == OPENSQ_TYPE) then
break;
end if;
end loop;
LexerGetToken();
when else:
if (f & 0x0f) != 0 then
if (token == COMMA) and (opsp == 0) then
return;
end if;
if (type_mode != 0) and (token == OPENSQ) then
token := OPENSQ_TYPE;
type_mode := 0;
end if;
pushop(token);
LexerGetToken();
break; # next token is a term
else
return;
end if;
end case;
end loop;
end loop;
end sub;
parse();
while opsp != startopsp loop
applyop();
end loop;
if sp != (startsp+1) then
StartError();
print("syntax error: unterminated expression at ");
PrintTokenName(token);
EndError();
end if;
end sub;
sub ParseExpression(): (node: [Node]) is
type_mode := 0;
ParseRaw();
node := PopNode();
end sub;
sub ParseType(): (type: [Type]) is
type_mode := 1;
ParseRaw();
type := PopType();
end sub;
sub ParseVar() is
LexerGetToken();
var sym := ParseNewSymbol(current_subr);
var type: [Type] := 0 as [Type];
if token == COLON then
LexerGetToken();
type := ParseType();
end if;
var value: [Node] := (0 as [Node]);
if token == ASSIGN then
LexerGetToken();
value := ParseExpression();
if type == (0 as [Type]) then
type := value.type;
if type == (0 as [Type]) then
SimpleError("types cannot be inferred for numeric constants");
end if;
if IsScalar(type) == 0 then
SimpleError("you can only assign scalar values");
end if;
end if;
end if;
if type == (0 as [Type]) then
SimpleError("type inference can't be used with a value to infer from");
end if;
InitVariable(current_subr, sym, type);
if value != (0 as [Node]) then
CheckExpressionType(value, sym.vardata.type);
var w := value.type.width as uint8;
Generate(MidStore(w, value, MidDeref(w, MidAddress(sym, 0))));
end if;
RequireToken(SEMICOLON);
end sub;
sub ParseExpressionStatement() is
var lvalue: [Node] := ParseExpression();
case token is
when SEMICOLON:
# Bare expressions must be void procedure calls.
when ASSIGN:
# Either a simple or a multi assignment.
RequireToken(ASSIGN);
var rvalue: [Node] := ParseExpression();
var type := lvalue.type;
var address := UndoLValue(lvalue);
CheckExpressionType(rvalue, type);
var w := type.width as uint8;
Generate(MidStore(w, rvalue, MidDeref(w, address)));
when else:
SyntaxError(token);
end case;
RequireToken(SEMICOLON);
end sub;
sub ParseIf() is
LexerGetToken();
PushIfData();
true_label := AllocLabel();
false_label := AllocLabel();
exit_label := AllocLabel();
false_generated := 0;
var cond := ParseExpression();
cond.beq.truelabel := true_label;
cond.beq.falselabel := false_label;
cond.beq.fallthrough := true_label;
GenerateConditional(cond);
RequireToken(THEN);
end sub;
sub CheckElseIsLast() is
if false_generated != 0 then
SimpleError("'else' must be last");
end if;
end sub;
sub ParseElseif() is
LexerGetToken();
CheckStructure(IF);
CheckElseIsLast();
Generate(MidJump(exit_label));
Generate(MidLabel(false_label));
true_label := AllocLabel();
false_label := AllocLabel();
# false_generated is 0
var cond := ParseExpression();
cond.beq.truelabel := true_label;
cond.beq.falselabel := false_label;
cond.beq.fallthrough := true_label;
GenerateConditional(cond);
RequireToken(THEN);
end sub;
sub ParseElse() is
LexerGetToken();
CheckStructure(IF);
CheckElseIsLast();
Generate(MidJump(exit_label));
Generate(MidLabel(false_label));
false_generated := 1;
end sub;
sub ParseEndIf() is
LexerGetToken();
CheckStructure(IF);
if false_generated == 0 then
Generate(MidLabel(false_label));
end if;
Generate(MidLabel(exit_label));
PopIfData();
RequireToken(SEMICOLON);
end sub;
sub ParseLoop() is
LexerGetToken();
PushLoopData();
break_label := AllocLabel();
continue_label := AllocLabel();
Generate(MidLabel(continue_label));
end sub;
sub ParseWhile() is
ParseLoop();
var cond := ParseExpression();
cond.beq.truelabel := AllocLabel();
cond.beq.falselabel := break_label;
cond.beq.fallthrough := cond.beq.truelabel;
GenerateConditional(cond);
RequireToken(LOOP);
end sub;
sub ParseEndLoop() is
LexerGetToken();
Generate(MidJump(continue_label));
Generate(MidLabel(break_label));
PopLoopData();
RequireToken(SEMICOLON);
end sub;
sub ParseContinue() is
LexerGetToken();
if continue_label == 0 then
SimpleError("nowhere to continue to");
end if;
Generate(MidJump(continue_label));
RequireToken(SEMICOLON);
end sub;
sub ParseBreak() is
LexerGetToken();
if break_label == 0 then
SimpleError("nowhere to break to");
end if;
Generate(MidJump(break_label));
RequireToken(SEMICOLON);
end sub;
sub ParseAsm() is
sub bad_reference() is
SimpleError("you can only emit references to variables, subroutines, or constants");
end sub;
LexerGetToken();
Generate(MidAsmstart());
loop
case token is
when STRING:
Generate(MidAsmtext(&token_buffer[0]));
LexerGetToken();
when NUMBER:
Generate(MidAsmvalue(token_value));
LexerGetToken();
when ID:
var sym := ParseExistingSymbol();
case sym.kind is
when TYPE:
if IsSubroutine(sym.typedata) != 0 then
EmitterReferenceSubroutine(current_subr, sym.typedata.subrtype.subr);
Generate(MidAsmsubref(sym.typedata.subrtype.subr));
else
bad_reference();
end if;
when VAR:
Generate(MidAsmsymbol(sym));
when CONST:
Generate(MidAsmvalue(sym.constant));
when else:
bad_reference();
end case;
when else:
SyntaxError(token);
end case;
if token == SEMICOLON then
break;
end if;
RequireToken(COMMA);
end loop;
Generate(MidAsmend());
RequireToken(SEMICOLON);
end sub;
sub ParseParameterList(subr: [Subroutine]): (first: [Symbol]) is
RequireToken(OPENPAREN);
first := nil;
var lastsym: [Symbol] := nil;
while token != CLOSEPAREN loop
var sym := ParseNewSymbol(subr);
RequireToken(COLON);
var type := ParseType();
InitVariable(subr, sym, type);
if first != nil then
first := sym;
end if;
if lastsym != nil then
lastsym.vardata.next_parameter := sym;
end if;
lastsym := sym;
if token == CLOSEPAREN then
break;
end if;
RequireToken(COMMA);
end loop;
RequireToken(CLOSEPAREN);
end sub;
sub ParseSub() is
LexerGetToken();
# Create the new subroutine.
var sym := ParseNewSymbol(current_subr);
var subr := InternalAlloc(@bytesof Subroutine) as [Subroutine];
subr.namespace.parent := ¤t_subr.namespace;
subr.parent := current_subr;
subr.id := AllocSubrId();