From df19524177db541a9c6967be7c8d191e10a283bf Mon Sep 17 00:00:00 2001 From: sergeych Date: Tue, 17 Feb 2026 08:17:57 +0300 Subject: [PATCH] Refine variable resolution logic, update type alias syntax --- .../lyng/bytecode/BytecodeCompiler.kt | 21 ++++++++++++++++--- lynglib/src/commonTest/kotlin/StdlibTest.kt | 15 +++++++++++++ site/src/jsMain/kotlin/HomePage.kt | 6 +++--- site/src/jsMain/kotlin/TryLyngPage.kt | 2 +- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/bytecode/BytecodeCompiler.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/bytecode/BytecodeCompiler.kt index a145b92..73d79e1 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/bytecode/BytecodeCompiler.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/bytecode/BytecodeCompiler.kt @@ -7063,9 +7063,24 @@ class BytecodeCompiler( private fun isKnownClassReceiver(ref: ObjRef): Boolean { return when (ref) { - is LocalVarRef -> knownClassNames.contains(ref.name) && !knownObjectNames.contains(ref.name) - is LocalSlotRef -> knownClassNames.contains(ref.name) && !knownObjectNames.contains(ref.name) - is FastLocalVarRef -> knownClassNames.contains(ref.name) && !knownObjectNames.contains(ref.name) + is LocalVarRef -> { + val name = ref.name + if (localSlotIndexByName.containsKey(name)) return false + if (localSlotInfoMap.values.any { it.name == name }) return false + knownClassNames.contains(name) && !knownObjectNames.contains(name) + } + is LocalSlotRef -> { + val name = ref.name + if (localSlotIndexByName.containsKey(name)) return false + if (localSlotInfoMap.values.any { it.name == name }) return false + knownClassNames.contains(name) && !knownObjectNames.contains(name) + } + is FastLocalVarRef -> { + val name = ref.name + if (localSlotIndexByName.containsKey(name)) return false + if (localSlotInfoMap.values.any { it.name == name }) return false + knownClassNames.contains(name) && !knownObjectNames.contains(name) + } else -> false } } diff --git a/lynglib/src/commonTest/kotlin/StdlibTest.kt b/lynglib/src/commonTest/kotlin/StdlibTest.kt index 5c2894d..9486762 100644 --- a/lynglib/src/commonTest/kotlin/StdlibTest.kt +++ b/lynglib/src/commonTest/kotlin/StdlibTest.kt @@ -157,4 +157,19 @@ class StdlibTest { """ ) } + + @Test + fun testFilter3() = runTest { + eval(""" + type Numeric = Int | Real + + fun process(items: List): List { + items.filter { it > 0 }.map { it * it } + } + + val data: List = [-2, -1, 0, 1, 2] + println("Processed: " + process(data)) + + """.trimIndent()) + } } diff --git a/site/src/jsMain/kotlin/HomePage.kt b/site/src/jsMain/kotlin/HomePage.kt index e67fcf4..aef031a 100644 --- a/site/src/jsMain/kotlin/HomePage.kt +++ b/site/src/jsMain/kotlin/HomePage.kt @@ -50,7 +50,7 @@ fun HomePage() { """.trimIndent(), """ // Generics and type aliases - typealias Num = Int | Real + type Num = Int | Real class Box(val value: T) { fun get(): T = value @@ -188,9 +188,9 @@ fun HomePage() { Div({ classes("text-center") }) { H1({ classes("display-5", "fw-bold", "mb-3") }) { Text("Welcome to Lyng") } P({ classes("lead", "text-muted", "mb-4") }) { - Text("A lightweight, expressive scripting language with strict static typing and functional power. ") + Text("A lightweight but extremely powerful and expressive scripting language with strict static typing and functional power. ") Br() - Text("Run it anywhere Kotlin runs — share logic across JS, JVM, and more.") + Text("Run it anywhere Kotlin runs — share logic across JS, JVM, Native and more.") } Div({ classes("d-flex", "justify-content-center", "gap-2", "flex-wrap", "mb-4") }) { // Benefits pills diff --git a/site/src/jsMain/kotlin/TryLyngPage.kt b/site/src/jsMain/kotlin/TryLyngPage.kt index 0912139..b333895 100644 --- a/site/src/jsMain/kotlin/TryLyngPage.kt +++ b/site/src/jsMain/kotlin/TryLyngPage.kt @@ -51,7 +51,7 @@ fun TryLyngPage(route: String) { initialCode ?: """ // Welcome to Lyng! Modern scripting with strict types and generics. - typealias Numeric = Int | Real + type Numeric = Int | Real fun process(items: List): List { items.filter { it > 0 }.map { it * it }