2014-10-30 23:47:22 -07:00
|
|
|
import {FIELD, toBool, autoConvertAdd} from "facade/lang";
|
|
|
|
|
2014-10-28 12:22:38 -04:00
|
|
|
export class AST {
|
2014-10-30 23:47:22 -07:00
|
|
|
eval(context, formatters) {
|
2014-10-28 12:22:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
visit(visitor) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ImplicitReceiver extends AST {
|
2014-10-30 23:47:22 -07:00
|
|
|
eval(context, formatters) {
|
2014-10-28 12:22:38 -04:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit(visitor) {
|
|
|
|
visitor.visitImplicitReceiver(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 17:25:16 -08:00
|
|
|
export class Conditional extends AST {
|
|
|
|
constructor(condition:AST, yes:AST, no:AST){
|
|
|
|
this.condition = condition;
|
|
|
|
this.yes = yes;
|
|
|
|
this.no = no;
|
|
|
|
}
|
|
|
|
|
|
|
|
eval(context, formatters) {
|
|
|
|
if(this.condition.eval(context, formatters)) {
|
|
|
|
return this.yes.eval(context, formatters);
|
|
|
|
} else {
|
|
|
|
return this.no.eval(context, formatters);
|
|
|
|
}
|
2014-10-30 23:47:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 17:25:16 -08:00
|
|
|
export class FieldRead extends AST {
|
2014-10-28 12:22:38 -04:00
|
|
|
constructor(receiver:AST, name:string, getter:Function) {
|
|
|
|
this.receiver = receiver;
|
|
|
|
this.name = name;
|
|
|
|
this.getter = getter;
|
|
|
|
}
|
|
|
|
|
2014-10-30 23:47:22 -07:00
|
|
|
eval(context, formatters) {
|
|
|
|
return this.getter(this.receiver.eval(context, formatters));
|
2014-10-28 12:22:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
visit(visitor) {
|
|
|
|
visitor.visitFieldRead(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;
|
|
|
|
}
|
|
|
|
eval(context, formatters) {
|
|
|
|
return this.value;
|
|
|
|
}
|
|
|
|
visit(visitor) {
|
|
|
|
visitor.visitLiteralPrimitive(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
eval(context, formatters) {
|
|
|
|
var left = this.left.eval(context, formatters);
|
|
|
|
switch (this.operation) {
|
|
|
|
case '&&': return toBool(left) && toBool(this.right.eval(context, formatters));
|
|
|
|
case '||': return toBool(left) || toBool(this.right.eval(context, formatters));
|
|
|
|
}
|
|
|
|
var right = this.right.eval(context, formatters);
|
|
|
|
|
|
|
|
// Null check for the operations.
|
|
|
|
if (left == null || right == null) {
|
|
|
|
switch (this.operation) {
|
|
|
|
case '+':
|
|
|
|
if (left != null) return left;
|
|
|
|
if (right != null) return right;
|
|
|
|
return 0;
|
|
|
|
case '-':
|
|
|
|
if (left != null) return left;
|
|
|
|
if (right != null) return 0 - right;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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); }
|
|
|
|
eval(context, formatters) {
|
|
|
|
return !toBool(this.expression.eval(context, formatters));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {}
|
|
|
|
}
|