diff --git a/src/simplify.js b/src/simplify.js index 7257a52..39a4acd 100644 --- a/src/simplify.js +++ b/src/simplify.js @@ -29,29 +29,54 @@ export default function simplify(tokens, unaryOps, binaryOps, ternaryOps, values f = ternaryOps[item.value]; item = new Instruction(INUMBER, f(n1.value, n2.value, n3.value)); nstack.push(item); + } else if (type === IVAR && values.hasOwnProperty(item.value)) { + item = new Instruction(INUMBER, values[item.value]); + nstack.push(item); + } else if (type === IOP2 && nstack.length > 1) { + n2 = nstack.pop(); + n1 = nstack.pop(); + f = binaryOps[item.value]; + item = new Instruction(INUMBER, f(n1.value, n2.value)); + nstack.push(item); + } else if (type === IOP3 && nstack.length > 2) { + n3 = nstack.pop(); + n2 = nstack.pop(); + n1 = nstack.pop(); + if (item.value === '?') { + nstack.push(n1.value ? n2.value : n3.value); + } else { + f = ternaryOps[item.value]; + item = new Instruction(INUMBER, f(n1.value, n2.value, n3.value)); + nstack.push(item); + } + } else if (type === IOP1 && nstack.length > 0) { + n1 = nstack.pop(); + f = unaryOps[item.value]; + item = new Instruction(INUMBER, f(n1.value)); + nstack.push(item); + } else if (type === IEXPR) { + var simplified = simplify(item.value, unaryOps, binaryOps, ternaryOps, values); + if(simplified.length === 1 && simplified[0].type === INUMBER) { + nstack.push(simplified[0]); } - } else if (type === IOP1 && nstack.length > 0) { - n1 = nstack.pop(); - f = unaryOps[item.value]; - item = new Instruction(INUMBER, f(n1.value)); - nstack.push(item); - } else if (type === IEXPR) { - while (nstack.length > 0) { - newexpression.push(nstack.shift()); - } - newexpression.push(new Instruction(IEXPR, simplify(item.value, unaryOps, binaryOps, ternaryOps, values))); - } else if (type === IMEMBER && nstack.length > 0) { - n1 = nstack.pop(); - nstack.push(new Instruction(INUMBER, n1.value[item.value])); - } else { - while (nstack.length > 0) { - newexpression.push(nstack.shift()); + else { + while (nstack.length > 0) { + newexpression.push(nstack.shift()); + } + newexpression.push(new Instruction(IEXPR, simplified)); + } + } else if (type === IMEMBER && nstack.length > 0) { + n1 = nstack.pop(); + nstack.push(new Instruction(INUMBER, n1.value[item.value])); + } else { + while (nstack.length > 0) { + newexpression.push(nstack.shift()); + } + newexpression.push(item); } - newexpression.push(item); } + while (nstack.length > 0) { + newexpression.push(nstack.shift()); + } + return newexpression; } - while (nstack.length > 0) { - newexpression.push(nstack.shift()); - } - return newexpression; -} diff --git a/test/expression.js b/test/expression.js index 887c72d..d88aaa7 100644 --- a/test/expression.js +++ b/test/expression.js @@ -210,11 +210,11 @@ describe('Expression', function () { }); it('x ? (y + 1) : z', function () { - assert.strictEqual(Parser.parse('x ? (y + 1) : z').simplify({ y: 2 }).toString(), '(x ? (3) : (z))'); + assert.strictEqual(Parser.parse('x ? (y + 1) : z').simplify({ y: 2 }).toString(), '(x ? 3 : (z))'); }); it('x ? y : (z * 4)', function () { - assert.strictEqual(Parser.parse('x ? y : (z * 4)').simplify({ z: 3 }).toString(), '(x ? (y) : (12))'); + assert.strictEqual(Parser.parse('x ? y : (z * 4)').simplify({ z: 3 }).toString(), '(x ? (y) : 12)'); }); it('x = 2*x', function () {