Skip to content

Commit

Permalink
fixed comma parsing in functions with complex arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
zserge committed Jun 10, 2016
1 parent f968803 commit 7b23a47
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
32 changes: 17 additions & 15 deletions expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,29 +279,31 @@ function parse(s, vars, funcs) {
}
parenNext = parenForbidden;
} else if (!isNaN(token)) {
// TODO check if parse is correct to avoid 2.3.3
var n = parseFloat(token);
es.push(constExpr(n));
parenNext = parenForbidden;
} else if (funcs[token]) {
os.push(token);
parenNext = parenExpected;
} else if (ops[token]) {
if (token == ',' && os.length > 0 && os[os.length-1] == '{') {
as[as.length-1].args.push(es.pop());
} else {
var op = ops[token];
var o2 = os[os.length-1];
while (ops[o2] !== 0 && ((isLeftAssoc(op) && op >= ops[o2]) || op > ops[o2])) {
var expr = bind(o2, funcs, es);
if (!expr) {
return;
}
es.push(expr);
os.pop();
o2 = os[os.length-1];
var op = ops[token];
var o2 = os[os.length-1];
for (;;) {
if (token == ',' && os.length > 0 && os[os.length-1] == '{') {
as[as.length-1].args.push(es.pop());
break;
}
if (!(ops[o2] !== 0 && ((isLeftAssoc(op) && op >= ops[o2]) || op > ops[o2]))) {
os.push(token);
break;
}
var expr = bind(o2, funcs, es);
if (!expr) {
return;
}
os.push(token);
es.push(expr);
os.pop();
o2 = os[os.length-1];
}
} else if (token.match(/^\D/)) {
// Variable
Expand Down
4 changes: 4 additions & 0 deletions expr_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ function testFunc() {
'next(next(4))': 6,
'next((2,4))': 5,
'next(1, (2,4))': 2,
'next(3*2)': 7,
'next(3*2, 2)': 7,
'next(3*2, (2,4))': 7,
'next((2,4),1)': 5,
'2+add3(3, add3(1, 2, 3), 9)': 20,

Expand All @@ -92,6 +95,7 @@ function testFunc() {
'nop(1)': 0,
'nop((1))': 0,
'1,nop()': 0,
'1,nop(),2': 2,
}, {}, {
'add3': function(args) { return args[0]()+args[1]()+args[2](); },
'nop': function() {return 0;},
Expand Down

0 comments on commit 7b23a47

Please sign in to comment.