2014-11-04 10:19:37 -08:00
|
|
|
import {FIELD, toBool, autoConvertAdd, isBlank, FunctionWrapper, BaseException} from "facade/lang";
|
2014-11-05 13:48:36 -08:00
|
|
|
import {List, Map, ListWrapper, MapWrapper} from "facade/collection";
|
2014-10-30 23:47:22 -07:00
|
|
|
|
2014-10-28 12:22:38 -04:00
|
|
|
export class AST {
|
2014-11-04 15:51:56 -08:00
|
|
|
eval(context) {
|
|
|
|
throw new BaseException("Not supported");
|
2014-10-28 12:22:38 -04:00
|
|
|
}
|
|
|
|
|
2014-11-05 13:53:45 -08:00
|
|
|
get isAssignable() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-05 10:00:19 -08:00
|
|
|
assign(context, value) {
|
|
|
|
throw new BaseException("Not supported");
|
|
|
|
}
|
|
|
|
|
2014-10-28 12:22:38 -04:00
|
|
|
visit(visitor) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ImplicitReceiver extends AST {
|
2014-11-04 15:51:56 -08:00
|
|
|
eval(context) {
|
2014-10-28 12:22:38 -04:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit(visitor) {
|
|
|
|
visitor.visitImplicitReceiver(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 10:00:19 -08:00
|
|
|
export class Chain extends AST {
|
|
|
|
constructor(expressions:List) {
|
|
|
|
this.expressions = expressions;
|
|
|
|
}
|
|
|
|
|
|
|
|
eval(context) {
|
|
|
|
var result;
|
|
|
|
for (var i = 0; i < this.expressions.length; i++) {
|
|
|
|
var last = this.expressions[i].eval(context);
|
|
|
|
if (last != null) result = last;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-03 17:25:16 -08:00
|
|
|
export class Conditional extends AST {
|
2014-11-04 15:54:00 -08:00
|
|
|
@FIELD('final condition:AST')
|
|
|
|
@FIELD('final trueExp:AST')
|
|
|
|
@FIELD('final falseExp:AST')
|
2014-11-04 15:51:56 -08:00
|
|
|
constructor(condition:AST, trueExp:AST, falseExp:AST){
|
2014-11-03 17:25:16 -08:00
|
|
|
this.condition = condition;
|
2014-11-04 15:51:56 -08:00
|
|
|
this.trueExp = trueExp;
|
|
|
|
this.falseExp = falseExp;
|
2014-11-03 17:25:16 -08:00
|
|
|
}
|
|
|
|
|
2014-11-04 15:51:56 -08:00
|
|
|
eval(context) {
|
|
|
|
if(this.condition.eval(context)) {
|
|
|
|
return this.trueExp.eval(context);
|
2014-11-03 17:25:16 -08:00
|
|
|
} else {
|
2014-11-04 15:51:56 -08:00
|
|
|
return this.falseExp.eval(context);
|
2014-11-03 17:25:16 -08:00
|
|
|
}
|
2014-10-30 23:47:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 17:25:16 -08:00
|
|
|
export class FieldRead extends AST {
|
2014-11-04 15:54:00 -08:00
|
|
|
@FIELD('final receiver:AST')
|
|
|
|
@FIELD('final name:string')
|
|
|
|
@FIELD('final getter:Function')
|
2014-11-05 10:00:19 -08:00
|
|
|
@FIELD('final setter:Function')
|
|
|
|
constructor(receiver:AST, name:string, getter:Function, setter:Function) {
|
2014-10-28 12:22:38 -04:00
|
|
|
this.receiver = receiver;
|
|
|
|
this.name = name;
|
|
|
|
this.getter = getter;
|
2014-11-05 10:00:19 -08:00
|
|
|
this.setter = setter;
|
2014-10-28 12:22:38 -04:00
|
|
|
}
|
|
|
|
|
2014-11-04 15:51:56 -08:00
|
|
|
eval(context) {
|
|
|
|
return this.getter(this.receiver.eval(context));
|
2014-10-28 12:22:38 -04:00
|
|
|
}
|
|
|
|
|
2014-11-05 13:53:45 -08:00
|
|
|
get isAssignable() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-05 10:00:19 -08:00
|
|
|
assign(context, value) {
|
|
|
|
return this.setter(this.receiver.eval(context), value);
|
|
|
|
}
|
|
|
|
|
2014-10-28 12:22:38 -04:00
|
|
|
visit(visitor) {
|
|
|
|
visitor.visitFieldRead(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 10:00:19 -08:00
|
|
|
export class KeyedAccess extends AST {
|
|
|
|
constructor(obj:AST, key:AST) {
|
|
|
|
this.obj = obj;
|
|
|
|
this.key = key;
|
|
|
|
}
|
2014-11-05 13:48:36 -08:00
|
|
|
eval(context) {
|
|
|
|
var obj = this.obj.eval(context);
|
|
|
|
var key = this.key.eval(context);
|
|
|
|
|
|
|
|
if (obj instanceof Map) {
|
|
|
|
return MapWrapper.get(obj, key);
|
|
|
|
} else if (obj instanceof List) {
|
|
|
|
return ListWrapper.get(obj, key);
|
|
|
|
} else {
|
|
|
|
throw new BaseException(`Cannot access ${key} on ${obj}`);
|
|
|
|
}
|
|
|
|
}
|
2014-11-05 13:53:45 -08:00
|
|
|
|
|
|
|
get isAssignable() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-05 13:48:36 -08:00
|
|
|
assign(context, value) {
|
|
|
|
var obj = this.obj.eval(context);
|
|
|
|
var key = this.key.eval(context);
|
|
|
|
|
|
|
|
if (obj instanceof Map) {
|
|
|
|
MapWrapper.set(obj, key, value);
|
|
|
|
} else if (obj instanceof List) {
|
|
|
|
ListWrapper.set(obj, key, value);
|
|
|
|
} else {
|
|
|
|
throw new BaseException(`Cannot access ${key} on ${obj}`);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-05 10:00:19 -08:00
|
|
|
}
|
|
|
|
|
2014-11-04 10:19:37 -08:00
|
|
|
export class Formatter extends AST {
|
2014-11-04 15:54:00 -08:00
|
|
|
@FIELD('final exp:AST')
|
|
|
|
@FIELD('final name:string')
|
|
|
|
@FIELD('final args:List<AST>')
|
2014-11-04 10:19:37 -08:00
|
|
|
constructor(exp:AST, name:string, args:List) {
|
|
|
|
this.exp = exp;
|
|
|
|
this.name = name;
|
|
|
|
this.args = args;
|
|
|
|
this.allArgs = ListWrapper.concat([exp], args);
|
|
|
|
}
|
|
|
|
|
|
|
|
visit(visitor) {
|
|
|
|
visitor.visitFormatter(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 17:25:16 -08:00
|
|
|
export class LiteralPrimitive extends AST {
|
2014-10-30 23:47:22 -07:00
|
|
|
@FIELD('final value')
|
|
|
|
constructor(value) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
2014-11-04 15:51:56 -08:00
|
|
|
eval(context) {
|
2014-10-30 23:47:22 -07:00
|
|
|
return this.value;
|
|
|
|
}
|
|
|
|
visit(visitor) {
|
|
|
|
visitor.visitLiteralPrimitive(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 13:48:36 -08:00
|
|
|
export class LiteralArray extends AST {
|
|
|
|
@FIELD('final expressions:List')
|
|
|
|
constructor(expressions:List) {
|
|
|
|
this.expressions = expressions;
|
|
|
|
}
|
|
|
|
eval(context) {
|
|
|
|
return ListWrapper.map(this.expressions, (e) => e.eval(context));
|
|
|
|
}
|
|
|
|
visit(visitor) {
|
|
|
|
visitor.visitLiteralArray(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LiteralMap extends AST {
|
|
|
|
@FIELD('final keys:List')
|
|
|
|
@FIELD('final values:List')
|
|
|
|
constructor(keys:List, values:List) {
|
|
|
|
this.keys = keys;
|
|
|
|
this.values = values;
|
|
|
|
}
|
|
|
|
|
|
|
|
eval(context) {
|
|
|
|
var res = MapWrapper.create();
|
|
|
|
for(var i = 0; i < this.keys.length; ++i) {
|
|
|
|
MapWrapper.set(res, this.keys[i], this.values[i].eval(context));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 17:25:16 -08:00
|
|
|
export class Binary extends AST {
|
2014-10-30 23:47:22 -07:00
|
|
|
@FIELD('final operation:string')
|
2014-11-03 17:25:16 -08:00
|
|
|
@FIELD('final left:AST')
|
|
|
|
@FIELD('final right:AST')
|
|
|
|
constructor(operation:string, left:AST, right:AST) {
|
2014-10-30 23:47:22 -07:00
|
|
|
this.operation = operation;
|
|
|
|
this.left = left;
|
|
|
|
this.right = right;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit(visitor) {
|
|
|
|
visitor.visitBinary(this);
|
|
|
|
}
|
|
|
|
|
2014-11-04 15:51:56 -08:00
|
|
|
eval(context) {
|
|
|
|
var left = this.left.eval(context);
|
2014-10-30 23:47:22 -07:00
|
|
|
switch (this.operation) {
|
2014-11-04 15:51:56 -08:00
|
|
|
case '&&': return toBool(left) && toBool(this.right.eval(context));
|
|
|
|
case '||': return toBool(left) || toBool(this.right.eval(context));
|
2014-10-30 23:47:22 -07:00
|
|
|
}
|
2014-11-04 15:51:56 -08:00
|
|
|
var right = this.right.eval(context);
|
2014-10-30 23:47:22 -07:00
|
|
|
|
|
|
|
// Null check for the operations.
|
2014-11-04 15:51:56 -08:00
|
|
|
if (left == null || right == null) {
|
|
|
|
throw new BaseException("One of the operands is null");
|
|
|
|
}
|
2014-10-30 23:47:22 -07:00
|
|
|
|
|
|
|
switch (this.operation) {
|
|
|
|
case '+' : return autoConvertAdd(left, right);
|
|
|
|
case '-' : return left - right;
|
|
|
|
case '*' : return left * right;
|
|
|
|
case '/' : return left / right;
|
|
|
|
// This exists only in Dart, TODO(rado) figure out whether to support it.
|
|
|
|
// case '~/' : return left ~/ right;
|
|
|
|
case '%' : return left % right;
|
|
|
|
case '==' : return left == right;
|
|
|
|
case '!=' : return left != right;
|
|
|
|
case '<' : return left < right;
|
|
|
|
case '>' : return left > right;
|
|
|
|
case '<=' : return left <= right;
|
|
|
|
case '>=' : return left >= right;
|
|
|
|
case '^' : return left ^ right;
|
|
|
|
case '&' : return left & right;
|
|
|
|
}
|
|
|
|
throw 'Internal error [$operation] not handled';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 17:25:16 -08:00
|
|
|
export class PrefixNot extends AST {
|
2014-10-30 23:47:22 -07:00
|
|
|
@FIELD('final operation:string')
|
2014-11-03 17:25:16 -08:00
|
|
|
@FIELD('final expression:AST')
|
|
|
|
constructor(expression:AST) {
|
2014-10-30 23:47:22 -07:00
|
|
|
this.expression = expression;
|
|
|
|
}
|
|
|
|
visit(visitor) { visitor.visitPrefixNot(this); }
|
2014-11-04 15:51:56 -08:00
|
|
|
eval(context) {
|
|
|
|
return !toBool(this.expression.eval(context));
|
2014-10-30 23:47:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 10:00:19 -08:00
|
|
|
export class Assignment extends AST {
|
|
|
|
@FIELD('final target:AST')
|
|
|
|
@FIELD('final value:AST')
|
|
|
|
constructor(target:AST, value:AST) {
|
|
|
|
this.target = target;
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
visit(visitor) { visitor.visitAssignment(this); }
|
|
|
|
|
|
|
|
eval(context) {
|
|
|
|
return this.target.assign(context, this.value.eval(context));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 12:22:38 -04:00
|
|
|
//INTERFACE
|
|
|
|
export class AstVisitor {
|
|
|
|
visitImplicitReceiver(ast:ImplicitReceiver) {}
|
|
|
|
visitFieldRead(ast:FieldRead) {}
|
2014-10-30 23:47:22 -07:00
|
|
|
visitBinary(ast:Binary) {}
|
|
|
|
visitPrefixNot(ast:PrefixNot) {}
|
|
|
|
visitLiteralPrimitive(ast:LiteralPrimitive) {}
|
2014-11-04 10:19:37 -08:00
|
|
|
visitFormatter(ast:Formatter) {}
|
2014-11-05 10:00:19 -08:00
|
|
|
visitAssignment(ast:Assignment) {}
|
2014-11-05 13:48:36 -08:00
|
|
|
visitLiteralArray(ast:LiteralArray) {}
|
2014-10-30 23:47:22 -07:00
|
|
|
}
|
2014-11-04 10:19:37 -08:00
|
|
|
|
|
|
|
var _evalListCache = [[],[0],[0,0],[0,0,0],[0,0,0,0],[0,0,0,0,0]];
|
2014-11-04 15:51:56 -08:00
|
|
|
function evalList(context, exps:List){
|
2014-11-04 10:19:37 -08:00
|
|
|
var length = exps.length;
|
|
|
|
var result = _evalListCache[length];
|
|
|
|
for (var i = 0; i < length; i++) {
|
2014-11-04 15:51:56 -08:00
|
|
|
result[i] = exps[i].eval(context);
|
2014-11-04 10:19:37 -08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|