Skip to content

Commit

Permalink
update fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
osheow committed May 2, 2024
1 parent 7293b88 commit 88625e4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ export default function generate(program) {
const op = { '==': '===', '!=': '!==' }[e.op] ?? e.op
return `(${gen(e.left)} ${op} ${gen(e.right)})`
},
// NegationExpression(e) {
// const operand = gen(e.operand)
NegationExpression(e) {
const operand = gen(e.operand)
// // if (e.op === 'some') {
// // return operand
// // } else if (e.op === '#') {
// // return `${operand}.length`
// // } else if (e.op === 'random') {
// // return `((a=>a[~~(Math.random()*a.length)])(${operand}))`
// // }
// return `${e.op}${operand}`
// },
return `${e.op}${operand}`
},
SubscriptExpression(e) {
return `${gen(e.array)}[${gen(e.index)}]`
},
Expand Down
8 changes: 0 additions & 8 deletions test/optimizer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,9 @@ const tests = [
["optimizes assignments", core.program([assign(x, core.binary("+", x, 0)), returnX]), core.program([returnX])],
["optimizes assignments return null", core.program([assign(x, core.binary("+", x, 0)), return1p1]), core.program([return2])],
["optimizes ifStatement with alternate", core.ifStatement(eq(x, 0), [return1p1], [return2]), core.ifStatement(eq(x, 0), [return1p1], [return2])],
["optimizes ifStatement with Boolean test and both consequent and alternate branches", core.ifStatement(true, [returnX], [return1p1]), [returnX]],
["optimizes shortIfStatement with Boolean test and both consequent and alternate branches", core.shortIfStatement(true, [returnX]), [returnX]],
["optimizes whileStatement no-op", core.whileStatement(true, []), core.whileStatement(true, [])],
["optimizes whileStatement with false test", core.whileStatement(false, [returnX]), []],
["optimizes ifStatement with boolean test, no alternate", core.ifStatement(true, [returnX], []), [returnX]],
["optimizes ifStatement alternate", core.ifStatement(eq(x, 0), [return1p1], [return2]), core.ifStatement(eq(x, 0), [return1p1], [return2])],
["optimizes ifStatement with boolean test and alternate", core.ifStatement(true, [returnX], [return1p1]), [returnX]],
["optimizes shortIfStatement with boolean test, no alternate", core.shortIfStatement(true, [returnX]), [returnX]],
["optimizes ifStatement with test not being a Boolean",
core.ifStatement(x, [returnX], [return1p1]), // `x` is not a Boolean
core.ifStatement(x, [returnX], [return1p1]) // No optimization, returns the original statement
Expand Down Expand Up @@ -110,9 +105,6 @@ const tests = [
core.forRangeStatement(x, 5, "..", 2, [returnX]),
[]
],
["short ifStatement with boolean test, no alternate", core.shortIfStatement(true, [returnX]), [returnX]],
["optimizes ifStatement with boolean test, no alternate", core.ifStatement(true, [returnX], []), [returnX]],
["optimizes short ifStatement with boolean test", core.shortIfStatement(true, [returnX]), [returnX]],
["optimizes short ifStatement with test not being a Boolean", core.shortIfStatement(x, [returnX]), core.shortIfStatement(x, [returnX])],
]

Expand Down

0 comments on commit 88625e4

Please sign in to comment.