diff --git a/src/ast.js b/src/ast.js index 1a08082..05b306a 100644 --- a/src/ast.js +++ b/src/ast.js @@ -310,6 +310,7 @@ export class Binary extends Expression { // Null check for the operations. if (left == null || right == null) { + switch (this.operation) { case '+': if (left != null) return left; @@ -366,6 +367,24 @@ export class PrefixNot extends Expression { } } + +export class PrefixPlus extends Expression { + constructor(operation:string, expression:Expression){ + super(); + + this.operation = operation; + this.expression = expression; + } + + eval(scope, filters=defaultFilterMap){ + return +this.expression.eval(scope); + } + + accept(visitor){ + visitor.visitPrefix(this); + } +} + export class LiteralPrimitive extends Expression { constructor(value){ super(); diff --git a/src/parser.js b/src/parser.js index ca285e9..2424a49 100644 --- a/src/parser.js +++ b/src/parser.js @@ -1,7 +1,7 @@ import {Lexer,Token} from './lexer'; import {Expression,ArrayOfExpression,Chain,Filter,Assign, Conditional, AccessScope, AccessMember, AccessKeyed, - CallScope, CallFunction, CallMember, PrefixNot, + CallScope, CallFunction, CallMember, PrefixNot, PrefixPlus, Binary, LiteralPrimitive, LiteralArray, LiteralObject, LiteralString} from './ast'; var EOF = new Token(-1, null); @@ -205,7 +205,7 @@ export class ParserImplementation { parsePrefix():Expression { if (this.optional('+')) { - return this.parsePrefix(); // TODO(kasperl): This is different than the original parser. + return new PrefixPlus('+', this.parsePrefix()); } else if (this.optional('-')) { return new Binary('-', new LiteralPrimitive(0), this.parsePrefix()); } else if (this.optional('!')) { diff --git a/test/parser.spec.js b/test/parser.spec.js index f03b1de..74f0416 100644 --- a/test/parser.spec.js +++ b/test/parser.spec.js @@ -24,7 +24,12 @@ describe('parser', () => { it('should parse unary - expressions', () => { expect(evaluate("-1")).toEqual(-1); + }); + + it('should parse unary + expressions', () => { expect(evaluate("+1")).toEqual(1); + expect(evaluate("+'1'")).toEqual(1); + expect(evaluate("+'not a number'")).toEqual(NaN); }); it('should parse unary ! expressions', () => {