-
Notifications
You must be signed in to change notification settings - Fork 12
/
AST.cs
1016 lines (926 loc) · 34.6 KB
/
AST.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cnpl
{
interface IAST
{
string Title { get; }
IValue Execute(ActuatorBase state);
void Compile(CompilerBase state);
}
abstract class ActuatorBase
{
public ActuatorBase()
{
BreakLoop = false;
BreakProgram = false;
VariableTable = new Dictionary<string, IValue>();
FunctionTable = new Dictionary<string, Func<IValue[], IValue>>();
}
public bool BreakLoop { get; set; }
public bool BreakProgram { get; set; }
public Dictionary<string, IValue> VariableTable { get; set; }
public Dictionary<string, Func<IValue[], IValue>> FunctionTable { get; set; }
}
interface IAllocDSTK
{
int Size { get; set; }
}
abstract class CompilerBase
{
public abstract string CurrentLoopEndLabel { get; }
public abstract int CurrentScopeVariableCount { get; }
public abstract void VariableDefine(string name);
public abstract void FunctionDefine(string name, int parDefCount, int parMinCount);
public abstract void EnterFunction(string name);
public abstract void ExitFunction();
public abstract IAllocDSTK InsertALLOCDSTK();
public abstract void InsertSD(string name);
public abstract void InsertARRAYWRITE(string name);
public abstract void InsertLabel(string label);
public abstract void InsertJMPC(string jmpLabel);
public abstract void InsertJMPN(string jmpLabel);
public abstract void InsertJMP(string endLabel);
public abstract void EnterLoop(string beginLabel, string endLabel);
public abstract void ExitLoop();
public abstract void InsertLC(IValue value);
public abstract void InsertLD(string name);
public abstract void InsertPOP();
public abstract void InsertPUSH();
public abstract void InsertADD();
public abstract void InsertSUB();
public abstract void InsertARRAYMAKE();
public abstract void InsertCALL(string name,int pc);
public abstract void InsertARRAYREAD(string name);
public abstract void InsertMUL();
public abstract void InsertDIV();
public abstract void InsertMOD();
public abstract void InsertEQ();
public abstract void InsertNE();
public abstract void InsertNOT();
public abstract void InsertGT();
public abstract void InsertLT();
public abstract void InsertAND();
public abstract void InsertOR();
public abstract void InsertRET();
}
namespace AST
{
class Program : IAST
{
List<BlockStatement> mStatements= new List<BlockStatement>();
public string Title => "Program";
public IValue Execute(ActuatorBase state)
{
IValue value = new IntegerValue(0);
foreach (var s in mStatements)
{
value = s.Execute(state);
if (state.BreakProgram)
{
state.BreakProgram = false;
break;
}
}
return value;
}
public void AddBlockStatement(BlockStatement block)
{
mStatements.Add(block);
}
public void Compile(CompilerBase state)
{
state.EnterFunction("<Program>");
var iadstk = state.InsertALLOCDSTK();
foreach (var s in mStatements)
{
s.Compile(state);
}
state.InsertLC(new BooleanValue(false));
state.InsertRET();
iadstk.Size = state.CurrentScopeVariableCount;
state.ExitFunction();
}
}
abstract class Statement : IAST
{
public abstract string Title { get; }
public abstract void Compile(CompilerBase state);
public abstract IValue Execute(ActuatorBase state);
}
class VariableStatement : Statement
{
public string Name { get; private set; }
public Expression Value { get; private set; }
public override string Title => $"变量申明:{Name}";
public VariableStatement(string name, Expression value)
{
Name = name;
Value = value;
}
public override IValue Execute(ActuatorBase state)
{
var v = Value.Execute(state);
state.VariableTable[Name] = v;
return v;
}
public override void Compile(CompilerBase state)
{
state.VariableDefine(Name);
Value.Compile(state);
state.InsertSD(Name);
}
}
class FunctionStatement : Statement
{
public override string Title => $"函数定义:{Name}";
public string Name { get; private set; }
public string[] Parameters { get; private set; }
public BlockStatement mBlockStatement { get; private set; }
public FunctionStatement(string name, string[] parameters, BlockStatement block)
{
Name = name;
Parameters = parameters;
mBlockStatement = block;
}
public override IValue Execute(ActuatorBase state)
{
state.FunctionTable[Name] = (IValue[] args)=>
{
var vt = state.VariableTable;
var bp = state.BreakProgram;
var newVT= new Dictionary<string, IValue>();
var i = 0;
foreach (var pn in Parameters)
{
if (i < args.Length)
newVT[pn] = args[i++];
else
newVT[pn] = new IntegerValue(0);
}
state.VariableTable = newVT;
try
{
state.BreakProgram = false;
return mBlockStatement.Execute(state);
}
finally
{
state.VariableTable = vt;
state.BreakProgram = bp;
}
};
return new BooleanValue(true);
}
public override void Compile(CompilerBase state)
{
var cid = Guid.NewGuid();
var end = $"{cid}-function-end";
state.InsertJMP(end);
state.FunctionDefine(Name, Parameters.Length, 0);
state.EnterFunction(Name);
state.InsertLabel($"<{Name}>");
var iadstk = state.InsertALLOCDSTK();
foreach (var p in Parameters)
{
state.VariableDefine(p);
}
foreach (var p in Parameters)
{
state.InsertSD(p);
}
mBlockStatement.Compile(state);
state.InsertLC(new BooleanValue(false));
state.InsertRET();
iadstk.Size = state.CurrentScopeVariableCount;
state.ExitFunction();
state.InsertLabel(end);
}
}
class SetVariableStatement : Statement
{
public override string Title => $"写变量:{Name}";
public string Name { get; private set; }
public Expression Value { get; private set; }
public SetVariableStatement(string name, Expression value)
{
Name = name;
Value = value;
}
public override IValue Execute(ActuatorBase state)
{
if(!state.VariableTable.ContainsKey(Name))
return new IntegerValue(0);
var value = Value.Execute(state);
state.VariableTable[Name] = value;
return value;
}
public override void Compile(CompilerBase state)
{
Value.Compile(state);
state.InsertSD(Name);
}
}
class SetArrayVariableStatement : Statement
{
public override string Title => $"写数组变量:{Name}";
public string Name { get; private set; }
private Expression mRow;
private Expression mCol;
public Expression Value { get; private set; }
public SetArrayVariableStatement(string name, Expression row, Expression col, Expression value)
{
Name = name;
mRow = row;
mCol = col;
Value = value;
}
public override IValue Execute(ActuatorBase state)
{
IValue value;
if (state.VariableTable.TryGetValue(Name,out value) && value is ArrayValue)
{
var row = (int)mRow.Execute(state).AsReal();
var col = (int)mCol.Execute(state).AsReal();
var v = Value.Execute(state);
var arr = value as ArrayValue;
arr.SetValue(row, col, v);
return v;
}
return new IntegerValue(0);
}
public override void Compile(CompilerBase state)
{
mRow.Compile(state);
mCol.Compile(state);
Value.Compile(state);
state.InsertARRAYWRITE(Name);
}
}
class IfStatement : Statement
{
public override string Title => $"条件分支-如果";
private Expression mConditional;
private Statement mTrueBlock;
private Statement mFalseBlock;
public IfStatement(Expression conditional, Statement trueBlock, Statement falseBlock)
{
mConditional = conditional;
mTrueBlock = trueBlock;
mFalseBlock = falseBlock;
}
public override IValue Execute(ActuatorBase state)
{
IValue value;
if (mConditional.Execute(state).AsBoolean())
value = mTrueBlock.Execute(state);
else
{
if (mFalseBlock != null)
value = mFalseBlock.Execute(state);
else
value = new BooleanValue(false);
}
return value;
}
public override void Compile(CompilerBase state)
{
var cid = Guid.NewGuid();
var falseLabel = $"{cid}-false";
var endLabel = $"{cid}-end";
mConditional.Compile(state);
if (mFalseBlock != null)
state.InsertJMPN(falseLabel);
else
state.InsertJMPN(endLabel);
mTrueBlock.Compile(state);
state.InsertJMP(endLabel);
if (mFalseBlock != null)
{
state.InsertLabel(falseLabel);
mFalseBlock.Compile(state);
}
state.InsertLabel(endLabel);
}
}
class WhileStatement : Statement
{
public override string Title => $"条件循环-当";
private Expression mConditional;
private Statement mLoopBlock;
public WhileStatement(Expression conditional, Statement loopBlock)
{
mConditional = conditional;
mLoopBlock = loopBlock;
}
public override IValue Execute(ActuatorBase state)
{
IValue value = mConditional.Execute(state);
while (value.AsBoolean())
{
value = mLoopBlock.Execute(state);
if(state.BreakLoop)
{
state.BreakLoop = false;
break;
}
if(state.BreakProgram)
{
break;
}
value = mConditional.Execute(state);
}
return value;
}
public override void Compile(CompilerBase state)
{
var cid = Guid.NewGuid();
var beginLabel = $"{cid}-begin";
var endLabel = $"{cid}-end";
state.EnterLoop(beginLabel, endLabel);
state.InsertLabel(beginLabel);
mConditional.Compile(state);
state.InsertJMPN(endLabel);
mLoopBlock.Compile(state);
state.InsertJMP(beginLabel);
state.InsertLabel(endLabel);
state.ExitLoop();
}
}
class LoopCountStatement : Statement
{
public override string Title => $"次数循环-次";
private Expression mCount;
private Statement mLoopBlock;
private string IndexName;
public LoopCountStatement(Expression count, Statement loopBlock,string indexName)
{
mCount = count;
mLoopBlock = loopBlock;
IndexName = indexName;
}
public override IValue Execute(ActuatorBase state)
{
IValue value = mCount.Execute(state);
long c = (long)value.AsReal();
long i = 0;
var indexName = string.IsNullOrEmpty(IndexName) ? $"<index>" : IndexName;
state.VariableTable[indexName] = new IntegerValue(i);
while ( i < c)
{
value = mLoopBlock.Execute(state);
++i;
c = (long)(mCount.Execute(state).AsReal());
if (state.BreakLoop)
{
state.BreakLoop = false;
break;
}
if (state.BreakProgram)
{
break;
}
state.VariableTable[indexName] = new IntegerValue(i);
value = mCount.Execute(state);
c = (long)value.AsReal();
}
return value;
}
public override void Compile(CompilerBase state)
{
var cid = Guid.NewGuid();
var beginLabel = $"{cid}-begin";
var endLabel = $"{cid}-end";
var indexName = string.IsNullOrEmpty(IndexName)?$"<{cid}-index>": IndexName;
state.EnterLoop(beginLabel, endLabel);
state.VariableDefine(indexName);
state.InsertLC(new IntegerValue(0));
state.InsertSD(indexName);
state.InsertLabel(beginLabel);
state.InsertLD(indexName);
mCount.Compile(state);
state.InsertLT();
state.InsertJMPN(endLabel);
mLoopBlock.Compile(state);
state.InsertLD(indexName);
state.InsertLC(new IntegerValue(1));
state.InsertADD();
state.InsertSD(indexName);
state.InsertJMP(beginLabel);
state.InsertLabel(endLabel);
state.ExitLoop();
}
}
class BreakStatement : Statement
{
public override string Title => $"中止循环";
public override IValue Execute(ActuatorBase state)
{
IValue value = new BooleanValue(false);
state.BreakLoop = true;
return value;
}
public override void Compile(CompilerBase state)
{
var endLabel = state.CurrentLoopEndLabel;
state.InsertJMP(endLabel);
}
}
class ReturnStatement : Statement
{
public override string Title => $"返回";
private Expression mExpression;
public ReturnStatement(Expression expression)
{
mExpression = expression;
}
public override IValue Execute(ActuatorBase state)
{
IValue value = null;
if (mExpression != null)
{
value = mExpression.Execute(state);
}
else
{
value = new BooleanValue(false);
}
state.BreakProgram = true;
return value;
}
public override void Compile(CompilerBase state)
{
if (mExpression != null)
mExpression.Compile(state);
else
state.InsertLC(new BooleanValue(false));
state.InsertRET();
}
}
class BlockStatement : Statement
{
public override string Title => $"语句块({mStatements.Count})";
List<Statement> mStatements = new List<Statement>();
public override IValue Execute(ActuatorBase state)
{
IValue value = new IntegerValue(0);
foreach (var s in mStatements)
{
value = s.Execute(state);
if (state.BreakProgram)
break;
if (state.BreakLoop)
break;
}
return value;
}
public void AddStatement(Statement statement)
{
mStatements.Add(statement);
}
public override void Compile(CompilerBase state)
{
foreach (var s in mStatements)
s.Compile(state);
}
}
abstract class Expression : Statement
{
public override string Title => $"表达式";
}
class AddExpression : Expression
{
public override string Title => $"加法";
private Expression mExpression1;
private Expression mExpression2;
public AddExpression(Expression expression1, Expression expression2)
{
mExpression1 = expression1;
mExpression2 = expression2;
}
public override IValue Execute(ActuatorBase state)
{
var a = mExpression1.Execute(state);
var b = mExpression2.Execute(state);
if (a.Is(ValueType.String) || b.Is(ValueType.String))
{
return new StringValue(a.AsString() + b.AsString());
}
else if (a.Is(ValueType.Real) || b.Is(ValueType.Real))
{
return new RealValue(a.AsReal() + b.AsReal());
}
return new IntegerValue(a.AsInteger() + b.AsInteger());
}
public override void Compile(CompilerBase state)
{
mExpression1.Compile(state);
mExpression2.Compile(state);
state.InsertADD();
}
}
class SubExpression : Expression
{
public override string Title => $"减法";
private Expression mExpression1;
private Expression mExpression2;
public SubExpression(Expression expression1, Expression expression2)
{
mExpression1 = expression1;
mExpression2 = expression2;
}
public override IValue Execute(ActuatorBase state)
{
var a = mExpression1.Execute(state);
var b = mExpression2.Execute(state);
if (a.Is(ValueType.String) || b.Is(ValueType.String))
{
return new StringValue(a.AsString().TrimEnd() + b.AsString().TrimStart());
}
else if (a.Is(ValueType.Real) || b.Is(ValueType.Real))
{
return new RealValue(a.AsReal() - b.AsReal());
}
return new IntegerValue(a.AsInteger() - b.AsInteger());
}
public override void Compile(CompilerBase state)
{
mExpression1.Compile(state);
mExpression2.Compile(state);
state.InsertSUB();
}
}
class MulExpression : Expression
{
public override string Title => $"乘法";
private Expression mExpression1;
private Expression mExpression2;
public MulExpression(Expression expression1, Expression expression2)
{
mExpression1 = expression1;
mExpression2 = expression2;
}
public override IValue Execute(ActuatorBase state)
{
var a = mExpression1.Execute(state);
var b = mExpression2.Execute(state);
if (a.Is(ValueType.Integer) && b.Is(ValueType.Integer))
return new IntegerValue(a.AsInteger() * b.AsInteger());
return new RealValue(a.AsReal() * b.AsReal());
}
public override void Compile(CompilerBase state)
{
mExpression1.Compile(state);
mExpression2.Compile(state);
state.InsertMUL();
}
}
class DivExpression : Expression
{
public override string Title => $"除法";
private Expression mExpression1;
private Expression mExpression2;
public DivExpression(Expression expression1, Expression expression2)
{
mExpression1 = expression1;
mExpression2 = expression2;
}
public override IValue Execute(ActuatorBase state)
{
var a = mExpression1.Execute(state);
var b = mExpression2.Execute(state);
if (a.Is(ValueType.Integer) && b.Is(ValueType.Integer))
return new IntegerValue(a.AsInteger() / b.AsInteger());
return new RealValue(a.AsReal() / b.AsReal());
}
public override void Compile(CompilerBase state)
{
mExpression1.Compile(state);
mExpression2.Compile(state);
state.InsertDIV();
}
}
class ModExpression : Expression
{
public override string Title => $"求余";
private Expression mExpression1;
private Expression mExpression2;
public ModExpression(Expression expression1, Expression expression2)
{
mExpression1 = expression1;
mExpression2 = expression2;
}
public override IValue Execute(ActuatorBase state)
{
var v1 = mExpression1.Execute(state);
var v2 = mExpression2.Execute(state);
return new IntegerValue(v1.AsInteger() % v2.AsInteger());
}
public override void Compile(CompilerBase state)
{
mExpression1.Compile(state);
mExpression2.Compile(state);
state.InsertMOD();
}
}
class EQExpression : Expression
{
public override string Title => $"等于比较";
private Expression mExpression1;
private Expression mExpression2;
public EQExpression(Expression expression1, Expression expression2)
{
mExpression1 = expression1;
mExpression2 = expression2;
}
public override IValue Execute(ActuatorBase state)
{
var v1 = mExpression1.Execute(state);
var v2 = mExpression2.Execute(state);
return v1.VEquals(v2);
}
public override void Compile(CompilerBase state)
{
mExpression1.Compile(state);
mExpression2.Compile(state);
state.InsertEQ();
}
}
class NEQExpression : Expression
{
public override string Title => $"不等于比较";
private Expression mExpression1;
private Expression mExpression2;
public NEQExpression(Expression expression1, Expression expression2)
{
mExpression1 = expression1;
mExpression2 = expression2;
}
public override IValue Execute(ActuatorBase state)
{
var v1 = mExpression1.Execute(state);
var v2 = mExpression2.Execute(state);
return v1.VNEquals(v2);
}
public override void Compile(CompilerBase state)
{
mExpression1.Compile(state);
mExpression2.Compile(state);
state.InsertNE();
}
}
class GTExpression : Expression
{
public override string Title => $"大于比较";
private Expression mExpression1;
private Expression mExpression2;
public GTExpression(Expression expression1, Expression expression2)
{
mExpression1 = expression1;
mExpression2 = expression2;
}
public override IValue Execute(ActuatorBase state)
{
var a = mExpression1.Execute(state);
var b = mExpression2.Execute(state);
if (a.Is(ValueType.String) || b.Is(ValueType.String))
{
return new BooleanValue(a.AsString().Length > b.AsString().Length);
}
else if (a.Is(ValueType.Real) || b.Is(ValueType.Real))
{
return new BooleanValue(a.AsReal() > b.AsReal());
}
return new BooleanValue(a.AsInteger() > b.AsInteger());
}
public override void Compile(CompilerBase state)
{
mExpression1.Compile(state);
mExpression2.Compile(state);
state.InsertGT();
}
}
class LTExpression : Expression
{
public override string Title => $"小于比较";
private Expression mExpression1;
private Expression mExpression2;
public LTExpression(Expression expression1, Expression expression2)
{
mExpression1 = expression1;
mExpression2 = expression2;
}
public override IValue Execute(ActuatorBase state)
{
var a = mExpression1.Execute(state);
var b = mExpression2.Execute(state);
if (a.Is(ValueType.String) || b.Is(ValueType.String))
{
return new BooleanValue(a.AsString().Length < b.AsString().Length);
}
else if (a.Is(ValueType.Real) || b.Is(ValueType.Real))
{
return new BooleanValue(a.AsReal() < b.AsReal());
}
return new BooleanValue(a.AsInteger() < b.AsInteger());
}
public override void Compile(CompilerBase state)
{
mExpression1.Compile(state);
mExpression2.Compile(state);
state.InsertLT();
}
}
class ANDExpression : Expression
{
public override string Title => $"逻辑与";
private Expression mExpression1;
private Expression mExpression2;
public ANDExpression(Expression expression1, Expression expression2)
{
mExpression1 = expression1;
mExpression2 = expression2;
}
public override IValue Execute(ActuatorBase state)
{
var v1 = mExpression1.Execute(state);
var v2 = mExpression2.Execute(state);
return new BooleanValue(v1.AsBoolean() && v2.AsBoolean());
}
public override void Compile(CompilerBase state)
{
mExpression1.Compile(state);
mExpression2.Compile(state);
state.InsertAND();
}
}
class ORExpression : Expression
{
public override string Title => $"逻辑或";
private Expression mExpression1;
private Expression mExpression2;
public ORExpression(Expression expression1, Expression expression2)
{
mExpression1 = expression1;
mExpression2 = expression2;
}
public override IValue Execute(ActuatorBase state)
{
var v1 = mExpression1.Execute(state);
var v2 = mExpression2.Execute(state);
return new BooleanValue(v1.AsBoolean() || v2.AsBoolean());
}
public override void Compile(CompilerBase state)
{
mExpression1.Compile(state);
mExpression2.Compile(state);
state.InsertOR();
}
}
class ReadVariableExpression : Expression
{
public override string Title => $"读变量:{Name}";
private string Name;
public ReadVariableExpression(string name)
{
Name = name;
}
public override IValue Execute(ActuatorBase state)
{
IValue value;
if (state.VariableTable.TryGetValue(Name, out value))
return value;
return new IntegerValue(0);
}
public override void Compile(CompilerBase state)
{
state.InsertLD(Name);
}
}
class ReadArrayVariableExpression : Expression
{
public override string Title => $"读数组变量:{Name}";
private string Name;
private Expression mRow;
private Expression mCol;
public ReadArrayVariableExpression(string name, Expression row, Expression col)
{
Name = name;
mRow = row;
mCol = col;
}
public override IValue Execute(ActuatorBase state)
{
IValue value;
if (state.VariableTable.TryGetValue(Name, out value))
{
if(value.GetType() == typeof(ArrayValue))
{
var row = (int)mRow.Execute(state).AsReal();
var col = (int)mCol.Execute(state).AsReal();
return (value as ArrayValue).GetValue(row, col);
}
return value;
}
return new IntegerValue(0);
}
public override void Compile(CompilerBase state)
{
mRow.Compile(state);
mCol.Compile(state);
state.InsertARRAYREAD(Name);
}
}
class CallExpression : Expression
{
public override string Title => $"调用函数:{Name}";
protected string Name;
protected Expression[] mParameters;
public CallExpression(string name, Expression[] parameters)
{
Name = name;
mParameters = parameters;
}
public override IValue Execute(ActuatorBase state)
{
Func<IValue[],IValue> func;
var parameters = new IValue[mParameters.Length];
int idx = 0;
foreach(var p in mParameters)
{
parameters[idx++] = p.Execute(state);
}
if (state.FunctionTable.TryGetValue(Name, out func))
return func.Invoke(parameters);
else
throw new Exception($"函数未找到:{Name}");
}
public override void Compile(CompilerBase state)
{
foreach (var p in mParameters.Reverse())
p.Compile(state);
state.InsertCALL(Name, mParameters.Length);
}
}
class CallStatement : CallExpression
{
public CallStatement(string name, Expression[] parameters):
base(name,parameters)
{
}
public override void Compile(CompilerBase state)
{
foreach (var p in mParameters.Reverse())
p.Compile(state);
state.InsertCALL(Name, mParameters.Length);
state.InsertPOP();
}
}
class MakeArrayExpression : Expression
{
public override string Title => $"构造数组";
private Expression Row;
private Expression Col;
private IValue Value;
public MakeArrayExpression(Expression row, Expression col,IValue fillValue)
{
Row = row;
Col = col;
Value = fillValue;
}
public override IValue Execute(ActuatorBase state)
{
var r = (int)(Row.Execute(state).AsReal());
var c = (int)(Col.Execute(state).AsReal());
return new ArrayValue(r, c, Value);
}
public override void Compile(CompilerBase state)
{
Row.Compile(state);
Col.Compile(state);
state.InsertLC(Value);
state.InsertARRAYMAKE();
}
}
class ConstantExpression : Expression
{
public override string Title => $"常量:{Value}";