forked from JoeStrout/miniscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiniscriptParser.cs
1320 lines (1190 loc) · 51.5 KB
/
MiniscriptParser.cs
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
/* MiniscriptParser.cs
This file is responsible for parsing MiniScript source code, and converting
it into an internal format (a three-address byte code) that is considerably
faster to execute.
This is normally wrapped by the Interpreter class, so you probably don't
need to deal with Parser directly.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
namespace Miniscript {
public class Parser {
public string errorContext; // name of file, etc., used for error reporting
//public int lineNum; // which line number we're currently parsing
// BackPatch: represents a place where we need to patch the code to fill
// in a jump destination (once we figure out where that destination is).
class BackPatch {
public int lineNum; // which code line to patch
public string waitingFor; // what keyword we're waiting for (e.g., "end if")
}
// JumpPoint: represents a place in the code we will need to jump to later
// (typically, the top of a loop of some sort).
class JumpPoint {
public int lineNum; // line number to jump to
public string keyword; // jump type, by keyword: "while", "for", etc.
}
class ParseState {
public List<TAC.Line> code = new List<TAC.Line>();
public List<BackPatch> backpatches = new List<BackPatch>();
public List<JumpPoint> jumpPoints = new List<JumpPoint>();
public int nextTempNum = 0;
public string localOnlyIdentifier; // identifier to be looked up in local scope *only*
public bool localOnlyStrict; // whether localOnlyIdentifier applies strictly, or merely warns
public void Add(TAC.Line line) {
code.Add(line);
}
/// <summary>
/// Add the last code line as a backpatch point, to be patched
/// (in rhsA) when we encounter a line with the given waitFor.
/// </summary>
/// <param name="waitFor">Wait for.</param>
public void AddBackpatch(string waitFor) {
backpatches.Add(new BackPatch() { lineNum=code.Count-1, waitingFor=waitFor });
}
public void AddJumpPoint(string jumpKeyword) {
jumpPoints.Add(new JumpPoint() { lineNum = code.Count, keyword = jumpKeyword });
}
public JumpPoint CloseJumpPoint(string keyword) {
int idx = jumpPoints.Count - 1;
if (idx < 0 || jumpPoints[idx].keyword != keyword) {
throw new CompilerException(string.Format("'end {0}' without matching '{0}'", keyword));
}
JumpPoint result = jumpPoints[idx];
jumpPoints.RemoveAt(idx);
return result;
}
// Return whether the given line is a jump target.
public bool IsJumpTarget(int lineNum) {
for (int i=0; i < code.Count; i++) {
var op = code[i].op;
if ((op == TAC.Line.Op.GotoA || op == TAC.Line.Op.GotoAifB
|| op == TAC.Line.Op.GotoAifNotB || op == TAC.Line.Op.GotoAifTrulyB)
&& code[i].rhsA is ValNumber && code[i].rhsA.IntValue() == lineNum) return true;
}
for (int i=0; i<jumpPoints.Count(); i++) {
if (jumpPoints[i].lineNum == lineNum) return true;
}
return false;
}
/// <summary>
/// Call this method when we've found an 'end' keyword, and want
/// to patch up any jumps that were waiting for that. Patch the
/// matching backpatch (and any after it) to the current code end.
/// </summary>
/// <param name="keywordFound">Keyword found.</param>
/// <param name="reservingLines">Extra lines (after the current position) to patch to.</param>
public void Patch(string keywordFound, int reservingLines=0) {
Patch(keywordFound, false, reservingLines);
}
/// <summary>
/// Call this method when we've found an 'end' keyword, and want
/// to patch up any jumps that were waiting for that. Patch the
/// matching backpatch (and any after it) to the current code end.
/// </summary>
/// <param name="keywordFound">Keyword found.</param>
/// <param name="alsoBreak">If true, also patch "break"; otherwise skip it.</param>
/// <param name="reservingLines">Extra lines (after the current position) to patch to.</param>
public void Patch(string keywordFound, bool alsoBreak, int reservingLines=0) {
Value target = TAC.Num(code.Count + reservingLines);
bool done = false;
for (int idx = backpatches.Count - 1; idx >= 0 && !done; idx--) {
bool patchIt = false;
if (backpatches[idx].waitingFor == keywordFound) patchIt = done = true;
else if (backpatches[idx].waitingFor == "break") {
// Not the expected keyword, but "break"; this is always OK,
// but we may or may not patch it depending on the call.
patchIt = alsoBreak;
} else {
// Not the expected patch, and not "break"; we have a mismatched block start/end.
throw new CompilerException("'" + keywordFound + "' skips expected '" + backpatches[idx].waitingFor + "'");
}
if (patchIt) {
code[backpatches[idx].lineNum].rhsA = target;
backpatches.RemoveAt(idx);
}
}
// Make sure we found one...
if (!done) throw new CompilerException("'" + keywordFound + "' without matching block starter");
}
/// <summary>
/// Patches up all the branches for a single open if block. That includes
/// the last "else" block, as well as one or more "end if" jumps.
/// </summary>
public void PatchIfBlock(bool singleLineIf) {
Value target = TAC.Num(code.Count);
int idx = backpatches.Count - 1;
while (idx >= 0) {
BackPatch bp = backpatches[idx];
if (bp.waitingFor == "if:MARK") {
// There's the special marker that indicates the true start of this if block.
backpatches.RemoveAt(idx);
return;
} else if (bp.waitingFor == "end if" || bp.waitingFor == "else") {
code[bp.lineNum].rhsA = target;
backpatches.RemoveAt(idx);
} else if (backpatches[idx].waitingFor == "break") {
// Not the expected keyword, but "break"; this is always OK.
} else {
// Not the expected patch, and not "break"; we have a mismatched block start/end.
string msg;
if (singleLineIf) {
if (bp.waitingFor == "end for" || bp.waitingFor == "end while") {
msg = "loop is invalid within single-line 'if'";
} else {
msg = "invalid control structure within single-line 'if'";
}
} else {
msg = "'end if' without matching 'if'";
}
throw new CompilerException(msg);
}
idx--;
}
// If we get here, we never found the expected if:MARK. That's an error.
throw new CompilerException("'end if' without matching 'if'");
}
}
// Partial input, in the case where line continuation has been used.
string partialInput;
// List of open code blocks we're working on (while compiling a function,
// we push a new one onto this stack, compile to that, and then pop it
// off when we reach the end of the function).
Stack<ParseState> outputStack;
// Handy reference to the top of outputStack.
ParseState output;
// A new parse state that needs to be pushed onto the stack, as soon as we
// finish with the current line we're working on:
ParseState pendingState = null;
public Parser() {
Reset();
}
/// <summary>
/// Completely clear out and reset our parse state, throwing out
/// any code and intermediate results.
/// </summary>
public void Reset() {
output = new ParseState();
if (outputStack == null) outputStack = new Stack<ParseState>();
else outputStack.Clear();
outputStack.Push(output);
}
/// <summary>
/// Partially reset, abandoning backpatches, but keeping already-
/// compiled code. This would be used in a REPL, when the user
/// may want to reset and continue after a botched loop or function.
/// </summary>
public void PartialReset() {
if (outputStack == null) outputStack = new Stack<ParseState>();
while (outputStack.Count > 1) outputStack.Pop();
output = outputStack.Peek();
output.backpatches.Clear();
output.jumpPoints.Clear();
output.nextTempNum = 0;
partialInput = null;
pendingState = null;
}
public bool NeedMoreInput() {
if (!string.IsNullOrEmpty(partialInput)) return true;
if (outputStack.Count > 1) return true;
if (output.backpatches.Count > 0) return true;
return false;
}
/// <summary>
/// Return whether the given source code ends in a token that signifies that
/// the statement continues on the next line. That includes binary operators,
/// open brackets or parentheses, etc.
/// </summary>
/// <param name="sourceCode">source code to analyze</param>
/// <returns>true if line continuation is called for; false otherwise</returns>
public static bool EndsWithLineContinuation(string sourceCode) {
try {
Token lastTok = Lexer.LastToken(sourceCode);
// Almost any token at the end will signify line continuation, except:
switch (lastTok.type) {
case Token.Type.EOL:
case Token.Type.Identifier:
case Token.Type.Number:
case Token.Type.RCurly:
case Token.Type.RParen:
case Token.Type.RSquare:
case Token.Type.String:
case Token.Type.Unknown:
return false;
case Token.Type.Keyword:
// of keywords, only these can cause line continuation:
return lastTok.text == "and" || lastTok.text == "or" || lastTok.text == "isa"
|| lastTok.text == "not" || lastTok.text == "new";
default:
return true;
}
} catch (LexerException) {
return false;
}
}
void CheckForOpenBackpatches(int sourceLineNum) {
if (output.backpatches.Count == 0) return;
BackPatch bp = output.backpatches[output.backpatches.Count - 1];
string msg;
switch (bp.waitingFor) {
case "end for":
msg = "'for' without matching 'end for'";
break;
case "end if":
case "else":
msg = "'if' without matching 'end if'";
break;
case "end while":
msg = "'while' without matching 'end while'";
break;
default:
msg = "unmatched block opener";
break;
}
throw new CompilerException(errorContext, sourceLineNum, msg);
}
public void Parse(string sourceCode, bool replMode=false) {
if (replMode) {
// Check for an incomplete final line by finding the last (non-comment) token.
bool isPartial = EndsWithLineContinuation(sourceCode);
if (isPartial) {
partialInput += Lexer.TrimComment(sourceCode);
partialInput += " ";
return;
}
}
Lexer tokens = new Lexer(partialInput + sourceCode);
partialInput = null;
ParseMultipleLines(tokens);
if (!replMode && NeedMoreInput()) {
// Whoops, we need more input but we don't have any. This is an error.
tokens.lineNum++; // (so we report PAST the last line, making it clear this is an EOF problem)
if (outputStack.Count > 1) {
throw new CompilerException(errorContext, tokens.lineNum,
"'function' without matching 'end function'");
}
CheckForOpenBackpatches(tokens.lineNum);
}
}
/// <summary>
/// Create a virtual machine loaded with the code we have parsed.
/// </summary>
/// <param name="standardOutput"></param>
/// <returns></returns>
public TAC.Machine CreateVM(TextOutputMethod standardOutput) {
TAC.Context root = new TAC.Context(output.code);
return new TAC.Machine(root, standardOutput);
}
/// <summary>
/// Create a Function with the code we have parsed, for use as
/// an import. That means, it runs all that code, then at the
/// end it returns `locals` so that the caller can get its symbols.
/// </summary>
/// <returns></returns>
public Function CreateImport() {
// Add one additional line to return `locals` as the function return value.
ValVar locals = new ValVar("locals");
output.Add(new TAC.Line(TAC.LTemp(0), TAC.Line.Op.ReturnA, locals));
// Then wrap the whole thing in a Function.
var result = new Function(output.code);
return result;
}
public void REPL(string line) {
Parse(line);
TAC.Machine vm = CreateVM(null);
while (!vm.done) vm.Step();
}
void AllowLineBreak(Lexer tokens) {
while (tokens.Peek().type == Token.Type.EOL && !tokens.AtEnd) tokens.Dequeue();
}
delegate Value ExpressionParsingMethod(Lexer tokens, bool asLval=false, bool statementStart=false);
/// <summary>
/// Parse multiple statements until we run out of tokens, or reach 'end function'.
/// </summary>
/// <param name="tokens">Tokens.</param>
void ParseMultipleLines(Lexer tokens) {
while (!tokens.AtEnd) {
// Skip any blank lines
if (tokens.Peek().type == Token.Type.EOL) {
tokens.Dequeue();
continue;
}
// Prepare a source code location for error reporting
SourceLoc location = new SourceLoc(errorContext, tokens.lineNum);
// Pop our context if we reach 'end function'.
if (tokens.Peek().type == Token.Type.Keyword && tokens.Peek().text == "end function") {
tokens.Dequeue();
if (outputStack.Count > 1) {
CheckForOpenBackpatches(tokens.lineNum);
outputStack.Pop();
output = outputStack.Peek();
} else {
CompilerException e = new CompilerException("'end function' without matching block starter");
e.location = location;
throw e;
}
continue;
}
// Parse one line (statement).
int outputStart = output.code.Count;
try {
ParseStatement(tokens);
} catch (MiniscriptException mse) {
if (mse.location == null) mse.location = location;
throw;
}
// Fill in the location info for all the TAC lines we just generated.
for (int i = outputStart; i < output.code.Count; i++) {
output.code[i].location = location;
}
}
}
void ParseStatement(Lexer tokens, bool allowExtra=false) {
if (tokens.Peek().type == Token.Type.Keyword && tokens.Peek().text != "not"
&& tokens.Peek().text != "true" && tokens.Peek().text != "false") {
// Handle statements that begin with a keyword.
string keyword = tokens.Dequeue().text;
switch (keyword) {
case "return":
{
Value returnValue = null;
if (tokens.Peek().type != Token.Type.EOL && tokens.Peek().text != "else" && tokens.Peek().text != "else if") {
returnValue = ParseExpr(tokens);
}
output.Add(new TAC.Line(TAC.LTemp(0), TAC.Line.Op.ReturnA, returnValue));
}
break;
case "if":
{
Value condition = ParseExpr(tokens);
RequireToken(tokens, Token.Type.Keyword, "then");
// OK, now we need to emit a conditional branch, but keep track of this
// on a stack so that when we get the corresponding "else" or "end if",
// we can come back and patch that jump to the right place.
output.Add(new TAC.Line(null, TAC.Line.Op.GotoAifNotB, null, condition));
// ...but if blocks also need a special marker in the backpack stack
// so we know where to stop when patching up (possibly multiple) 'end if' jumps.
// We'll push a special dummy backpatch here that we look for in PatchIfBlock.
output.AddBackpatch("if:MARK");
output.AddBackpatch("else");
// Allow for the special one-statement if: if the next token after "then"
// is not EOL, then parse a statement, and do the same for any else or
// else-if blocks, until we get to EOL (and then implicitly do "end if").
if (tokens.Peek().type != Token.Type.EOL) {
ParseStatement(tokens, true); // parses a single statement for the "then" body
if (tokens.Peek().type == Token.Type.Keyword && tokens.Peek().text == "else") {
tokens.Dequeue(); // skip "else"
StartElseClause();
ParseStatement(tokens, true); // parse a single statement for the "else" body
} else if (tokens.Peek().type == Token.Type.Keyword && tokens.Peek().text == "else if") {
tokens.Peek().text = "if"; // the trick: convert the "else if" token to a regular "if"...
StartElseClause(); // but start an else clause...
ParseStatement(tokens, true); // then parse a single statement starting with "if"
} else {
RequireEitherToken(tokens, Token.Type.Keyword, "else", Token.Type.EOL);
}
output.PatchIfBlock(true); // terminate the single-line if
} else {
tokens.Dequeue(); // skip EOL
}
}
return;
case "else":
StartElseClause();
break;
case "else if":
{
StartElseClause();
Value condition = ParseExpr(tokens);
RequireToken(tokens, Token.Type.Keyword, "then");
output.Add(new TAC.Line(null, TAC.Line.Op.GotoAifNotB, null, condition));
output.AddBackpatch("else");
}
break;
case "end if":
// OK, this is tricky. We might have an open "else" block or we might not.
// And, we might have multiple open "end if" jumps (one for the if part,
// and another for each else-if part). Patch all that as a special case.
output.PatchIfBlock(false);
break;
case "while":
{
// We need to note the current line, so we can jump back up to it at the end.
output.AddJumpPoint(keyword);
// Then parse the condition.
Value condition = ParseExpr(tokens);
// OK, now we need to emit a conditional branch, but keep track of this
// on a stack so that when we get the corresponding "end while",
// we can come back and patch that jump to the right place.
output.Add(new TAC.Line(null, TAC.Line.Op.GotoAifNotB, null, condition));
output.AddBackpatch("end while");
}
break;
case "end while":
{
// Unconditional jump back to the top of the while loop.
JumpPoint jump = output.CloseJumpPoint("while");
output.Add(new TAC.Line(null, TAC.Line.Op.GotoA, TAC.Num(jump.lineNum)));
// Then, backpatch the open "while" branch to here, right after the loop.
// And also patch any "break" branches emitted after that point.
output.Patch(keyword, true);
}
break;
case "for":
{
// Get the loop variable, "in" keyword, and expression to loop over.
// (Note that the expression is only evaluated once, before the loop.)
Token loopVarTok = RequireToken(tokens, Token.Type.Identifier);
ValVar loopVar = new ValVar(loopVarTok.text);
RequireToken(tokens, Token.Type.Keyword, "in");
Value stuff = ParseExpr(tokens);
if (stuff == null) {
throw new CompilerException(errorContext, tokens.lineNum,
"sequence expression expected for 'for' loop");
}
// Create an index variable to iterate over the sequence, initialized to -1.
ValVar idxVar = new ValVar("__" + loopVarTok.text + "_idx");
output.Add(new TAC.Line(idxVar, TAC.Line.Op.AssignA, TAC.Num(-1)));
// We need to note the current line, so we can jump back up to it at the end.
output.AddJumpPoint(keyword);
// Now increment the index variable, and branch to the end if it's too big.
// (We'll have to backpatch this branch later.)
output.Add(new TAC.Line(idxVar, TAC.Line.Op.APlusB, idxVar, TAC.Num(1)));
ValTemp sizeOfSeq = new ValTemp(output.nextTempNum++);
output.Add(new TAC.Line(sizeOfSeq, TAC.Line.Op.LengthOfA, stuff));
ValTemp isTooBig = new ValTemp(output.nextTempNum++);
output.Add(new TAC.Line(isTooBig, TAC.Line.Op.AGreatOrEqualB, idxVar, sizeOfSeq));
output.Add(new TAC.Line(null, TAC.Line.Op.GotoAifB, null, isTooBig));
output.AddBackpatch("end for");
// Otherwise, get the sequence value into our loop variable.
output.Add(new TAC.Line(loopVar, TAC.Line.Op.ElemBofIterA, stuff, idxVar));
}
break;
case "end for":
{
// Unconditional jump back to the top of the for loop.
JumpPoint jump = output.CloseJumpPoint("for");
output.Add(new TAC.Line(null, TAC.Line.Op.GotoA, TAC.Num(jump.lineNum)));
// Then, backpatch the open "for" branch to here, right after the loop.
// And also patch any "break" branches emitted after that point.
output.Patch(keyword, true);
}
break;
case "break":
{
// Emit a jump to the end, to get patched up later.
if (output.jumpPoints.Count == 0) {
throw new CompilerException(errorContext, tokens.lineNum,
"'break' without open loop block");
}
output.Add(new TAC.Line(null, TAC.Line.Op.GotoA));
output.AddBackpatch("break");
}
break;
case "continue":
{
// Jump unconditionally back to the current open jump point.
if (output.jumpPoints.Count == 0) {
throw new CompilerException(errorContext, tokens.lineNum,
"'continue' without open loop block");
}
JumpPoint jump = output.jumpPoints.Last();
output.Add(new TAC.Line(null, TAC.Line.Op.GotoA, TAC.Num(jump.lineNum)));
}
break;
default:
throw new CompilerException(errorContext, tokens.lineNum,
"unexpected keyword '" + keyword + "' at start of line");
}
} else {
ParseAssignment(tokens, allowExtra);
}
// A statement should consume everything to the end of the line.
if (!allowExtra) RequireToken(tokens, Token.Type.EOL);
// Finally, if we have a pending state, because we encountered a function(),
// then push it onto our stack now that we're done with that statement.
if (pendingState != null) {
output = pendingState;
outputStack.Push(output);
pendingState = null;
}
}
void StartElseClause() {
// Back-patch the open if block, but leaving room for the jump:
// Emit the jump from the current location, which is the end of an if-block,
// to the end of the else block (which we'll have to back-patch later).
output.Add(new TAC.Line(null, TAC.Line.Op.GotoA, null));
// Back-patch the previously open if-block to jump here (right past the goto).
output.Patch("else");
// And open a new back-patch for this goto (which will jump all the way to the end if).
output.AddBackpatch("end if");
}
void ParseAssignment(Lexer tokens, bool allowExtra=false) {
Value expr = ParseExpr(tokens, true, true);
Value lhs, rhs;
Token peek = tokens.Peek();
if (peek.type == Token.Type.EOL ||
(peek.type == Token.Type.Keyword && (peek.text == "else" || peek.text == "else if"))) {
// No explicit assignment; store an implicit result
rhs = FullyEvaluate(expr);
output.Add(new TAC.Line(null, TAC.Line.Op.AssignImplicit, rhs));
return;
}
if (peek.type == Token.Type.OpAssign) {
tokens.Dequeue(); // skip '='
lhs = expr;
output.localOnlyIdentifier = null;
output.localOnlyStrict = false; // ToDo: make this always strict, and change "localOnly" to a simple bool
if (lhs is ValVar vv) output.localOnlyIdentifier = vv.identifier;
rhs = ParseExpr(tokens);
output.localOnlyIdentifier = null;
} else if (peek.type == Token.Type.OpAssignPlus || peek.type == Token.Type.OpAssignMinus
|| peek.type == Token.Type.OpAssignTimes || peek.type == Token.Type.OpAssignDivide
|| peek.type == Token.Type.OpAssignMod || peek.type == Token.Type.OpAssignPower) {
var op = TAC.Line.Op.APlusB;
switch (tokens.Dequeue().type) {
case Token.Type.OpAssignMinus: op = TAC.Line.Op.AMinusB; break;
case Token.Type.OpAssignTimes: op = TAC.Line.Op.ATimesB; break;
case Token.Type.OpAssignDivide: op = TAC.Line.Op.ADividedByB; break;
case Token.Type.OpAssignMod: op = TAC.Line.Op.AModB; break;
case Token.Type.OpAssignPower: op = TAC.Line.Op.APowB; break;
default: break;
}
lhs = expr;
output.localOnlyIdentifier = null;
output.localOnlyStrict = true;
if (lhs is ValVar vv) output.localOnlyIdentifier = vv.identifier;
rhs = ParseExpr(tokens);
var opA = FullyEvaluate(lhs, ValVar.LocalOnlyMode.Strict);
Value opB = FullyEvaluate(rhs);
int tempNum = output.nextTempNum++;
output.Add(new TAC.Line(TAC.LTemp(tempNum), op, opA, opB));
rhs = TAC.RTemp(tempNum);
output.localOnlyIdentifier = null;
} else {
// This looks like a command statement. Parse the rest
// of the line as arguments to a function call.
Value funcRef = expr;
int argCount = 0;
while (true) {
Value arg = ParseExpr(tokens);
output.Add(new TAC.Line(null, TAC.Line.Op.PushParam, arg));
argCount++;
if (tokens.Peek().type == Token.Type.EOL) break;
if (tokens.Peek().type == Token.Type.Keyword && (tokens.Peek().text == "else" || tokens.Peek().text == "else if")) break;
if (tokens.Peek().type == Token.Type.Comma) {
tokens.Dequeue();
AllowLineBreak(tokens);
continue;
}
if (RequireEitherToken(tokens, Token.Type.Comma, Token.Type.EOL).type == Token.Type.EOL) break;
}
ValTemp result = new ValTemp(output.nextTempNum++);
output.Add(new TAC.Line(result, TAC.Line.Op.CallFunctionA, funcRef, TAC.Num(argCount)));
output.Add(new TAC.Line(null, TAC.Line.Op.AssignImplicit, result));
return;
}
// Now we need to assign the value in rhs to the lvalue in lhs.
// First, check for the case where lhs is a temp; that indicates it is not an lvalue
// (for example, it might be a list slice).
if (lhs is ValTemp) {
throw new CompilerException(errorContext, tokens.lineNum, "invalid assignment (not an lvalue)");
}
// OK, now, in many cases our last TAC line at this point is an assignment to our RHS temp.
// In that case, as a simple (but very useful) optimization, we can simply patch that to
// assign to our lhs instead. BUT, we must not do this if there are any jumps to the next
// line, as may happen due to short-cut evaluation (issue #6).
if (rhs is ValTemp && output.code.Count > 0 && !output.IsJumpTarget(output.code.Count)) {
TAC.Line line = output.code[output.code.Count - 1];
if (line.lhs.Equals(rhs)) {
// Yep, that's the case. Patch it up.
line.lhs = lhs;
return;
}
}
// If the last line was us creating and assigning a function, then we don't add a second assign
// op, we instead just update that line with the proper LHS
if (rhs is ValFunction && output.code.Count > 0) {
TAC.Line line = output.code[output.code.Count - 1];
if (line.op == TAC.Line.Op.BindAssignA) {
line.lhs = lhs;
return;
}
}
// In any other case, do an assignment statement to our lhs.
output.Add(new TAC.Line(lhs, TAC.Line.Op.AssignA, rhs));
}
Value ParseExpr(Lexer tokens, bool asLval=false, bool statementStart=false) {
ExpressionParsingMethod nextLevel = ParseFunction;
return nextLevel(tokens, asLval, statementStart);
}
Value ParseFunction(Lexer tokens, bool asLval=false, bool statementStart=false) {
ExpressionParsingMethod nextLevel = ParseOr;
Token tok = tokens.Peek();
if (tok.type != Token.Type.Keyword || tok.text != "function") return nextLevel(tokens, asLval, statementStart);
tokens.Dequeue();
Function func = new Function(null);
tok = tokens.Peek();
if (tok.type != Token.Type.EOL) {
var paren = RequireToken(tokens, Token.Type.LParen);
while (tokens.Peek().type != Token.Type.RParen) {
// parse a parameter: a comma-separated list of
// identifier
// or... identifier = constant
Token id = tokens.Dequeue();
if (id.type != Token.Type.Identifier) throw new CompilerException(errorContext, tokens.lineNum,
"got " + id + " where an identifier is required");
Value defaultValue = null;
if (tokens.Peek().type == Token.Type.OpAssign) {
tokens.Dequeue(); // skip '='
defaultValue = ParseExpr(tokens);
// Ensure the default value is a constant, not an expression.
if (defaultValue is ValTemp) {
throw new CompilerException(errorContext, tokens.lineNum,
"parameter default value must be a literal value");
}
}
func.parameters.Add(new Function.Param(id.text, defaultValue));
if (tokens.Peek().type == Token.Type.RParen) break;
RequireToken(tokens, Token.Type.Comma);
}
RequireToken(tokens, Token.Type.RParen);
}
// Now, we need to parse the function body into its own parsing context.
// But don't push it yet -- we're in the middle of parsing some expression
// or statement in the current context, and need to finish that.
if (pendingState != null) throw new CompilerException(errorContext, tokens.lineNum,
"can't start two functions in one statement");
pendingState = new ParseState();
pendingState.nextTempNum = 1; // (since 0 is used to hold return value)
// Console.WriteLine("STARTED FUNCTION");
// Create a function object attached to the new parse state code.
func.code = pendingState.code;
var valFunc = new ValFunction(func);
output.Add(new TAC.Line(null, TAC.Line.Op.BindAssignA, valFunc));
return valFunc;
}
Value ParseOr(Lexer tokens, bool asLval=false, bool statementStart=false) {
ExpressionParsingMethod nextLevel = ParseAnd;
Value val = nextLevel(tokens, asLval, statementStart);
List<TAC.Line> jumpLines = null;
Token tok = tokens.Peek();
while (tok.type == Token.Type.Keyword && tok.text == "or") {
tokens.Dequeue(); // discard "or"
val = FullyEvaluate(val);
AllowLineBreak(tokens); // allow a line break after a binary operator
// Set up a short-circuit jump based on the current value;
// we'll fill in the jump destination later. Note that the
// usual GotoAifB opcode won't work here, without breaking
// our calculation of intermediate truth. We need to jump
// only if our truth value is >= 1 (i.e. absolutely true).
TAC.Line jump = new TAC.Line(null, TAC.Line.Op.GotoAifTrulyB, null, val);
output.Add(jump);
if (jumpLines == null) jumpLines = new List<TAC.Line>();
jumpLines.Add(jump);
Value opB = nextLevel(tokens);
int tempNum = output.nextTempNum++;
output.Add(new TAC.Line(TAC.LTemp(tempNum), TAC.Line.Op.AOrB, val, opB));
val = TAC.RTemp(tempNum);
tok = tokens.Peek();
}
// Now, if we have any short-circuit jumps, those are going to need
// to copy the short-circuit result (always 1) to our output temp.
// And anything else needs to skip over that. So:
if (jumpLines != null) {
output.Add(new TAC.Line(null, TAC.Line.Op.GotoA, TAC.Num(output.code.Count+2))); // skip over this line:
output.Add(new TAC.Line(val, TAC.Line.Op.AssignA, ValNumber.one)); // result = 1
foreach (TAC.Line jump in jumpLines) {
jump.rhsA = TAC.Num(output.code.Count-1); // short-circuit to the above result=1 line
}
}
return val;
}
Value ParseAnd(Lexer tokens, bool asLval=false, bool statementStart=false) {
ExpressionParsingMethod nextLevel = ParseNot;
Value val = nextLevel(tokens, asLval, statementStart);
List<TAC.Line> jumpLines = null;
Token tok = tokens.Peek();
while (tok.type == Token.Type.Keyword && tok.text == "and") {
tokens.Dequeue(); // discard "and"
val = FullyEvaluate(val);
AllowLineBreak(tokens); // allow a line break after a binary operator
// Set up a short-circuit jump based on the current value;
// we'll fill in the jump destination later.
TAC.Line jump = new TAC.Line(null, TAC.Line.Op.GotoAifNotB, null, val);
output.Add(jump);
if (jumpLines == null) jumpLines = new List<TAC.Line>();
jumpLines.Add(jump);
Value opB = nextLevel(tokens);
int tempNum = output.nextTempNum++;
output.Add(new TAC.Line(TAC.LTemp(tempNum), TAC.Line.Op.AAndB, val, opB));
val = TAC.RTemp(tempNum);
tok = tokens.Peek();
}
// Now, if we have any short-circuit jumps, those are going to need
// to copy the short-circuit result (always 0) to our output temp.
// And anything else needs to skip over that. So:
if (jumpLines != null) {
output.Add(new TAC.Line(null, TAC.Line.Op.GotoA, TAC.Num(output.code.Count+2))); // skip over this line:
output.Add(new TAC.Line(val, TAC.Line.Op.AssignA, ValNumber.zero)); // result = 0
foreach (TAC.Line jump in jumpLines) {
jump.rhsA = TAC.Num(output.code.Count-1); // short-circuit to the above result=0 line
}
}
return val;
}
Value ParseNot(Lexer tokens, bool asLval=false, bool statementStart=false) {
ExpressionParsingMethod nextLevel = ParseIsA;
Token tok = tokens.Peek();
Value val;
if (tok.type == Token.Type.Keyword && tok.text == "not") {
tokens.Dequeue(); // discard "not"
AllowLineBreak(tokens); // allow a line break after a unary operator
val = nextLevel(tokens);
int tempNum = output.nextTempNum++;
output.Add(new TAC.Line(TAC.LTemp(tempNum), TAC.Line.Op.NotA, val));
val = TAC.RTemp(tempNum);
} else {
val = nextLevel(tokens, asLval, statementStart
);
}
return val;
}
Value ParseIsA(Lexer tokens, bool asLval=false, bool statementStart=false) {
ExpressionParsingMethod nextLevel = ParseComparisons;
Value val = nextLevel(tokens, asLval, statementStart);
if (tokens.Peek().type == Token.Type.Keyword && tokens.Peek().text == "isa") {
tokens.Dequeue(); // discard the isa operator
AllowLineBreak(tokens); // allow a line break after a binary operator
val = FullyEvaluate(val);
Value opB = nextLevel(tokens);
int tempNum = output.nextTempNum++;
output.Add(new TAC.Line(TAC.LTemp(tempNum), TAC.Line.Op.AisaB, val, opB));
val = TAC.RTemp(tempNum);
}
return val;
}
Value ParseComparisons(Lexer tokens, bool asLval=false, bool statementStart=false) {
ExpressionParsingMethod nextLevel = ParseAddSub;
Value val = nextLevel(tokens, asLval, statementStart);
Value opA = val;
TAC.Line.Op opcode = ComparisonOp(tokens.Peek().type);
// Parse a string of comparisons, all multiplied together
// (so every comparison must be true for the whole expression to be true).
bool firstComparison = true;
while (opcode != TAC.Line.Op.Noop) {
tokens.Dequeue(); // discard the operator (we have the opcode)
opA = FullyEvaluate(opA);
AllowLineBreak(tokens); // allow a line break after a binary operator
Value opB = nextLevel(tokens);
int tempNum = output.nextTempNum++;
output.Add(new TAC.Line(TAC.LTemp(tempNum), opcode, opA, opB));
if (firstComparison) {
firstComparison = false;
} else {
tempNum = output.nextTempNum++;
output.Add(new TAC.Line(TAC.LTemp(tempNum), TAC.Line.Op.ATimesB, val, TAC.RTemp(tempNum - 1)));
}
val = TAC.RTemp(tempNum);
opA = opB;
opcode = ComparisonOp(tokens.Peek().type);
}
return val;
}
// Find the TAC operator that corresponds to the given token type,
// for comparisons. If it's not a comparison operator, return TAC.Line.Op.Noop.
static TAC.Line.Op ComparisonOp(Token.Type tokenType) {
switch (tokenType) {
case Token.Type.OpEqual: return TAC.Line.Op.AEqualB;
case Token.Type.OpNotEqual: return TAC.Line.Op.ANotEqualB;
case Token.Type.OpGreater: return TAC.Line.Op.AGreaterThanB;
case Token.Type.OpGreatEqual: return TAC.Line.Op.AGreatOrEqualB;
case Token.Type.OpLesser: return TAC.Line.Op.ALessThanB;
case Token.Type.OpLessEqual: return TAC.Line.Op.ALessOrEqualB;
default: return TAC.Line.Op.Noop;
}
}
Value ParseAddSub(Lexer tokens, bool asLval=false, bool statementStart=false) {
ExpressionParsingMethod nextLevel = ParseMultDiv;
Value val = nextLevel(tokens, asLval, statementStart);
Token tok = tokens.Peek();
while (tok.type == Token.Type.OpPlus ||
(tok.type == Token.Type.OpMinus
&& (!statementStart || !tok.afterSpace || tokens.IsAtWhitespace()))) {
tokens.Dequeue();
AllowLineBreak(tokens); // allow a line break after a binary operator
val = FullyEvaluate(val);
Value opB = nextLevel(tokens);
int tempNum = output.nextTempNum++;
output.Add(new TAC.Line(TAC.LTemp(tempNum),
tok.type == Token.Type.OpPlus ? TAC.Line.Op.APlusB : TAC.Line.Op.AMinusB,
val, opB));
val = TAC.RTemp(tempNum);
tok = tokens.Peek();
}
return val;
}
Value ParseMultDiv(Lexer tokens, bool asLval=false, bool statementStart=false) {
ExpressionParsingMethod nextLevel = ParseUnaryMinus;
Value val = nextLevel(tokens, asLval, statementStart);
Token tok = tokens.Peek();
while (tok.type == Token.Type.OpTimes || tok.type == Token.Type.OpDivide || tok.type == Token.Type.OpMod) {
tokens.Dequeue();
AllowLineBreak(tokens); // allow a line break after a binary operator
val = FullyEvaluate(val);
Value opB = nextLevel(tokens);
int tempNum = output.nextTempNum++;
switch (tok.type) {
case Token.Type.OpTimes:
output.Add(new TAC.Line(TAC.LTemp(tempNum), TAC.Line.Op.ATimesB, val, opB));
break;
case Token.Type.OpDivide:
output.Add(new TAC.Line(TAC.LTemp(tempNum), TAC.Line.Op.ADividedByB, val, opB));
break;
case Token.Type.OpMod:
output.Add(new TAC.Line(TAC.LTemp(tempNum), TAC.Line.Op.AModB, val, opB));
break;
}
val = TAC.RTemp(tempNum);
tok = tokens.Peek();
}
return val;
}
Value ParseUnaryMinus(Lexer tokens, bool asLval=false, bool statementStart=false) {
ExpressionParsingMethod nextLevel = ParseNew;
if (tokens.Peek().type != Token.Type.OpMinus) return nextLevel(tokens, asLval, statementStart);
tokens.Dequeue(); // skip '-'
AllowLineBreak(tokens); // allow a line break after a unary operator
Value val = nextLevel(tokens);
if (val is ValNumber) {
// If what follows is a numeric literal, just invert it and be done!
ValNumber valnum = (ValNumber)val;
valnum.value = -valnum.value;
return valnum;
}
// Otherwise, subtract it from 0 and return a new temporary.
int tempNum = output.nextTempNum++;
output.Add(new TAC.Line(TAC.LTemp(tempNum), TAC.Line.Op.AMinusB, TAC.Num(0), val));
return TAC.RTemp(tempNum);
}
Value ParseNew(Lexer tokens, bool asLval=false, bool statementStart=false) {
ExpressionParsingMethod nextLevel = ParsePower;
if (tokens.Peek().type != Token.Type.Keyword || tokens.Peek().text != "new") return nextLevel(tokens, asLval, statementStart);
tokens.Dequeue(); // skip 'new'
AllowLineBreak(tokens); // allow a line break after a unary operator
Value isa = nextLevel(tokens);
Value result = new ValTemp(output.nextTempNum++);
output.Add(new TAC.Line(result, TAC.Line.Op.NewA, isa));
return result;
}
Value ParsePower(Lexer tokens, bool asLval=false, bool statementStart=false) {
ExpressionParsingMethod nextLevel = ParseAddressOf;
Value val = nextLevel(tokens, asLval, statementStart);
Token tok = tokens.Peek();
while (tok.type == Token.Type.OpPower) {
tokens.Dequeue();
AllowLineBreak(tokens); // allow a line break after a binary operator
val = FullyEvaluate(val);
Value opB = nextLevel(tokens);
int tempNum = output.nextTempNum++;
output.Add(new TAC.Line(TAC.LTemp(tempNum), TAC.Line.Op.APowB, val, opB));
val = TAC.RTemp(tempNum);
tok = tokens.Peek();
}