Skip to content

Commit

Permalink
Codegen: while-expr recalculate condition and the end of the loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Mar 12, 2024
1 parent 5638f02 commit 11b692b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/codegen/js/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,13 @@ export const emitOperand = (operand: Operand, module: Module, ctx: Context): Emi
return { emit: jsError('if-let'), resultVar }
case 'while-expr': {
const { emit: cEmit, resultVar: cVar } = emitExpr(operand.condition, module, ctx)
const block = emitBlock(operand.block, module, ctx)
return { emit: emitLines([cEmit, `while (${extractValue(cVar)}) ${block}`]), resultVar }
const { emit: cEndEmit, resultVar: cEndVar } = emitExpr(operand.condition, module, ctx)
const block = emitLines([
...emitBlockStatements(operand.block, module, ctx),
cEndEmit,
`${cVar} = ${cEndVar}`
])
return { emit: emitLines([cEmit, `while (${extractValue(cVar)}) {\n${indent(block)}\n}`]), resultVar }
}
case 'for-expr': {
const expr = emitExpr(operand.expr, module, ctx)
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const jsString = (str: string): string => {
}

export const jsVariable = (name: string, emit?: string, pub = false): string => {
const assign = emit !== undefined ? `const ${name} = ${emit};` : `let ${name}`
const assign = `let ${name}${emit !== undefined ? ` = ${emit}` : ''}`
return `${pub ? 'export ' : ''}${assign}`
}

Expand Down

0 comments on commit 11b692b

Please sign in to comment.