Skip to content

Commit

Permalink
Handle nested call functions
Browse files Browse the repository at this point in the history
Allows use of returning functions from functions
  • Loading branch information
Mattchewone committed Apr 24, 2019
1 parent 5072435 commit 911ed9c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,20 @@ var expression = {
// foo[bar()]
else if(token === "(") {
top = stack.top();
lastToken = stack.topLastChild();
if(top.type === "Lookup") {
stack.replaceTopAndPush({
type: "Call",
method: convertToAtLookup(top)
});

// Nested Call
// foo()()
} else if (lastToken && lastToken.type === "Call") {
stack.replaceTopAndPush({
type: "Call",
method: lastToken
});
} else {
throw new Error("Unable to understand expression "+tokens.join(''));
}
Expand Down
82 changes: 82 additions & 0 deletions test/expression-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,18 @@ test("expression.ast - root hash expressions work", function(){

});

test("expression.ast - nested call expressions", function(){
var ast = expression.ast("foo()()");

deepEqual(ast, {
type: "Call",
method: {
type: "Call",
method: {type: "Lookup", key: "@foo"},
}
});

});

test("expression.ast - everything", function(){
var ast = expression.ast("helperA helperB(1, valueA, propA=~valueB propC=2, 1).zed() 'def' nested@prop outerPropA=helperC(2,valueB)");
Expand Down Expand Up @@ -223,6 +234,77 @@ test("expression.parse(str, {lookupRule: 'method', methodRule: 'call'})",
deepEqual(exprData.argExprs[0], hashArg, "correct hashes");
});

test("expression.parse nested Call expressions", function(){
QUnit.expect(7);

deepEqual(expression.parse("foo()()"),
new expression.Call(
new expression.Call(
new expression.Lookup('@foo'),
[],
{}
),
[],
{}
),
"Returned the correct expression"
);

var expr = new expression.Call(
new expression.Call(
new expression.Lookup('@bar'),
[ new expression.Literal(1) ],
{}
),
[ new expression.Literal(2) ],
{}
);
var compute = expr.value(
new Scope(
new SimpleMap({
bar: function(outter) {
equal(outter, 1, "Outter called with correct value");

return function (inner) {
equal(inner, 2, "Inner called with correct value");

return 'Inner!';
};
}
})
)
);
equal(compute.get(), "Inner!", "Got the inner value");

expr = new expression.Call(
new expression.Call(
new expression.Lookup('@foobar'),
[ new expression.Lookup('fname') ],
{}
),
[ new expression.Lookup('lname') ],
{}
);
compute = expr.value(
new Scope(
new SimpleMap({
foobar: function(outter) {
equal(outter, 'Matt', "Outter called with correct value");

return function (inner) {
equal(inner, 'Chaffe', "Inner called with correct value");

return 'Inner!';
};
},
fname: 'Matt',
lname: 'Chaffe'
})
)
);
equal(compute.get(), "Inner!", "Got the inner value");
});

test("numeric expression.Literal", function(){
var exprData = expression.parse("3");

Expand Down

0 comments on commit 911ed9c

Please sign in to comment.