-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRunner.dart
More file actions
103 lines (86 loc) · 2.8 KB
/
Runner.dart
File metadata and controls
103 lines (86 loc) · 2.8 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
library JavaEvaluator;
import 'ast.dart';
import 'types.dart';
import 'java/JavaLang.dart';
import 'web/site.dart';
part 'evaluator.dart';
part 'environment.dart';
part 'classloader.dart';
typedef dynamic EvalMethod(List args);
class Runner {
Environment environment;
final Program program;
ASTNode get current => environment._evaluator.current;
dynamic get lastValue => environment._evaluator.lastValue;
dynamic get next => environment.topStatement();
Runner(this.program) {
environment = new Environment();
ClassLoader loader = new ClassLoader(environment, environment._evaluator);
program.compilationUnits.forEach((unit) => loader.loadUnit(unit));
//perform static loading
while(!isDone())
step();
if(!program.mainSelectors.isEmpty){
MemberSelect main = program.mainSelectors.last;
print("main selector: ${main}");
StaticClass clazz = environment.lookupClass(main.owner);
environment.loadMethod(main.member_id,
[environment.newArray(0, null, const TypeNode.fixed(TypeNode.STRING))],
inClass:clazz);
}
}
bool isDone(){
return environment.isDone;
}
void step(){
var toEval = environment.popStatement();
print("toEval: $toEval");
environment._evaluator.returnValues;
print("preEvaluation: $current - id: ${current != null ? current.nodeId : "null"}");
BlockScope currentBlock = environment.currentBlock;
var result = environment._evaluator.eval(toEval);
if(result is EvalTree){
currentBlock.pushStatement(result);
}
if(current != null){
print("step: ${current} - id: ${current.nodeId}");
}
print("=> ${environment._evaluator.lastValue}");
print("-----");
}
}
class EvalTree extends ASTNode {
final List _args;
final List _evaledArgs = [];
final Evaluator _evaluator;
EvalMethod _method;
final _origExpr;
EvalTree(this._origExpr, this._evaluator, [this._method, this._args = const []]) : super();
dynamic execute(){
//evaluate arguments
//
//skip literals
while(!_args.isEmpty && _args.first is Literal){
_evaledArgs.add(_evaluator.eval(_args.removeAt(0)));
}
if(!_args.isEmpty){
var evaledArg = _evaluator.eval(_args.first);
if(evaledArg is EvalTree)
_args[0] = evaledArg;
else {
_evaledArgs.add(evaledArg);
_args.removeAt(0);
}
//return _this_ since it has now stepped one execution
return this;
}
if(_origExpr != null)
_evaluator.current = _origExpr;
var value = _method(_evaledArgs);
_evaluator.lastValue = value;
return value;
}
String toString() {
return "evalTree [$_origExpr ${_evaledArgs.isEmpty ? "" : _evaledArgs.reduce((r,e) => "$r, $e")}${_args.isEmpty ? "" : _args.reduce((r,e) => "$r, $e")}]";
}
}