Skip to content

Commit

Permalink
feat(expressions): add support for multi-statement expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
PK1A committed Aug 28, 2014
1 parent f0e82bc commit 5c10af9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion hsp/expressions/manipulator.js
Expand Up @@ -59,6 +59,7 @@ module.exports = function(input, inputTree) {
evaluator(tree.l, scope)[evaluator(tree.r, scope)] = newValue;
}
},
isAssignable : isAssignable
isAssignable: isAssignable,
isMultiStatement: tree instanceof Array
};
};
12 changes: 9 additions & 3 deletions hsp/expressions/parser.js
Expand Up @@ -284,16 +284,22 @@ function expression(rbp) {
* @return {Object} - parsed AST
*/
module.exports = function (input) {
var expr, exprs = [];

tokens = lexer(input);
token = undefined;
tokenIdx = 0;

if (tokens.length) {
advance(); //get the first token
var expr = expression(0);
advance('(end)'); //make sure that we are at the end of an expression
return expr;
while(token.id !== '(end)') {
expr = expression(0);
exprs.push(expr);
if (token.v === ',') {
advance(',');
}
}
return exprs.length === 1 ? exprs[0] : exprs;
} else {
return {f: 0, a: 'literal', v: undefined};
}
Expand Down
13 changes: 13 additions & 0 deletions test/expressions/manipulator.spec.js
Expand Up @@ -259,6 +259,19 @@ describe('getValue', function () {
expect(expression("{foo: 'bar', foo2: 'baz'}").getValue({})).to.eql({foo: 'bar', foo2: 'baz'});
expect(expression("{foo: {foo2: 'baz'}}").getValue({})).to.eql({foo: {foo2: 'baz'}});
});

describe('multiple statements in an expression', function () {

it('should allow multiple comma-separated statements', function () {
expect(expression("foo, bar").getValue({foo: 'foo', bar: 'bar'}))
.to.eql(['foo', 'bar']);
});

it('should mark expressions as multi-statement', function () {
expect(expression("foo, bar").isMultiStatement).to.be.ok();
expect(expression("foo").isMultiStatement).to.not.be.ok();
});
});
});

describe('forgiving evaluation of expressions', function () {
Expand Down

0 comments on commit 5c10af9

Please sign in to comment.