fix: top level block, even isolated, is treated as a lambda too

This commit is contained in:
Sergey Chernov 2025-06-08 00:42:08 +04:00
parent c0cf190452
commit 4dc73b91c2
2 changed files with 7 additions and 4 deletions

View File

@ -47,7 +47,7 @@ One interesting way of using closure isolation is to keep state of the functions
>>> 0 >>> 0
>>> 1 >>> 1
>>> 2 >>> 2
>> void >>> void
Inner `counter` is not accessible from outside, no way; still it is kept Inner `counter` is not accessible from outside, no way; still it is kept
between calls in the closure, as inner function `doit`, returned from the between calls in the closure, as inner function `doit`, returned from the

View File

@ -16,14 +16,14 @@ class Compiler(
private fun parseScript(start: Pos, tokens: CompilerContext): Script { private fun parseScript(start: Pos, tokens: CompilerContext): Script {
val statements = mutableListOf<Statement>() val statements = mutableListOf<Statement>()
while (parseStatement(tokens)?.also { while (parseStatement(tokens,braceMeansLambda = true)?.also {
statements += it statements += it
} != null) {/**/ } != null) {/**/
} }
return Script(start, statements) return Script(start, statements)
} }
private fun parseStatement(cc: CompilerContext): Statement? { private fun parseStatement(cc: CompilerContext,braceMeansLambda: Boolean = false): Statement? {
while (true) { while (true) {
val t = cc.next() val t = cc.next()
return when (t.type) { return when (t.type) {
@ -49,6 +49,9 @@ class Compiler(
Token.Type.LBRACE -> { Token.Type.LBRACE -> {
cc.previous() cc.previous()
if( braceMeansLambda )
parseExpression(cc)
else
parseBlock(cc) parseBlock(cc)
} }