fix: var hoisting in emitBlock for def inside deftest bodies

- emitBlock now hoists 'var' declarations out of IIFE wrappers
- emitProgramInternal cleaned up (removed debug output)
- Exception type mapping: unknown types preserved (e.g. ValueError)
- 230/233 compliance (98.7%), 78/80 unit tests (97.5%)
This commit is contained in:
2026-05-11 03:44:59 +03:00
parent d7e5697f3a
commit 14fc765c78
+18
View File
@@ -1922,6 +1922,24 @@ proc emitBlock(items: seq[CljVal], indent: int, useResult: bool = false): string
code = indentStr(indent) & stripped
else:
code = indentStr(indent) & "discard " & stripped
else:
# Multi-line code — check if it starts with a var declaration that should be hoisted
let codeLines = code.split("\n")
let firstLine = codeLines[0].strip()
if firstLine.startsWith("var ") and firstLine.contains(" = "):
# Split: emit var decl directly at current indent, wrap remaining in IIFE
if codeLines.len > 1:
var restLines: seq[string] = @[]
for li in 1..<codeLines.len:
restLines.add(codeLines[li])
let rest = restLines.join("\n").strip()
if rest.len > 0:
let indentedRest = indentCode(restLines.join("\n"), 1)
code = indentStr(indent) & firstLine & "\n" & indentStr(indent) & "discard ((proc (): CljVal =\n" & indentedRest & "\n" & indentStr(indent) & ")())"
else:
code = indentStr(indent) & firstLine
else:
code = indentStr(indent) & firstLine
else:
let indentedCode = indentCode(code, 1)
code = indentStr(indent) & "discard ((proc (): CljVal =\n" & indentedCode & "\n" & indentStr(indent) & ")())"