Skip to content

Commit

Permalink
4/7 gen tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattmart42 committed Apr 29, 2024
1 parent afb2994 commit 6883a78
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function returnStatement(expression) {
}

export function shortReturnStatement() {
return { kind: 'shortreturnStatement' }
return { kind: 'shortReturnStatement' }
}

export function ifStatement(test, consequent, alternate) {
Expand Down
15 changes: 11 additions & 4 deletions src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,22 @@ export default function generate(program) {
s.body.forEach(gen)
output.push("}")
},
// Conditional(e) {
// return `((${gen(e.test)}) ? (${gen(e.consequent)}) : (${gen(e.alternate)}))`
// },
Conditional(e) {
return `((${gen(e.test)}) ? (${gen(e.consequent)}) : (${gen(e.alternate)}))`
},
BinaryExpression(e) {
const op = { "==": "===", "!=": "!==" }[e.op] ?? e.op
return `(${gen(e.left)} ${op} ${gen(e.right)})`
},
NegationExpression(e) {
UnaryExpression(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})`
},
SubscriptExpression(e) {
Expand Down
5 changes: 2 additions & 3 deletions test/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const fixtures = [
`,
expected: dedent`
let x_1 = 21;
x_1 = x_1 + 1;
x_1 = x_1 - 1;
x_1 = (x_1 + 1);
x_1 = (x_1 - 1);
let y_2 = true;
`,
},
Expand Down Expand Up @@ -97,7 +97,6 @@ const fixtures = [
func g(): bool {
return false;
}
f(z, g());
`,
expected: dedent`
let z_1 = 0.5;
Expand Down

0 comments on commit 6883a78

Please sign in to comment.