-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenvironment.dart
More file actions
498 lines (389 loc) · 13.6 KB
/
environment.dart
File metadata and controls
498 lines (389 loc) · 13.6 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
part of JavaEvaluator;
class Environment {
int _counter = 0;
Evaluator _evaluator;
List<MethodScope> methodStack = new List<MethodScope>();
Map<ReferenceValue, dynamic> values = new Map<ReferenceValue, dynamic>();
Map<Identifier, Package> packages = new Map<Identifier, Package>();
BlockScope get currentBlock => methodStack.last.currentBlock;
int get newAddress => ++_counter;
Environment(){
packages[Identifier.DEFAULT_PACKAGE] = new Package(Identifier.DEFAULT_PACKAGE);
_evaluator = new Evaluator(this);
}
dynamic _lookup(final dynamic select){
Identifier name;
dynamic parent = methodStack.last;
//recursive lookup
if(select is MemberSelect){
parent = _lookup(select.owner);
if(parent is ReferenceValue)
parent = values[parent];
name = select.member_id;
}
else name = select;
//lookup of class should only be allowed if parent is Package
if(parent is Package){
return parent.getMember(name);
}
//check if it is a variable, this must be the first lookup, since it trumps every other
Value val = parent.lookup(name);
if(val != null)
return val;
//if this is the root of the lookup, i.e. ${select is Identifier}, then we can allow to look
//for imports in the parent (class it belongs to) of the current method scope and
//in the list of root packages
if(select is Identifier){
StaticClass c = methodStack.last.lookupClass(select);
if(c != null)
return c;
if(packages.containsKey(select))
return packages[select];
}
if(parent is StaticClass){
throw "Don't support lookup in inner classes yet!";
}
throw "Uncovered lookup case...";
}
Value lookup(name){
print("Looking up: $name");
if(name is Identifier)
return methodStack.last.lookup(name);
return _lookup(name);
}
dynamic _lookupClass(select){
//if this is the root of the lookup, i.e. ${select is Identifier}, then we can allow to look
//for imports in the parent (class it belongs to) of the current method scope and
//in the list of root packages
if(select is Identifier){
StaticClass c = methodStack.last.lookupClass(select);
if(c != null)
return c;
if(packages.containsKey(select))
return packages[select];
}
assert(select is MemberSelect);
//recursive lookup
var parent = _lookupClass(select.owner);
assert(parent is StaticClass || parent is Package);
Identifier name = select.member_id;
if(parent is Package){
return parent.getMember(name);
}
else if(parent is StaticClass){
throw "Don't support lookup for inner classes yet!";
}
throw "Uncovered lookup case...";
}
StaticClass lookupClass(select){
return _lookupClass(select);
}
void assign(final dynamic select, Value value){
print("assigning to: $select");
if(select is Identifier){
methodStack.last.assign(select, value);
}
else {
assert(select is MemberSelect);
var clazz = _lookup(select.owner);
if(clazz is ReferenceValue)
clazz = values[clazz];
assert(clazz is ClassScope);
clazz.assign(select.member_id, value);
}
}
void newVariable(Identifier name, [Value value = ReferenceValue.invalid]){
methodStack.last.newVariable(name, value);
}
ReferenceValue addLibraryObject(dynamic obj){
ReferenceValue ref = new ReferenceValue(newAddress);
values[ref] = obj;
return ref;
}
ReferenceValue newObject(StaticClass clazz, List<Value> constructorArgs){
List<EvalTree> initializers = new List<EvalTree>();
ClassInstance inst = new ClassInstance(clazz);
//declare all variables and create assignments of the initializers
clazz._declaration.instanceVariables.forEach((Variable v){
Identifier id = new Identifier.fixed(v.name);
inst.newVariable(id);
if(v.initializer != null)
initializers.add(new EvalTree(v, _evaluator, (List args) => assign(id, args.first), [v.initializer]));
});
//add method call to constructor
initializers.add(new EvalTree(null, _evaluator, (List args){
loadMethod(Identifier.CONSTRUCTOR, constructorArgs);
}, []));
loadScope(new MethodScope("<new>", initializers, inst));
return _newValue(inst);
}
void arrayAssign(ReferenceValue array, int index, Value value) {
(values[array] as Array)[index] = value;
}
Value getArrayValue(ReferenceValue array, int index){
return values[array][index];
}
ReferenceValue newArray(int size, Value value, TypeNode type) {
return _newValue(new Array(size, value, type));
}
ReferenceValue _newValue(dynamic value){
ReferenceValue addr = new ReferenceValue(newAddress);
values[addr] = value;
return addr;
}
bool get isDone {
if(methodStack.any((MethodScope m) => !m.isDone))
return false;
return true;
}
dynamic popStatement(){
while(!methodStack.isEmpty && methodStack.last.isDone)
methodStack.removeLast();
return methodStack.last.popStatement();
}
dynamic topStatement(){
while(!methodStack.isEmpty && methodStack.last.isDone)
methodStack.removeLast();
return methodStack.last.topStatement();
}
/**
* Returns true if it has a return value,
* TODO should probably return something more
* fitting like the toReturn EvalTree maybe.
* TODO fix return type of loadMethod
*/
dynamic loadMethod(Identifier name, List args, {ClassScope inClass}) {
ClassScope parent = methodStack.last.parentScope;
if(inClass != null)
parent = inClass;
if(parent is JDKString){
_evaluator.returnValues.last._method = (v) => parent.charAt(args[0]);
}
else {
MethodDecl method = parent.methods.singleWhere((MethodDecl m)
=> m.name == name.name && _checkParamArgTypeMatch(m.type.parameters, args.map(typeOf).toList()));
print("loading method: void=${method.type.returnType.isVoid} ${method.name}");
print("body: ${method.body}");
methodStack.add(new MethodScope(name.toString(), method.body, parent));
for(int i = 0; i < method.parameters.length; i++){
newVariable(new Identifier.fixed(method.parameters[i].name), args[i]);
}
return ! method.type.returnType.isVoid;
}
}
void addBlockScope(List statements){
methodStack.last.addBlock(new BlockScope(statements));
}
void methodReturn() { methodStack.removeLast(); }
void loadScope(MethodScope scope){ methodStack.add(scope); }
TypeNode typeOf(dynamic val){
if(val is ReferenceValue)
val = values[val];
if(val is ClassScope){
return new TypeNode(new Identifier.fixed(val.name.name));
}
else if(val is PrimitiveValue){
return new TypeNode(val.type);
}
else if(val is Array){
return val.type;
}
}
static bool _checkParamArgTypeMatch(List<TypeNode> parameters, List<TypeNode> args) {
if(parameters.length != args.length)
return false;
for(int i = 0; i < parameters.length; i++){
if(parameters[i] != args[i])
return false;
}
return true;
}
}
class Array {
final List _list;
final TypeNode type;
Array(int size, value, this.type) : _list = new List.filled(size, value);
operator[](int index) => _list[index];
void operator[]=(int index, value) { _list[index] = value; }
String toString() => _list.toString();
}
abstract class Scope {
final Map<Identifier, Value> _variables = new Map<Identifier, Value>();
Map<Identifier, Value> get variables => new Map.from(_variables);
Value _lookup(Identifier name) => _variables[name];
void _newVariable(Identifier name, [Value value = ReferenceValue.invalid]){ _variables[name] = value; }
bool _assign(Identifier name, Value value){
if(_variables.containsKey(name)){
_variables[name] = value;
return true;
}
return false;
}
Value lookup(Identifier name);
void newVariable(Identifier name, [Value value]);
bool assign(Identifier name, Value value);
}
class BlockScope extends Scope {
final List<dynamic> _statements;
BlockScope subScope;
BlockScope get currentBlock => subScope == null ? this : subScope.currentBlock;
BlockScope(List<dynamic> statements) : this._statements = statements.toList();
void addBlock(BlockScope block){
if(subScope == null)
subScope = block;
else
subScope.addBlock(block);
}
Value lookup(Identifier name){
Value val;
if(subScope != null)
val = subScope.lookup(name);
if(val != null)
return val;
return _lookup(name);
}
void newVariable(Identifier name, [Value value]){
if(subScope != null)
subScope.newVariable(name, value);
else
_newVariable(name, value);
}
bool assign(Identifier name, Value value){
if(subScope != null && subScope.assign(name, value))
return true;
return _assign(name, value);
}
StaticClass lookupClass(Identifier name) => null;
void pushStatement(dynamic statement) {
if(subScope != null)
subScope.pushStatement(statement);
else
_statements.insert(0, statement);
}
dynamic popStatement() {
//remove subScope if it is empty
if(subScope != null && subScope.isDone)
subScope = null;
if(subScope != null){
assert(!subScope.isDone);
return subScope.popStatement();
}
return _statements.removeAt(0);
}
dynamic topStatement() {
//remove subScope if it is empty
if(subScope != null && subScope.isDone)
subScope = null;
if(subScope != null){
assert(!subScope.isDone);
return subScope.topStatement();
}
return _statements.elementAt(0);
}
bool get isDone {
if(subScope != null && !subScope.isDone)
return false;
return _statements.isEmpty;
}
}
class MethodScope extends BlockScope {
final ClassScope parentScope;
String methodName;
MethodScope(this.methodName, List<dynamic> statements, ClassScope this.parentScope) : super(statements);
StaticClass lookupClass(Identifier name) => parentScope.lookupClass(name);
Value lookup(Identifier name){
Value val = super.lookup(name);
if(val != null)
return val;
return parentScope.lookup(name);
}
bool assign(Identifier name, Value value){
if(super.assign(name, value))
return true;
return parentScope.assign(name, value);
}
}
abstract class ClassScope extends Scope {
StaticClass lookupClass(Identifier name);
List<MethodDecl> get methods;
Identifier get name;
String toString() => "${_variables}";
}
class StaticClass extends ClassScope {
Package _package;
ClassDecl _declaration;
final Map<Identifier, StaticClass> _importedClasses = new Map<Identifier, StaticClass>();
final Map<Identifier, Package> _importedPackages = new Map<Identifier, Package>();
Package get package => _package;
Identifier get name => _declaration.name;
List<MethodDecl> get methods => _declaration.staticMethods;
StaticClass(ClassDecl this._declaration, Package this._package);
StaticClass.empty();
void addImport(dynamic import) {
if(import is StaticClass)
_importedClasses[import.name] = import;
else if(import is Package)
_importedPackages[import.name] = import;
else
throw "Can't add object of type ${import.runtimeType} as import to class $name";
}
StaticClass lookupClass(Identifier name) {
if(name == _declaration.name)
return this;
if(_importedClasses.containsKey(name))
return _importedClasses[name];
for(Package p in _importedPackages.values){
StaticClass c = p.getClass(name);
if(c != null)
return c;
}
return package.getClass(name);
}
Value lookup(Identifier name){
print("Looking for $name => ${_lookup(name)} in ${this.name}.");
return _lookup(name);
}
// => _lookup(name);
void newVariable(Identifier name, [Value value]){
_newVariable(name, value);
}
bool assign(Identifier name, Value value) => _assign(name, value);
}
class ClassInstance extends ClassScope {
final StaticClass _static;
Identifier get name => _static.name;
List<MethodDecl> get methods => new List<MethodDecl>()
..addAll(_static._declaration.instanceMethods)
..addAll(_static._declaration.staticMethods);
ClassInstance(StaticClass this._static);
StaticClass lookupClass(Identifier name) => _static.lookupClass(name);
Value lookup(Identifier name) {
Value val = _lookup(name);
if(val != null)
return val;
return _static.lookup(name);
}
void newVariable(Identifier name, [Value value]){
_newVariable(name, value);
}
bool assign(Identifier name, Value value){
if(_assign(name, value))
return true;
return _static.assign(name, value);
}
}
class Package {
final Identifier name;
final Map<String, dynamic> _members;
Package(this.name) : _members = new Map<String, dynamic>();
const Package.fixed(this.name, this._members);
void addMember(member){
assert(member is Package || member is StaticClass);
_members[member.name.name] = member;
}
dynamic getMember(Identifier name) => _members[name.name];
StaticClass getClass(Identifier name) => _members[name.name];
Package getPackage(Identifier name) => _members[name.name];
List<Package> get getPackages => _members.values.where((m) => m is Package).toList();
List<StaticClass> get getClasses => _members.values.where((c) => c is StaticClass).toList();
}