forked from OpenDreamProject/OpenDream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDMProcBuilder.cs
More file actions
896 lines (750 loc) · 38.7 KB
/
DMProcBuilder.cs
File metadata and controls
896 lines (750 loc) · 38.7 KB
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
using DMCompiler.Bytecode;
using DMCompiler.Compiler;
using DMCompiler.Compiler.DM;
using DMCompiler.Compiler.DM.AST;
using DMCompiler.DM.Expressions;
namespace DMCompiler.DM.Builders;
internal sealed class DMProcBuilder(DMCompiler compiler, DMObject dmObject, DMProc proc) {
private readonly DMExpressionBuilder _exprBuilder = new(new(compiler, dmObject, proc));
private ExpressionContext ExprContext => new(compiler, dmObject, proc);
public void ProcessProcDefinition(DMASTProcDefinition procDefinition) {
if (procDefinition.Body == null) return;
foreach (DMASTDefinitionParameter parameter in procDefinition.Parameters) {
string parameterName = parameter.Name;
if (parameter.Value != null) { //Parameter has a default value
string afterDefaultValueCheck = proc.NewLabelName();
DMReference parameterRef = proc.GetLocalVariableReference(parameterName, parameter.Location);
//Don't set parameter to default if not null
proc.PushReferenceValue(parameterRef);
proc.IsNull();
proc.JumpIfFalse(afterDefaultValueCheck);
//Set default
_exprBuilder.Emit(parameter.Value, parameter.ObjectType);
proc.Assign(parameterRef);
proc.Pop();
proc.AddLabel(afterDefaultValueCheck);
}
}
ProcessBlockInner(procDefinition.Body, silenceEmptyBlockWarning : true);
proc.ResolveLabels();
}
/// <param name="block">The block to process</param>
/// <param name="silenceEmptyBlockWarning">Used to avoid emitting noisy warnings about procs with nothing in them.<br/>
/// FIXME: Eventually we should try to be smart enough to emit the error anyway for procs that <br/>
/// A: are not marked opendream_unimplemented and <br/>
/// B: have no descendant proc which actually has code in it (implying that this proc is just some abstract virtual for it)
/// </param>
private void ProcessBlockInner(DMASTProcBlockInner block, bool silenceEmptyBlockWarning = false) {
if(!silenceEmptyBlockWarning && block.Statements.Length == 0) { // If this block has no real statements
// Not an error in BYOND, but we do have an emission for this!
if (block.SetStatements.Length != 0) {
// Give a more articulate message about this, since it's kinda weird
compiler.Emit(WarningCode.EmptyBlock,block.Location,"Empty block detected - set statements are executed outside of, before, and unconditional to, this block");
} else {
compiler.Emit(WarningCode.EmptyBlock,block.Location,"Empty block detected");
}
return;
}
foreach (DMASTProcStatement statement in block.Statements) {
proc.DebugSource(statement.Location);
ProcessStatement(statement);
}
}
private void ProcessStatement(DMASTProcStatement statement) {
switch (statement) {
case DMASTInvalidProcStatement: break;
case DMASTNullProcStatement: break;
case DMASTProcStatementExpression statementExpression: ProcessStatementExpression(statementExpression); break;
case DMASTProcStatementContinue statementContinue: ProcessStatementContinue(statementContinue); break;
case DMASTProcStatementGoto statementGoto: ProcessStatementGoto(statementGoto); break;
case DMASTProcStatementLabel statementLabel: ProcessStatementLabel(statementLabel); break;
case DMASTProcStatementBreak statementBreak: ProcessStatementBreak(statementBreak); break;
case DMASTProcStatementDel statementDel: ProcessStatementDel(statementDel); break;
case DMASTProcStatementSpawn statementSpawn: ProcessStatementSpawn(statementSpawn); break;
case DMASTProcStatementReturn statementReturn: ProcessStatementReturn(statementReturn); break;
case DMASTProcStatementIf statementIf: ProcessStatementIf(statementIf); break;
case DMASTProcStatementFor statementFor: ProcessStatementFor(statementFor); break;
case DMASTProcStatementInfLoop statementInfLoop: ProcessStatementInfLoop(statementInfLoop); break;
case DMASTProcStatementWhile statementWhile: ProcessStatementWhile(statementWhile); break;
case DMASTProcStatementDoWhile statementDoWhile: ProcessStatementDoWhile(statementDoWhile); break;
case DMASTProcStatementSwitch statementSwitch: ProcessStatementSwitch(statementSwitch); break;
case DMASTProcStatementBrowse statementBrowse: ProcessStatementBrowse(statementBrowse); break;
case DMASTProcStatementBrowseResource statementBrowseResource: ProcessStatementBrowseResource(statementBrowseResource); break;
case DMASTProcStatementOutputControl statementOutputControl: ProcessStatementOutputControl(statementOutputControl); break;
case DMASTProcStatementLink statementLink: ProcessStatementLink(statementLink); break;
case DMASTProcStatementFtp statementFtp: ProcessStatementFtp(statementFtp); break;
case DMASTProcStatementOutput statementOutput: ProcessStatementOutput(statementOutput); break;
case DMASTProcStatementInput statementInput: ProcessStatementInput(statementInput); break;
case DMASTProcStatementVarDeclaration varDeclaration: ProcessStatementVarDeclaration(varDeclaration); break;
case DMASTProcStatementTryCatch tryCatch: ProcessStatementTryCatch(tryCatch); break;
case DMASTProcStatementThrow dmThrow: ProcessStatementThrow(dmThrow); break;
//NOTE: Is there a more generic way of doing this, where Aggregate doesn't need every possible type state specified here?
// please write such generic thing if more than three aggregates show up in this switch.
case DMASTAggregate<DMASTProcStatementVarDeclaration> gregVar:
foreach (var declare in gregVar.Statements)
ProcessStatementVarDeclaration(declare);
break;
default:
compiler.ForcedError(statement.Location, $"Invalid proc statement {statement.GetType()}");
break;
}
}
private void ProcessStatementExpression(DMASTProcStatementExpression statement) {
_exprBuilder.Emit(statement.Expression);
proc.Pop();
}
private void ProcessStatementContinue(DMASTProcStatementContinue statementContinue) {
proc.Continue(statementContinue.Label);
}
private void ProcessStatementGoto(DMASTProcStatementGoto statementGoto) {
proc.Goto(statementGoto.Label);
}
private void ProcessStatementLabel(DMASTProcStatementLabel statementLabel) {
var codeLabel = proc.TryAddCodeLabel(statementLabel.Name);
var labelName = codeLabel?.LabelName ?? statementLabel.Name;
proc.AddLabel(labelName);
if (statementLabel.Body is not null) {
proc.StartScope();
{
ProcessBlockInner(statementLabel.Body);
}
proc.EndScope();
proc.AddLabel(labelName + "_end");
}
}
private void ProcessStatementBreak(DMASTProcStatementBreak statementBreak) {
proc.Break(statementBreak.Label);
}
private void ProcessStatementDel(DMASTProcStatementDel statementDel) {
_exprBuilder.Emit(statementDel.Value);
proc.DeleteObject();
}
private void ProcessStatementSpawn(DMASTProcStatementSpawn statementSpawn) {
_exprBuilder.Emit(statementSpawn.Delay);
string afterSpawnLabel = proc.NewLabelName();
proc.Spawn(afterSpawnLabel);
proc.StartScope();
{
ProcessBlockInner(statementSpawn.Body);
//Prevent the new thread from executing outside its own code
proc.PushNull();
proc.Return();
}
proc.EndScope();
proc.AddLabel(afterSpawnLabel);
}
/// <remarks>
/// Global/static var declarations are handled by <see cref="DMCodeTree.ProcGlobalVarNode" />
/// </remarks>
private void ProcessStatementVarDeclaration(DMASTProcStatementVarDeclaration varDeclaration) {
if (varDeclaration.IsGlobal) { return; }
DMExpression value;
if (varDeclaration.Value != null) {
value = _exprBuilder.Create(varDeclaration.Value, varDeclaration.Type);
if (!varDeclaration.ValType.MatchesType(compiler, value.ValType)) {
compiler.Emit(WarningCode.InvalidVarType, varDeclaration.Location,
$"{varDeclaration.Name}: Invalid var value {value.ValType}, expected {varDeclaration.ValType}");
}
} else {
value = new Null(varDeclaration.Location);
}
bool successful;
if (varDeclaration.IsConst) {
if (!value.TryAsConstant(compiler, out var constValue)) {
compiler.Emit(WarningCode.HardConstContext, varDeclaration.Location, "Const var must be set to a constant");
return;
}
successful = proc.TryAddLocalConstVariable(varDeclaration.Name, varDeclaration.Type, constValue);
} else {
successful = proc.TryAddLocalVariable(varDeclaration.Name, varDeclaration.Type, varDeclaration.ValType);
}
if (!successful) {
compiler.Emit(WarningCode.DuplicateVariable, varDeclaration.Location, $"Duplicate var {varDeclaration.Name}");
return;
}
value.EmitPushValue(ExprContext);
proc.Assign(proc.GetLocalVariableReference(varDeclaration.Name, varDeclaration.Location));
proc.Pop();
}
private void ProcessStatementReturn(DMASTProcStatementReturn statement) {
if (statement.Value != null) {
var expr = _exprBuilder.Create(statement.Value);
// Don't type-check unimplemented procs
if (proc.TypeChecked && (proc.Attributes & ProcAttributes.Unimplemented) == 0) {
if (expr.TryAsConstant(compiler, out var exprConst)) {
proc.ValidateReturnType(exprConst);
} else {
proc.ValidateReturnType(expr);
}
}
expr.EmitPushValue(ExprContext);
} else {
proc.PushReferenceValue(DMReference.Self); //Default return value
}
proc.Return();
}
private void ProcessStatementIf(DMASTProcStatementIf statement) {
_exprBuilder.Emit(statement.Condition);
if (statement.ElseBody == null) {
string endLabel = proc.NewLabelName();
proc.JumpIfFalse(endLabel);
proc.StartScope();
ProcessBlockInner(statement.Body);
proc.EndScope();
proc.AddLabel(endLabel);
} else {
string elseLabel = proc.NewLabelName();
string endLabel = proc.NewLabelName();
proc.JumpIfFalse(elseLabel);
proc.StartScope();
ProcessBlockInner(statement.Body);
proc.EndScope();
proc.Jump(endLabel);
proc.AddLabel(elseLabel);
proc.StartScope();
ProcessBlockInner(statement.ElseBody);
proc.EndScope();
proc.AddLabel(endLabel);
}
}
private void ProcessStatementFor(DMASTProcStatementFor statementFor) {
proc.StartScope();
{
foreach (var decl in FindVarDecls(statementFor.Expression1)) {
ProcessStatementVarDeclaration(new DMASTProcStatementVarDeclaration(statementFor.Location, decl.DeclPath, null, DMValueType.Anything));
}
if (statementFor is { Expression2: DMASTExpressionIn dmastIn, Expression3: null }) { // for(var/i,j in expr) or for(i,j in expr)
var valueVar = statementFor.Expression2 != null ? _exprBuilder.CreateIgnoreUnknownReference(statementFor.Expression2) : null;
var list = _exprBuilder.Create(dmastIn.RHS);
// TODO: Wow this sucks
if (valueVar is UnknownReference unknownRef) { // j in var/i,j isn't already a var
if(dmastIn.LHS is not DMASTIdentifier ident)
unknownRef.EmitCompilerError(compiler);
else
ProcessStatementVarDeclaration(new DMASTProcStatementVarDeclaration(statementFor.Location, new DMASTPath(statementFor.Location, new DreamPath(ident.Identifier)), null, DMValueType.Anything));
}
DMASTExpression outputExpr;
if (statementFor.Expression1 is DMASTVarDeclExpression decl) {
outputExpr = new DMASTIdentifier(decl.Location, decl.DeclPath.Path.LastElement!);
} else {
outputExpr = statementFor.Expression1;
}
var keyVar = _exprBuilder.Create(outputExpr);
valueVar = _exprBuilder.Create(dmastIn.LHS);
switch (keyVar) {
case Local outputLocal: {
outputLocal.LocalVar.ExplicitValueType = statementFor.DMTypes;
if(outputLocal.LocalVar is DMProc.LocalConstVariable)
compiler.Emit(WarningCode.WriteToConstant, outputExpr.Location, "Cannot change constant value");
break;
}
case Field { IsConst: true }: {
compiler.Emit(WarningCode.WriteToConstant, outputExpr.Location, "Cannot change constant value");
break;
}
}
switch (valueVar) {
case Local assocLocal: {
assocLocal.LocalVar.ExplicitValueType = statementFor.DMTypes;
if(assocLocal.LocalVar is DMProc.LocalConstVariable)
compiler.Emit(WarningCode.WriteToConstant, outputExpr.Location, "Cannot change constant value");
break;
}
case Field { IsConst: true }: {
compiler.Emit(WarningCode.WriteToConstant, outputExpr.Location, "Cannot change constant value");
break;
}
}
ProcessStatementForList(list, keyVar, valueVar, statementFor.DMTypes, statementFor.Body);
} else if (statementFor.Expression2 != null || statementFor.Expression3 != null) {
var initializer = statementFor.Expression1 != null ? _exprBuilder.Create(statementFor.Expression1) : null;
var comparator = statementFor.Expression2 != null ? _exprBuilder.Create(statementFor.Expression2) : null;
var incrementor = statementFor.Expression3 != null ? _exprBuilder.Create(statementFor.Expression3) : null;
ProcessStatementForStandard(initializer, comparator, incrementor, statementFor.Body);
} else {
switch (statementFor.Expression1) {
case DMASTAssign {LHS: DMASTVarDeclExpression decl, RHS: DMASTExpressionInRange range}: {
var initializer = statementFor.Expression1 != null ? _exprBuilder.Create(statementFor.Expression1) : null;
var identifier = new DMASTIdentifier(decl.Location, decl.DeclPath.Path.LastElement);
var outputVar = _exprBuilder.Create(identifier);
var start = _exprBuilder.Create(range.StartRange);
var end = _exprBuilder.Create(range.EndRange);
var step = range.Step != null
? _exprBuilder.Create(range.StartRange)
: new Number(range.Location, 1);
ProcessStatementForRange(initializer, outputVar, start, end, step, statementFor.Body);
break;
}
case DMASTExpressionInRange exprRange: {
DMASTVarDeclExpression? decl = exprRange.Value as DMASTVarDeclExpression;
decl ??= exprRange.Value is DMASTAssign assign
? assign.LHS as DMASTVarDeclExpression
: null;
DMASTExpression outputExpr;
if (decl != null) {
outputExpr = new DMASTIdentifier(exprRange.Value.Location, decl.DeclPath.Path.LastElement);
} else {
outputExpr = exprRange.Value;
}
var outputVar = _exprBuilder.Create(outputExpr);
if (outputVar is Local { LocalVar: DMProc.LocalConstVariable } or Field { IsConst: true }) {
compiler.Emit(WarningCode.WriteToConstant, outputExpr.Location, "Cannot change constant value");
}
var start = _exprBuilder.Create(exprRange.StartRange);
var end = _exprBuilder.Create(exprRange.EndRange);
var step = exprRange.Step != null
? _exprBuilder.Create(exprRange.Step)
: new Number(exprRange.Location, 1);
ProcessStatementForRange(null, outputVar, start, end, step, statementFor.Body);
break;
}
case DMASTVarDeclExpression vd: {
var initializer = statementFor.Expression1 != null ? _exprBuilder.Create(statementFor.Expression1) : null;
var declInfo = new ProcVarDeclInfo(vd.DeclPath.Path);
var identifier = new DMASTIdentifier(vd.Location, declInfo.VarName);
var outputVar = _exprBuilder.Create(identifier);
ProcessStatementForType(vd.Location, initializer, outputVar, declInfo.TypePath, statementFor.Body);
break;
}
case DMASTExpressionIn exprIn: {
DMASTExpression outputExpr;
if (exprIn.LHS is DMASTVarDeclExpression decl) {
outputExpr = new DMASTIdentifier(decl.Location, decl.DeclPath.Path.LastElement);
} else {
outputExpr = exprIn.LHS;
}
var outputVar = _exprBuilder.Create(outputExpr);
var list = _exprBuilder.Create(exprIn.RHS);
if (outputVar is Local outputLocal) {
outputLocal.LocalVar.ExplicitValueType = statementFor.DMTypes;
if(outputLocal.LocalVar is DMProc.LocalConstVariable)
compiler.Emit(WarningCode.WriteToConstant, outputExpr.Location, "Cannot change constant value");
} else if (outputVar is Field { IsConst: true })
compiler.Emit(WarningCode.WriteToConstant, outputExpr.Location, "Cannot change constant value");
ProcessStatementForList(list, outputVar, null, statementFor.DMTypes, statementFor.Body);
break;
}
default:
compiler.Emit(WarningCode.BadExpression, statementFor.Location, "Invalid expression in for");
break;
}
}
}
proc.EndScope();
IEnumerable<DMASTVarDeclExpression> FindVarDecls(DMASTExpression? expr) {
if (expr is null)
yield break;
if (expr is DMASTVarDeclExpression p)
yield return p;
foreach (var leaf in expr.Leaves()) {
foreach(var decl in FindVarDecls(leaf)) {
yield return decl;
}
}
}
}
private void ProcessStatementForStandard(DMExpression? initializer, DMExpression? comparator, DMExpression? incrementor, DMASTProcBlockInner body) {
proc.StartScope();
{
if (initializer != null) {
initializer.EmitPushValue(ExprContext);
proc.Pop();
}
string loopLabel = proc.NewLabelName();
proc.LoopStart(loopLabel);
{
if (comparator != null) {
comparator.EmitPushValue(ExprContext);
proc.BreakIfFalse();
}
ProcessBlockInner(body);
proc.MarkLoopContinue(loopLabel);
if (incrementor != null) {
incrementor.EmitPushValue(ExprContext);
proc.Pop();
}
proc.LoopJumpToStart(loopLabel);
}
proc.LoopEnd();
}
proc.EndScope();
}
private void ProcessLoopAssignment(LValue lValue, LValue? assocValue = null, DMExpression? list = null) {
if (lValue.CanReferenceShortCircuit()) { // TODO: If assocValue short circuits?
string endLabel = proc.NewLabelName();
string endLabel2 = proc.NewLabelName();
DMReference outputRef = lValue.EmitReference(ExprContext, endLabel, DMExpression.ShortCircuitMode.PopNull);
proc.Enumerate(outputRef);
proc.Jump(endLabel2);
proc.AddLabel(endLabel);
proc.EnumerateNoAssign();
proc.AddLabel(endLabel2);
} else {
if (assocValue != null && list != null) {
DMReference assocRef = assocValue.EmitReference(ExprContext, string.Empty);
DMReference outputRef = lValue.EmitReference(ExprContext, string.Empty);
proc.EnumerateAssoc(assocRef, outputRef);
} else {
DMReference outputRef = lValue.EmitReference(ExprContext, string.Empty);
proc.Enumerate(outputRef);
}
}
}
private void ProcessStatementForList(DMExpression list, DMExpression outputVar, DMExpression? outputAssocVar, DMComplexValueType? typeCheck, DMASTProcBlockInner body) {
if (outputVar is not LValue lValue) {
compiler.Emit(WarningCode.BadExpression, outputVar.Location, "Invalid output var");
lValue = new BadLValue(outputVar.Location);
}
if (outputAssocVar is not LValue && outputAssocVar is not null) {
compiler.Emit(WarningCode.BadExpression, outputAssocVar.Location, "Invalid output var");
lValue = new BadLValue(outputAssocVar.Location);
}
LValue? outputValue = (LValue?)outputAssocVar;
// Having no "as [types]" will use the var's type for the type filter
if (typeCheck == null && lValue.Path != null) {
typeCheck = lValue.Path;
}
bool performingImplicitIsType = false;
list.EmitPushValue(ExprContext);
if (typeCheck?.TypePath is { } typeCheckPath) { // We have a specific type to filter for
if (compiler.DMObjectTree.TryGetTypeId(typeCheckPath, out var filterTypeId)) {
// Create an enumerator that will do the implicit istype() for us
proc.CreateFilteredListEnumerator(filterTypeId, typeCheckPath);
} else {
compiler.Emit(WarningCode.ItemDoesntExist, outputVar.Location,
$"Cannot filter enumeration by type {typeCheckPath}, it does not exist");
proc.CreateListEnumerator();
}
} else { // Either no type filter or we're using the slower "as [types]"
performingImplicitIsType = !(typeCheck is null || typeCheck.Value.IsAnything);
proc.CreateListEnumerator();
}
proc.StartScope();
{
string loopLabel = proc.NewLabelName();
proc.LoopStart(loopLabel);
{
proc.MarkLoopContinue(loopLabel);
ProcessLoopAssignment(lValue, outputValue, list);
// "as mob|etc" will insert code equivalent to "if(!(istype(X, mob) || istype(X, etc))) continue;"
// It would be ideal if the type filtering could be done by the interpreter, like it does when the var has a type
// But the code currently isn't structured in a way that it could be done nicely
if (performingImplicitIsType) {
var afterTypeCheckIf = proc.NewLabelName();
var afterTypeCheckExpr = proc.NewLabelName();
void CheckType(DMValueType type, DreamPath path, ref bool doOr) {
if (!typeCheck!.Value.Type.HasFlag(type))
return;
if (!compiler.DMObjectTree.TryGetTypeId(path, out var typeId))
return;
if (doOr)
proc.BooleanOr(afterTypeCheckExpr);
doOr = true;
lValue.EmitPushValue(ExprContext);
proc.PushType(typeId);
proc.IsType();
}
bool doOr = false; // Only insert BooleanOr after the first type
CheckType(DMValueType.Area, DreamPath.Area, ref doOr);
CheckType(DMValueType.Turf, DreamPath.Turf, ref doOr);
CheckType(DMValueType.Obj, DreamPath.Obj, ref doOr);
CheckType(DMValueType.Mob, DreamPath.Mob, ref doOr);
proc.AddLabel(afterTypeCheckExpr);
if (doOr) {
proc.Not();
proc.JumpIfFalse(afterTypeCheckIf);
proc.Continue();
}
proc.AddLabel(afterTypeCheckIf);
}
ProcessBlockInner(body);
proc.LoopJumpToStart(loopLabel);
}
proc.LoopEnd();
}
proc.EndScope();
proc.DestroyEnumerator();
}
private void ProcessStatementForType(Location location, DMExpression? initializer, DMExpression outputVar, DreamPath? type, DMASTProcBlockInner body) {
if (type == null) {
// This shouldn't happen, just to be safe
compiler.ForcedError(location,
"Attempted to create a type enumerator with a null type");
return;
}
if (compiler.DMObjectTree.TryGetTypeId(type.Value, out var typeId)) {
proc.PushType(typeId);
proc.CreateTypeEnumerator();
} else {
compiler.Emit(WarningCode.ItemDoesntExist, location, $"Type {type.Value} does not exist");
}
proc.StartScope();
{
if (initializer != null) {
initializer.EmitPushValue(ExprContext);
proc.Pop();
}
string loopLabel = proc.NewLabelName();
proc.LoopStart(loopLabel);
{
proc.MarkLoopContinue(loopLabel);
if (outputVar is LValue lValue) {
ProcessLoopAssignment(lValue);
} else {
compiler.Emit(WarningCode.BadExpression, outputVar.Location, "Invalid output var");
}
ProcessBlockInner(body);
proc.LoopJumpToStart(loopLabel);
}
proc.LoopEnd();
}
proc.EndScope();
proc.DestroyEnumerator();
}
private void ProcessStatementForRange(DMExpression? initializer, DMExpression outputVar, DMExpression start, DMExpression end, DMExpression? step, DMASTProcBlockInner body) {
start.EmitPushValue(ExprContext);
end.EmitPushValue(ExprContext);
if (step != null) {
step.EmitPushValue(ExprContext);
} else {
proc.PushFloat(1.0f);
}
proc.CreateRangeEnumerator();
proc.StartScope();
{
if (initializer != null) {
initializer.EmitPushValue(ExprContext);
proc.Pop();
}
string loopLabel = proc.NewLabelName();
proc.LoopStart(loopLabel);
{
proc.MarkLoopContinue(loopLabel);
if (outputVar is LValue lValue) {
ProcessLoopAssignment(lValue);
} else {
compiler.Emit(WarningCode.BadExpression, outputVar.Location, "Invalid output var");
}
ProcessBlockInner(body);
proc.LoopJumpToStart(loopLabel);
}
proc.LoopEnd();
}
proc.EndScope();
proc.DestroyEnumerator();
}
//Generic infinite loop, while loops with static expression as their conditional with positive truthfulness get turned into this as well as empty for() calls
private void ProcessStatementInfLoop(DMASTProcStatementInfLoop statementInfLoop){
proc.StartScope();
{
string loopLabel = proc.NewLabelName();
proc.LoopStart(loopLabel);
{
proc.MarkLoopContinue(loopLabel);
ProcessBlockInner(statementInfLoop.Body);
proc.LoopJumpToStart(loopLabel);
}
proc.LoopEnd();
}
proc.EndScope();
}
private void ProcessStatementWhile(DMASTProcStatementWhile statementWhile) {
string loopLabel = proc.NewLabelName();
proc.LoopStart(loopLabel);
{
proc.MarkLoopContinue(loopLabel);
_exprBuilder.Emit(statementWhile.Conditional);
proc.BreakIfFalse();
proc.StartScope();
{
ProcessBlockInner(statementWhile.Body);
proc.LoopJumpToStart(loopLabel);
}
proc.EndScope();
}
proc.LoopEnd();
}
private void ProcessStatementDoWhile(DMASTProcStatementDoWhile statementDoWhile) {
string loopLabel = proc.NewLabelName();
string loopEndLabel = proc.NewLabelName();
proc.LoopStart(loopLabel);
{
ProcessBlockInner(statementDoWhile.Body);
proc.MarkLoopContinue(loopLabel);
_exprBuilder.Emit(statementDoWhile.Conditional);
proc.JumpIfFalse(loopEndLabel);
proc.LoopJumpToStart(loopLabel);
proc.AddLabel(loopEndLabel);
proc.Break();
}
proc.LoopEnd();
}
private void ProcessStatementSwitch(DMASTProcStatementSwitch statementSwitch) {
string endLabel = proc.NewLabelName();
List<(string CaseLabel, DMASTProcBlockInner CaseBody)> valueCases = new();
DMASTProcBlockInner? defaultCaseBody = null;
_exprBuilder.Emit(statementSwitch.Value);
foreach (DMASTProcStatementSwitch.SwitchCase switchCase in statementSwitch.Cases) {
if (switchCase is DMASTProcStatementSwitch.SwitchCaseValues switchCaseValues) {
string caseLabel = proc.NewLabelName();
foreach (DMASTExpression value in switchCaseValues.Values) {
Constant GetCaseValue(DMASTExpression expression) {
if (!_exprBuilder.TryConstant(expression, out var constant))
compiler.Emit(WarningCode.HardConstContext, expression.Location, "Expected a constant");
// Return 0 if unsuccessful so that we can continue compiling
return constant ?? new Number(expression.Location, 0.0f);
}
if (value is DMASTSwitchCaseRange range) { // if(1 to 5) or something
Constant lower = GetCaseValue(range.RangeStart);
Constant upper = GetCaseValue(range.RangeEnd);
Constant CoerceBound(Constant bound, bool upperRange) {
if (bound is Null) { // We do a little null coercion, as a treat
compiler.Emit(WarningCode.MalformedRange, range.RangeStart.Location,
$"Malformed range, {(upperRange ? "upper" : "lower")} bound is coerced from null to 0");
return new Number(lower.Location, 0.0f);
}
//DM 514.1580 does NOT care if the constants within a range are strings, and does a strange conversion to 0 or something, without warning or notice.
//We are (hopefully) deviating from parity here and just calling that a Compiler error.
if (bound is not Number) {
compiler.Emit(WarningCode.InvalidRange, range.RangeStart.Location,
$"Invalid range, {(upperRange ? "upper" : "lower")} bound is not a number");
bound = new Number(bound.Location, 0.0f);
}
return bound;
}
lower = CoerceBound(lower, false);
upper = CoerceBound(upper, true);
lower.EmitPushValue(ExprContext);
upper.EmitPushValue(ExprContext);
proc.SwitchCaseRange(caseLabel);
} else {
Constant constant = GetCaseValue(value);
constant.EmitPushValue(ExprContext);
proc.SwitchCase(caseLabel);
}
}
valueCases.Add((caseLabel, switchCase.Body));
} else {
defaultCaseBody = ((DMASTProcStatementSwitch.SwitchCaseDefault)switchCase).Body;
}
}
proc.Pop();
if (defaultCaseBody != null) {
proc.StartScope();
{
ProcessBlockInner(defaultCaseBody);
}
proc.EndScope();
}
proc.Jump(endLabel);
foreach ((string CaseLabel, DMASTProcBlockInner CaseBody) valueCase in valueCases) {
proc.AddLabel(valueCase.CaseLabel);
proc.StartScope();
{
ProcessBlockInner(valueCase.CaseBody);
}
proc.EndScope();
proc.Jump(endLabel);
}
proc.AddLabel(endLabel);
}
private void ProcessStatementBrowse(DMASTProcStatementBrowse statementBrowse) {
_exprBuilder.Emit(statementBrowse.Receiver);
_exprBuilder.Emit(statementBrowse.Body);
_exprBuilder.Emit(statementBrowse.Options);
proc.Browse();
}
private void ProcessStatementBrowseResource(DMASTProcStatementBrowseResource statementBrowseResource) {
_exprBuilder.Emit(statementBrowseResource.Receiver);
_exprBuilder.Emit(statementBrowseResource.File);
_exprBuilder.Emit(statementBrowseResource.Filename);
proc.BrowseResource();
}
private void ProcessStatementOutputControl(DMASTProcStatementOutputControl statementOutputControl) {
_exprBuilder.Emit(statementOutputControl.Receiver);
_exprBuilder.Emit(statementOutputControl.Message);
_exprBuilder.Emit(statementOutputControl.Control);
proc.OutputControl();
}
private void ProcessStatementLink(DMASTProcStatementLink statementLink) {
_exprBuilder.Emit(statementLink.Receiver);
_exprBuilder.Emit(statementLink.Url);
proc.Link();
}
private void ProcessStatementFtp(DMASTProcStatementFtp statementFtp) {
_exprBuilder.Emit(statementFtp.Receiver);
_exprBuilder.Emit(statementFtp.File);
_exprBuilder.Emit(statementFtp.Name);
proc.Ftp();
}
private void ProcessStatementOutput(DMASTProcStatementOutput statementOutput) {
DMExpression left = _exprBuilder.Create(statementOutput.A);
DMExpression right = _exprBuilder.Create(statementOutput.B);
if (left is LValue) {
// An LValue on the left needs a special opcode so that its reference can be used
// This allows for special operations like "savefile[...] << ..."
string endLabel = proc.NewLabelName();
DMReference leftRef = left.EmitReference(ExprContext, endLabel, DMExpression.ShortCircuitMode.PopNull);
right.EmitPushValue(ExprContext);
proc.OutputReference(leftRef);
proc.AddLabel(endLabel);
} else {
left.EmitPushValue(ExprContext);
right.EmitPushValue(ExprContext);
proc.Output();
}
}
private void ProcessStatementInput(DMASTProcStatementInput statementInput) {
DMExpression left = _exprBuilder.Create(statementInput.A);
DMExpression right = _exprBuilder.Create(statementInput.B);
// The left-side value of an input operation must be an LValue
// (I think? I haven't found an exception but there could be one)
if (left is not LValue) {
compiler.Emit(WarningCode.BadExpression, left.Location, "Left side must be an l-value");
return;
}
// The right side must also be an LValue. Because where else would the value go?
if (right is not LValue) {
compiler.Emit(WarningCode.BadExpression, right.Location, "Right side must be an l-value");
return;
}
string rightEndLabel = proc.NewLabelName();
string leftEndLabel = proc.NewLabelName();
DMReference rightRef = right.EmitReference(ExprContext, rightEndLabel, DMExpression.ShortCircuitMode.PopNull);
DMReference leftRef = left.EmitReference(ExprContext, leftEndLabel, DMExpression.ShortCircuitMode.PopNull);
proc.Input(leftRef, rightRef);
proc.AddLabel(leftEndLabel);
proc.AddLabel(rightEndLabel);
}
private void ProcessStatementTryCatch(DMASTProcStatementTryCatch tryCatch) {
string catchLabel = proc.NewLabelName();
string endLabel = proc.NewLabelName();
if (tryCatch.CatchParameter is DMASTProcStatementVarDeclaration param) {
if (!proc.TryAddLocalVariable(param.Name, param.Type, param.ValType)) {
compiler.Emit(WarningCode.DuplicateVariable, param.Location, $"Duplicate var {param.Name}");
}
proc.StartTry(catchLabel, proc.GetLocalVariableReference(param.Name, param.Location));
} else {
if (tryCatch.CatchParameter != null)
compiler.Emit(WarningCode.InvalidVarDefinition, tryCatch.CatchParameter.Location,
"Invalid catch parameter");
proc.StartTryNoValue(catchLabel);
}
proc.StartScope();
ProcessBlockInner(tryCatch.TryBody);
proc.EndScope();
proc.EndTry();
proc.Jump(endLabel);
proc.AddLabel(catchLabel);
if (tryCatch.CatchBody != null) {
proc.StartScope();
ProcessBlockInner(tryCatch.CatchBody);
proc.EndScope();
}
proc.AddLabel(endLabel);
}
private void ProcessStatementThrow(DMASTProcStatementThrow statement) {
_exprBuilder.Emit(statement.Value);
proc.Throw();
}
}