forked from RichardGong/PlayWithCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASTEvaluator.java
1323 lines (1127 loc) · 47.5 KB
/
ASTEvaluator.java
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
package play;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import play.PlayScriptParser.*;
/**
* AST解释器。利用语义信息(AnnotatedTree),在AST上解释执行脚本。
*/
public class ASTEvaluator extends PlayScriptBaseVisitor<Object> {
// 之前的编译结果
private AnnotatedTree at = null;
// 堆,用于保存对象
public ASTEvaluator(AnnotatedTree at) {
this.at = at;
}
protected boolean traceStackFrame = false;
protected boolean traceFunctionCall = false;
///////////////////////////////////////////////////////////
/// 栈桢的管理
private Stack<StackFrame> stack = new Stack<StackFrame>();
/**
* 栈桢入栈。
* 其中最重要的任务,是要保证栈桢的parentFrame设置正确。否则,
* (1)随着栈的变深,查找变量的性能会降低;
* (2)甚至有可能找错栈桢,比如在递归(直接或间接)的场景下。
* @param frame
*/
private void pushStack(StackFrame frame) {
if (stack.size() > 0) {
//从栈顶到栈底依次查找
for (int i = stack.size()-1; i>0; i--){
StackFrame f = stack.get(i);
/*
如果新加入的栈桢,跟某个已有的栈桢的enclosingScope是一样的,那么这俩的parentFrame也一样。
因为它们原本就是同一级的嘛。
比如:
void foo(){};
void bar(foo());
或者:
void foo();
if (...){
foo();
}
*/
if (f.scope.enclosingScope == frame.scope.enclosingScope){
frame.parentFrame = f.parentFrame;
break;
}
/*
如果新加入的栈桢,是某个已有的栈桢的下一级,那么就把把这个父子关系建立起来。比如:
void foo(){
if (...){ //把这个块往栈桢里加的时候,就符合这个条件。
}
}
再比如,下面的例子:
class MyClass{
void foo();
}
MyClass c = MyClass(); //先加Class的栈桢,里面有类的属性,包括父类的
c.foo(); //再加foo()的栈桢
*/
else if (f.scope == frame.scope.enclosingScope){
frame.parentFrame = f;
break;
}
/*
这是针对函数可能是一等公民的情况。这个时候,函数运行时的作用域,与声明时的作用域会不一致。
我在这里设计了一个“receiver”的机制,意思是这个函数是被哪个变量接收了。要按照这个receiver的作用域来判断。
*/
else if (frame.object instanceof FunctionObject){
FunctionObject functionObject = (FunctionObject)frame.object;
if (functionObject.receiver != null && functionObject.receiver.enclosingScope == f.scope) {
frame.parentFrame = f;
break;
}
}
}
if (frame.parentFrame == null){
frame.parentFrame = stack.peek();
}
}
stack.push(frame);
if (traceStackFrame){
dumpStackFrame();
}
}
private void popStack(){
stack.pop();
}
private void dumpStackFrame(){
System.out.println("\nStack Frames ----------------");
for (StackFrame frame : stack){
System.out.println(frame);
}
System.out.println("-----------------------------\n");
}
public LValue getLValue(Variable variable) {
StackFrame f = stack.peek();
PlayObject valueContainer = null;
while (f != null) {
if (f.scope.containsSymbol(variable)) { //对于对象来说,会查找所有父类的属性
valueContainer = f.object;
break;
}
f = f.parentFrame;
}
//通过正常的作用域找不到,就从闭包里找
//原理:PlayObject中可能有一些变量,其作用域跟StackFrame.scope是不同的。
if (valueContainer == null){
f = stack.peek();
while (f != null) {
if (f.contains(variable)) {
valueContainer = f.object;
break;
}
f = f.parentFrame;
}
}
MyLValue lvalue = new MyLValue(valueContainer, variable);
return lvalue;
}
///////////////////////////////////////////////
//为闭包获取环境变量的值
/**
* 为闭包获取环境变量的值
* @param function 闭包所关联的函数。这个函数会访问一些环境变量。
* @param valueContainer 存放环境变量的值的容器
*/
private void getClosureValues(Function function, PlayObject valueContainer){
if (function.closureVariables != null) {
for (Variable var : function.closureVariables) {
LValue lValue = getLValue(var); // 现在还可以从栈里取,退出函数以后就不行了
Object value = lValue.getValue();
valueContainer.fields.put(var, value);
}
}
}
/**
* 为从函数中返回的对象设置闭包值。因为多个函数型属性可能共享值,所以要打包到ClassObject中,而不是functionObject中
* @param classObject
*/
private void getClosureValues(ClassObject classObject){
//先放在一个临时对象里,避免对classObject即读又写
PlayObject tempObject = new PlayObject();
for ( Variable v : classObject.fields.keySet()) {
if (v.type instanceof FunctionType) {
Object object = classObject.fields.get(v);
if (object != null) {
FunctionObject functionObject = (FunctionObject) object;
getClosureValues(functionObject.function, tempObject);
}
}
}
classObject.fields.putAll(tempObject.fields);
}
///////////////////////////////////////////////
//自己实现的左值对象。
private final class MyLValue implements LValue {
private Variable variable;
private PlayObject valueContainer;
public MyLValue(PlayObject valueContainer, Variable variable) {
this.valueContainer = valueContainer;
this.variable = variable;
}
@Override
public Object getValue() {
//对于this或super关键字,直接返回这个对象,应该是ClassObject
if (variable instanceof This || variable instanceof Super){
return valueContainer;
}
return valueContainer.getValue(variable);
}
@Override
public void setValue(Object value) {
valueContainer.setValue(variable, value);
//如果variable是函数型变量,那改变functionObject.receiver
if (value instanceof FunctionObject){
((FunctionObject) value).receiver = (Variable)variable;
}
}
@Override
public Variable getVariable() {
return variable;
}
@Override
public String toString() {
return "LValue of " + variable.name + " : " + getValue();
}
@Override
public PlayObject getValueContainer() {
return valueContainer;
}
}
///////////////////////////////////////////////////////////
/// 对象初始化
//从父类到子类层层执行缺省的初始化方法,即不带参数的初始化方法。
protected ClassObject createAndInitClassObject(Class theClass) {
ClassObject obj = new ClassObject();
obj.type = theClass;
Stack<Class> ancestorChain = new Stack<Class>();
// 从上到下执行缺省的初始化方法
ancestorChain.push(theClass);
while (theClass.getParentClass() != null) {
ancestorChain.push(theClass.getParentClass());
theClass = theClass.getParentClass();
}
// 执行缺省的初始化方法
StackFrame frame = new StackFrame(obj);
pushStack(frame);
while (ancestorChain.size() > 0) {
Class c = ancestorChain.pop();
defaultObjectInit(c, obj);
}
popStack();
return obj;
}
// 类的缺省初始化方法
protected void defaultObjectInit(Class theClass, ClassObject obj) {
for (Symbol symbol : theClass.symbols) {
// 把变量加到obj里,缺省先都初始化成null,不允许有未初始化的
if (symbol instanceof Variable) {
obj.fields.put((Variable) symbol, null);
}
}
// 执行缺省初始化
ClassBodyContext ctx = ((ClassDeclarationContext) theClass.ctx).classBody();
visitClassBody(ctx);
//TODO 其实这里还没干完活。还需要调用显式声明的构造方法
}
///////////////////////////////////////////////////////////
//内置函数
//自己硬编码的println方法
private void println(FunctionCallContext ctx){
if (ctx.expressionList() != null) {
Object value = visitExpressionList(ctx.expressionList());
if (value instanceof LValue) {
value = ((LValue) value).getValue();
}
System.out.println(value);
}
else{
System.out.println();
}
}
///////////////////////////////////////////////////////////
/// 各种运算
private Object add(Object obj1, Object obj2, Type targetType) {
Object rtn = null;
if (targetType == PrimitiveType.String) {
rtn = String.valueOf(obj1) + String.valueOf(obj2);
} else if (targetType == PrimitiveType.Integer) {
rtn = ((Number) obj1).intValue() + ((Number) obj2).intValue();
} else if (targetType == PrimitiveType.Float) {
rtn = ((Number) obj1).floatValue() + ((Number) obj2).floatValue();
} else if (targetType == PrimitiveType.Long) {
rtn = ((Number) obj1).longValue() + ((Number) obj2).longValue();
} else if (targetType == PrimitiveType.Double) {
rtn = ((Number) obj1).doubleValue() + ((Number) obj2).doubleValue();
} else if (targetType == PrimitiveType.Short) {
rtn = ((Number) obj1).shortValue() + ((Number) obj2).shortValue();
}
else {
System.out.println("unsupported add operation");
}
return rtn;
}
private Object minus(Object obj1, Object obj2, Type targetType) {
Object rtn = null;
if (targetType == PrimitiveType.Integer) {
rtn = ((Number) obj1).intValue() - ((Number) obj2).intValue();
} else if (targetType == PrimitiveType.Float) {
rtn = ((Number) obj1).floatValue() - ((Number) obj2).floatValue();
} else if (targetType == PrimitiveType.Long) {
rtn = ((Number) obj1).longValue() - ((Number) obj2).longValue();
} else if (targetType == PrimitiveType.Double) {
rtn = ((Number) obj1).doubleValue() - ((Number) obj2).doubleValue();
} else if (targetType == PrimitiveType.Short) {
rtn = ((Number) obj1).shortValue() - ((Number) obj2).shortValue();
}
return rtn;
}
private Object mul(Object obj1, Object obj2, Type targetType) {
Object rtn = null;
if (targetType == PrimitiveType.Integer) {
rtn = ((Number) obj1).intValue() * ((Number) obj2).intValue();
} else if (targetType == PrimitiveType.Float) {
rtn = ((Number) obj1).floatValue() * ((Number) obj2).floatValue();
} else if (targetType == PrimitiveType.Long) {
rtn = ((Number) obj1).longValue() * ((Number) obj2).longValue();
} else if (targetType == PrimitiveType.Double) {
rtn = ((Number) obj1).doubleValue() * ((Number) obj2).doubleValue();
} else if (targetType == PrimitiveType.Short) {
rtn = ((Number) obj1).shortValue() * ((Number) obj2).shortValue();
}
return rtn;
}
private Object div(Object obj1, Object obj2, Type targetType) {
Object rtn = null;
if (targetType == PrimitiveType.Integer) {
rtn = ((Number) obj1).intValue() / ((Number) obj2).intValue();
} else if (targetType == PrimitiveType.Float) {
rtn = ((Number) obj1).floatValue() / ((Number) obj2).floatValue();
} else if (targetType == PrimitiveType.Long) {
rtn = ((Number) obj1).longValue() / ((Number) obj2).longValue();
} else if (targetType == PrimitiveType.Double) {
rtn = ((Number) obj1).doubleValue() / ((Number) obj2).doubleValue();
} else if (targetType == PrimitiveType.Short) {
rtn = ((Number) obj1).shortValue() / ((Number) obj2).shortValue();
}
return rtn;
}
private Boolean EQ(Object obj1, Object obj2, Type targetType) {
Boolean rtn = null;
if (targetType == PrimitiveType.Integer) {
rtn = ((Number) obj1).intValue() == ((Number) obj2).intValue();
} else if (targetType == PrimitiveType.Float) {
rtn = ((Number) obj1).floatValue() == ((Number) obj2).floatValue();
} else if (targetType == PrimitiveType.Long) {
rtn = ((Number) obj1).longValue() == ((Number) obj2).longValue();
} else if (targetType == PrimitiveType.Double) {
rtn = ((Number) obj1).doubleValue() == ((Number) obj2).doubleValue();
} else if (targetType == PrimitiveType.Short) {
rtn = ((Number) obj1).shortValue() == ((Number) obj2).shortValue();
}
//对于对象实例、函数,直接比较对象引用
else {
rtn = obj1 == obj2;
}
return rtn;
}
private Object GE(Object obj1, Object obj2, Type targetType) {
Object rtn = null;
if (targetType == PrimitiveType.Integer) {
rtn = ((Number) obj1).intValue() >= ((Number) obj2).intValue();
} else if (targetType == PrimitiveType.Float) {
rtn = ((Number) obj1).floatValue() >= ((Number) obj2).floatValue();
} else if (targetType == PrimitiveType.Long) {
rtn = ((Number) obj1).longValue() >= ((Number) obj2).longValue();
} else if (targetType == PrimitiveType.Double) {
rtn = ((Number) obj1).doubleValue() >= ((Number) obj2).doubleValue();
} else if (targetType == PrimitiveType.Short) {
rtn = ((Number) obj1).shortValue() >= ((Number) obj2).shortValue();
}
return rtn;
}
private Object GT(Object obj1, Object obj2, Type targetType) {
Object rtn = null;
if (targetType == PrimitiveType.Integer) {
rtn = ((Number) obj1).intValue() > ((Number) obj2).intValue();
} else if (targetType == PrimitiveType.Float) {
rtn = ((Number) obj1).floatValue() > ((Number) obj2).floatValue();
} else if (targetType == PrimitiveType.Long) {
rtn = ((Number) obj1).longValue() > ((Number) obj2).longValue();
} else if (targetType == PrimitiveType.Double) {
rtn = ((Number) obj1).doubleValue() > ((Number) obj2).doubleValue();
} else if (targetType == PrimitiveType.Short) {
rtn = ((Number) obj1).shortValue() > ((Number) obj2).shortValue();
}
return rtn;
}
private Object LE(Object obj1, Object obj2, Type targetType) {
Object rtn = null;
if (targetType == PrimitiveType.Integer) {
rtn = ((Number) obj1).intValue() <= ((Number) obj2).intValue();
} else if (targetType == PrimitiveType.Float) {
rtn = ((Number) obj1).floatValue() <= ((Number) obj2).floatValue();
} else if (targetType == PrimitiveType.Long) {
rtn = ((Number) obj1).longValue() <= ((Number) obj2).longValue();
} else if (targetType == PrimitiveType.Double) {
rtn = ((Number) obj1).doubleValue() <= ((Number) obj2).doubleValue();
} else if (targetType == PrimitiveType.Short) {
rtn = ((Number) obj1).shortValue() <= ((Number) obj2).shortValue();
}
return rtn;
}
private Object LT(Object obj1, Object obj2, Type targetType) {
Object rtn = null;
if (targetType == PrimitiveType.Integer) {
rtn = ((Number) obj1).intValue() < ((Number) obj2).intValue();
} else if (targetType == PrimitiveType.Float) {
rtn = ((Number) obj1).floatValue() < ((Number) obj2).floatValue();
} else if (targetType == PrimitiveType.Long) {
rtn = ((Number) obj1).longValue() < ((Number) obj2).longValue();
} else if (targetType == PrimitiveType.Double) {
rtn = ((Number) obj1).doubleValue() < ((Number) obj2).doubleValue();
} else if (targetType == PrimitiveType.Short) {
rtn = ((Number) obj1).shortValue() < ((Number) obj2).shortValue();
}
return rtn;
}
////////////////////////////////////////////////////////////
/// visit每个节点
@Override
public Object visitBlock(BlockContext ctx) {
BlockScope scope = (BlockScope) at.node2Scope.get(ctx);
if (scope != null){ //有些block是不对应scope的,比如函数底下的block.
StackFrame frame = new StackFrame(scope);
// frame.parentFrame = stack.peek();
pushStack(frame);
}
Object rtn = visitBlockStatements(ctx.blockStatements());
if (scope !=null) {
popStack();
}
return rtn;
}
@Override
public Object visitBlockStatement(BlockStatementContext ctx) {
Object rtn = null;
if (ctx.variableDeclarators() != null) {
rtn = visitVariableDeclarators(ctx.variableDeclarators());
} else if (ctx.statement() != null) {
rtn = visitStatement(ctx.statement());
}
return rtn;
}
@Override
public Object visitExpression(ExpressionContext ctx) {
Object rtn = null;
if (ctx.bop != null && ctx.expression().size() >= 2) {
Object left = visitExpression(ctx.expression(0));
Object right = visitExpression(ctx.expression(1));
Object leftObject = left;
Object rightObject = right;
if (left instanceof LValue) {
leftObject = ((LValue) left).getValue();
}
if (right instanceof LValue) {
rightObject = ((LValue) right).getValue();
}
//本节点期待的数据类型
Type type = at.typeOfNode.get(ctx);
//左右两个子节点的类型
Type type1 = at.typeOfNode.get(ctx.expression(0));
Type type2 = at.typeOfNode.get(ctx.expression(1));
switch (ctx.bop.getType()) {
case PlayScriptParser.ADD:
rtn = add(leftObject, rightObject, type);
break;
case PlayScriptParser.SUB:
rtn = minus(leftObject, rightObject, type);
break;
case PlayScriptParser.MUL:
rtn = mul(leftObject, rightObject, type);
break;
case PlayScriptParser.DIV:
rtn = div(leftObject, rightObject, type);
break;
case PlayScriptParser.EQUAL:
rtn = EQ(leftObject, rightObject, PrimitiveType.getUpperType(type1, type2));
break;
case PlayScriptParser.NOTEQUAL:
rtn = !EQ(leftObject, rightObject, PrimitiveType.getUpperType(type1, type2));
break;
case PlayScriptParser.LE:
rtn = LE(leftObject, rightObject, PrimitiveType.getUpperType(type1, type2));
break;
case PlayScriptParser.LT:
rtn = LT(leftObject, rightObject, PrimitiveType.getUpperType(type1, type2));
break;
case PlayScriptParser.GE:
rtn = GE(leftObject, rightObject, PrimitiveType.getUpperType(type1, type2));
break;
case PlayScriptParser.GT:
rtn = GT(leftObject, rightObject, PrimitiveType.getUpperType(type1, type2));
break;
case PlayScriptParser.AND:
rtn = (Boolean) leftObject && (Boolean) rightObject;
break;
case PlayScriptParser.OR:
rtn = (Boolean) leftObject || (Boolean) rightObject;
break;
case PlayScriptParser.ASSIGN:
if (left instanceof LValue) {
//((LValue) left).setValue(right);
((LValue) left).setValue(rightObject);
rtn = right;
} else {
System.out.println("Unsupported feature during assignment");
}
break;
default:
break;
}
} else if (ctx.bop != null && ctx.bop.getType() == PlayScriptParser.DOT) {
// 此语法是左递归的,算法体现这一点
Object leftObject = visitExpression(ctx.expression(0));
if (leftObject instanceof LValue) {
Object value = ((LValue) leftObject).getValue();
if (value instanceof ClassObject) {
ClassObject valueContainer = (ClassObject) value;
Variable leftVar = (Variable)at.symbolOfNode.get(ctx.expression(0));
// 获得field或调用方法
if (ctx.IDENTIFIER() != null) {
Variable variable = (Variable) at.symbolOfNode.get(ctx);
//对于this和super引用的属性,不用考虑重载,因为它们的解析是准确的
if (!(leftVar instanceof This || leftVar instanceof Super)) {
//类的成员可能需要重载
variable = at.lookupVariable(valueContainer.type, variable.getName());
}
LValue lValue = new MyLValue(valueContainer, variable);
rtn = lValue;
} else if (ctx.functionCall() != null) {
//要先计算方法的参数,才能加对象的StackFrame.
if (traceFunctionCall){
System.out.println("\n>>MethodCall : " + ctx.getText());
}
rtn = methodCall(valueContainer, ctx.functionCall(), (leftVar instanceof Super));
}
}
} else {
System.out.println("Expecting an Object Reference");
}
} else if (ctx.primary() != null) {
rtn = visitPrimary(ctx.primary());
}
// 后缀运算,例如:i++ 或 i--
else if (ctx.postfix != null) {
Object value = visitExpression(ctx.expression(0));
LValue lValue = null;
Type type = at.typeOfNode.get(ctx.expression(0));
if (value instanceof LValue) {
lValue = (LValue) value;
value = lValue.getValue();
}
switch (ctx.postfix.getType()) {
case PlayScriptParser.INC:
if (type == PrimitiveType.Integer) {
lValue.setValue((Integer) value + 1);
} else {
lValue.setValue((Long) value + 1);
}
rtn = value;
break;
case PlayScriptParser.DEC:
if (type == PrimitiveType.Integer) {
lValue.setValue((Integer) value - 1);
} else {
lValue.setValue((long) value - 1);
}
rtn = value;
break;
default:
break;
}
}
//前缀操作,例如:++i 或 --i
else if (ctx.prefix != null) {
Object value = visitExpression(ctx.expression(0));
LValue lValue = null;
Type type = at.typeOfNode.get(ctx.expression(0));
if (value instanceof LValue) {
lValue = (LValue) value;
value = lValue.getValue();
}
switch (ctx.prefix.getType()) {
case PlayScriptParser.INC:
if (type == PrimitiveType.Integer) {
rtn = (Integer) value + 1;
} else {
rtn = (Long) value + 1;
}
lValue.setValue(rtn);
break;
case PlayScriptParser.DEC:
if (type == PrimitiveType.Integer) {
rtn = (Integer) value - 1;
} else {
rtn = (Long) value - 1;
}
lValue.setValue(rtn);
break;
//!符号,逻辑非运算
case PlayScriptParser.BANG:
rtn = !((Boolean) value);
break;
default:
break;
}
} else if (ctx.functionCall() != null) {// functionCall
rtn = visitFunctionCall(ctx.functionCall());
}
return rtn;
}
@Override
public Object visitExpressionList(ExpressionListContext ctx) {
Object rtn = null;
for (ExpressionContext child : ctx.expression()) {
rtn = visitExpression(child);
}
return rtn;
}
@Override
public Object visitForInit(ForInitContext ctx) {
Object rtn = null;
if (ctx.variableDeclarators() != null) {
rtn = visitVariableDeclarators(ctx.variableDeclarators());
} else if (ctx.expressionList() != null) {
rtn = visitExpressionList(ctx.expressionList());
}
return rtn;
}
@Override
public Object visitLiteral(LiteralContext ctx) {
Object rtn = null;
//整数
if (ctx.integerLiteral() != null) {
rtn = visitIntegerLiteral(ctx.integerLiteral());
}
//浮点数
else if (ctx.floatLiteral() != null) {
rtn = visitFloatLiteral(ctx.floatLiteral());
}
//布尔值
else if (ctx.BOOL_LITERAL() != null) {
if (ctx.BOOL_LITERAL().getText().equals("true")) {
rtn = Boolean.TRUE;
} else {
rtn = Boolean.FALSE;
}
}
//字符串
else if (ctx.STRING_LITERAL() != null) {
String withQuotationMark = ctx.STRING_LITERAL().getText();
rtn = withQuotationMark.substring(1, withQuotationMark.length() - 1);
}
//单个的字符
else if (ctx.CHAR_LITERAL() != null) {
rtn = ctx.CHAR_LITERAL().getText().charAt(0);
}
//null字面量
else if (ctx.NULL_LITERAL() != null) {
rtn = NullObject.instance();
}
return rtn;
}
@Override
public Object visitIntegerLiteral(IntegerLiteralContext ctx) {
Object rtn = null;
if (ctx.DECIMAL_LITERAL() != null) {
rtn = Integer.valueOf(ctx.DECIMAL_LITERAL().getText());
}
return rtn;
}
@Override
public Object visitFloatLiteral(FloatLiteralContext ctx) {
return Float.valueOf(ctx.getText());
}
@Override
public Object visitParExpression(ParExpressionContext ctx) {
return visitExpression(ctx.expression());
}
@Override
public Object visitPrimary(PrimaryContext ctx) {
Object rtn = null;
//字面量
if (ctx.literal() != null) {
rtn = visitLiteral(ctx.literal());
}
//变量
else if (ctx.IDENTIFIER() != null) {
Symbol symbol = at.symbolOfNode.get(ctx);
if (symbol instanceof Variable) {
rtn = getLValue((Variable) symbol);
} else if (symbol instanceof Function) {
FunctionObject obj = new FunctionObject((Function) symbol);
rtn = obj;
}
}
//括号括起来的表达式
else if (ctx.expression() != null){
rtn = visitExpression(ctx.expression());
}
//this
else if (ctx.THIS() != null){
This thisRef = (This)at.symbolOfNode.get(ctx);
rtn = getLValue(thisRef);
}
//super
else if (ctx.SUPER() != null){
Super superRef = (Super) at.symbolOfNode.get(ctx);
rtn = getLValue(superRef);
}
return rtn;
}
@Override
public Object visitPrimitiveType(PrimitiveTypeContext ctx) {
Object rtn = null;
if (ctx.INT() != null) {
rtn = PlayScriptParser.INT;
} else if (ctx.LONG() != null) {
rtn = PlayScriptParser.LONG;
} else if (ctx.FLOAT() != null) {
rtn = PlayScriptParser.FLOAT;
} else if (ctx.DOUBLE() != null) {
rtn = PlayScriptParser.DOUBLE;
} else if (ctx.BOOLEAN() != null) {
rtn = PlayScriptParser.BOOLEAN;
} else if (ctx.CHAR() != null) {
rtn = PlayScriptParser.CHAR;
} else if (ctx.SHORT() != null) {
rtn = PlayScriptParser.SHORT;
} else if (ctx.BYTE() != null) {
rtn = PlayScriptParser.BYTE;
}
return rtn;
}
@Override
public Object visitStatement(StatementContext ctx){
Object rtn = null;
if (ctx.statementExpression != null) {
rtn = visitExpression(ctx.statementExpression);
} else if (ctx.IF() != null) {
Boolean condition = (Boolean) visitParExpression(ctx.parExpression());
if (Boolean.TRUE == condition) {
rtn = visitStatement(ctx.statement(0));
} else if (ctx.ELSE() != null) {
rtn = visitStatement(ctx.statement(1));
}
}
//while循环
else if (ctx.WHILE() != null) {
if (ctx.parExpression().expression() != null && ctx.statement(0) != null) {
while (true) {
//每次循环都要计算一下循环条件
Boolean condition = true;
Object value = visitExpression(ctx.parExpression().expression());
if (value instanceof LValue) {
condition = (Boolean) ((LValue) value).getValue();
} else {
condition = (Boolean) value;
}
if (condition) {
//执行while后面的语句
if (condition) {
rtn = visitStatement(ctx.statement(0));
//break
if (rtn instanceof BreakObject){
rtn = null; //清除BreakObject,也就是只跳出一层循环
break;
}
//return
else if (rtn instanceof ReturnObject){
break;
}
}
}
else{
break;
}
}
}
}
//for循环
else if (ctx.FOR() != null) {
// 添加StackFrame
BlockScope scope = (BlockScope) at.node2Scope.get(ctx);
StackFrame frame = new StackFrame(scope);
// frame.parentFrame = stack.peek();
pushStack(frame);
ForControlContext forControl = ctx.forControl();
if (forControl.enhancedForControl() != null) {
} else {
// 初始化部分执行一次
if (forControl.forInit() != null) {
rtn = visitForInit(forControl.forInit());
}
while (true) {
Boolean condition = true; // 如果没有条件判断部分,意味着一直循环
if (forControl.expression() != null) {
Object value = visitExpression(forControl.expression());
if (value instanceof LValue) {
condition = (Boolean) ((LValue) value).getValue();
} else {
condition = (Boolean) value;
}
}
if (condition) {
// 执行for的语句体
rtn = visitStatement(ctx.statement(0));
//处理break
if (rtn instanceof BreakObject){
rtn = null;
break;
}
//return
else if (rtn instanceof ReturnObject){
break;
}
// 执行forUpdate,通常是“i++”这样的语句。这个执行顺序不能出错。
if (forControl.forUpdate != null) {
visitExpressionList(forControl.forUpdate);
}
} else {
break;
}
}
}
// 去掉StackFrame
popStack();
}
//block
else if (ctx.blockLabel != null) {
rtn = visitBlock(ctx.blockLabel);
}
//break语句
else if (ctx.BREAK() != null) {
rtn = BreakObject.instance();
}
//return语句
else if (ctx.RETURN() != null) {
if (ctx.expression() != null) {
rtn = visitExpression(ctx.expression());
//return语句应该不需要左值 //TODO 取左值的场景需要优化,目前都是取左值。
if (rtn instanceof LValue){
rtn = ((LValue)rtn).getValue();
}
// 把闭包涉及的环境变量都打包带走
if (rtn instanceof FunctionObject) {
FunctionObject functionObject = (FunctionObject) rtn;
getClosureValues(functionObject.function, functionObject);
}
//如果返回的是一个对象,那么检查它的所有属性里有没有是闭包的。//TODO 如果属性仍然是一个对象,可能就要向下递归查找了。
else if (rtn instanceof ClassObject){
ClassObject classObject = (ClassObject)rtn;
getClosureValues(classObject);
}
}
//把真实的返回值封装在一个ReturnObject对象里,告诉visitBlockStatements停止执行下面的语句
rtn = new ReturnObject(rtn);
}
return rtn;
}
@Override
public Object visitTypeType(TypeTypeContext ctx) {
return visitPrimitiveType(ctx.primitiveType());
}
@Override
public Object visitVariableDeclarator(VariableDeclaratorContext ctx) {
Object rtn = null;
LValue lValue = (LValue) visitVariableDeclaratorId(ctx.variableDeclaratorId());
if (ctx.variableInitializer() != null) {
rtn = visitVariableInitializer(ctx.variableInitializer());
if (rtn instanceof LValue) {
rtn = ((LValue) rtn).getValue();
}
lValue.setValue(rtn);
}
return rtn;
}
@Override
public Object visitVariableDeclaratorId(VariableDeclaratorIdContext ctx) {
Object rtn = null;
Symbol symbol = at.symbolOfNode.get(ctx);
rtn = getLValue((Variable) symbol);
return rtn;
}
@Override
public Object visitVariableDeclarators(VariableDeclaratorsContext ctx) {
Object rtn = null;
// Integer typeType = (Integer)visitTypeType(ctx.typeType()); //后面要利用这个类型信息
for (VariableDeclaratorContext child : ctx.variableDeclarator()) {
rtn = visitVariableDeclarator(child);
}
return rtn;
}