Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 47 additions & 22 deletions src/simplify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions test/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down