refactor(Parser): cleanup

This commit is contained in:
vsavkin 2014-11-06 09:09:34 -08:00
parent 693489ce38
commit 7908533336
4 changed files with 18 additions and 14 deletions

View File

@ -1,4 +1,4 @@
import {FIELD, toBool, autoConvertAdd, isBlank, FunctionWrapper, BaseException} from "facade/lang"; import {FIELD, autoConvertAdd, isBlank, isPresent, FunctionWrapper, BaseException} from "facade/lang";
import {List, Map, ListWrapper, MapWrapper} from "facade/collection"; import {List, Map, ListWrapper, MapWrapper} from "facade/collection";
import {ClosureMap} from "./closure_map"; import {ClosureMap} from "./closure_map";
@ -29,6 +29,9 @@ export class ImplicitReceiver extends AST {
} }
} }
/**
* Multiple expressions separated by a semicolon.
*/
export class Chain extends AST { export class Chain extends AST {
@FIELD('final expressions:List') @FIELD('final expressions:List')
constructor(expressions:List) { constructor(expressions:List) {
@ -39,7 +42,7 @@ export class Chain extends AST {
var result; var result;
for (var i = 0; i < this.expressions.length; i++) { for (var i = 0; i < this.expressions.length; i++) {
var last = this.expressions[i].eval(context); var last = this.expressions[i].eval(context);
if (last != null) result = last; if (isPresent(last)) result = last;
} }
return result; return result;
} }
@ -118,7 +121,7 @@ export class KeyedAccess extends AST {
} else if (obj instanceof List) { } else if (obj instanceof List) {
return ListWrapper.get(obj, key); return ListWrapper.get(obj, key);
} else { } else {
throw new BaseException(`Cannot access ${key} on ${obj}`); return obj[key];
} }
} }
@ -135,7 +138,7 @@ export class KeyedAccess extends AST {
} else if (obj instanceof List) { } else if (obj instanceof List) {
ListWrapper.set(obj, key, value); ListWrapper.set(obj, key, value);
} else { } else {
throw new BaseException(`Cannot access ${key} on ${obj}`); obj[key] = value;
} }
return value; return value;
} }
@ -225,14 +228,14 @@ export class Binary extends AST {
eval(context) { eval(context) {
var left = this.left.eval(context); var left = this.left.eval(context);
switch (this.operation) { switch (this.operation) {
case '&&': return toBool(left) && toBool(this.right.eval(context)); case '&&': return left && this.right.eval(context);
case '||': return toBool(left) || toBool(this.right.eval(context)); case '||': return left || this.right.eval(context);
} }
var right = this.right.eval(context); var right = this.right.eval(context);
// Null check for the operations. // Null check for the operations.
if (left == null || right == null) { if (isBlank(left)|| isBlank(right)) {
throw new BaseException("One of the operands is null"); throw new BaseException("One of the operands is not defined");
} }
switch (this.operation) { switch (this.operation) {
@ -266,7 +269,7 @@ export class PrefixNot extends AST {
} }
eval(context) { eval(context) {
return !toBool(this.expression.eval(context)); return !this.expression.eval(context);
} }
visit(visitor) { visit(visitor) {

View File

@ -10,6 +10,6 @@ export class ClosureMap {
} }
fn(name:string) { fn(name:string) {
return new Function('o', 'pos', 'return o.' + name + '.apply(o, pos);'); return new Function('o', 'args', 'return o.' + name + '.apply(o, args);');
} }
} }

View File

@ -209,10 +209,11 @@ export function main() {
it('should evaluate map', () => { it('should evaluate map', () => {
expectEval("{}").toEqual(MapWrapper.create()); expectEval("{}").toEqual(MapWrapper.create());
expectEval("{a:'b'}").toEqual(MapWrapper.createFromPairs([["a", "b"]])); expectEval("{a:'b'}['a']").toEqual('b');
expectEval("{'a':'b'}").toEqual(MapWrapper.createFromPairs([["a", "b"]])); expectEval("{'a':'b'}['a']").toEqual('b');
expectEval("{\"a\":'b'}").toEqual(MapWrapper.createFromPairs([["a", "b"]])); expectEval("{\"a\":'b'}['a']").toEqual('b');
expectEval("{\"a\":'b'}['a']").toEqual("b"); expectEval("{\"a\":'b'}['a']").toEqual("b");
expectEval("{}['a']").not.toBeDefined();
expectEval("{\"a\":'b'}['invalid']").not.toBeDefined(); expectEval("{\"a\":'b'}['invalid']").not.toBeDefined();
}); });

View File

@ -6,7 +6,7 @@ export var Set = window.Set;
export class MapWrapper { export class MapWrapper {
static create():Map { return new Map(); } static create():Map { return new Map(); }
static createFromPairs(pairs:List):Map { return new Map(pairs); } static createFromPairs(pairs:List):Map {return new Map(pairs);}
static get(m, k) { return m.get(k); } static get(m, k) { return m.get(k); }
static set(m, k, v) { m.set(k,v); } static set(m, k, v) { m.set(k,v); }
static contains(m, k) { return m.has(k); } static contains(m, k) { return m.has(k); }