Refine variable resolution logic, update type alias syntax

This commit is contained in:
Sergey Chernov 2026-02-17 08:17:57 +03:00
parent b6851b7098
commit df19524177
4 changed files with 37 additions and 7 deletions

View File

@ -7063,9 +7063,24 @@ class BytecodeCompiler(
private fun isKnownClassReceiver(ref: ObjRef): Boolean { private fun isKnownClassReceiver(ref: ObjRef): Boolean {
return when (ref) { return when (ref) {
is LocalVarRef -> knownClassNames.contains(ref.name) && !knownObjectNames.contains(ref.name) is LocalVarRef -> {
is LocalSlotRef -> knownClassNames.contains(ref.name) && !knownObjectNames.contains(ref.name) val name = ref.name
is FastLocalVarRef -> knownClassNames.contains(ref.name) && !knownObjectNames.contains(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 else -> false
} }
} }

View File

@ -157,4 +157,19 @@ class StdlibTest {
""" """
) )
} }
@Test
fun testFilter3() = runTest {
eval("""
type Numeric = Int | Real
fun process<T: Numeric>(items: List<T>): List<T> {
items.filter { it > 0 }.map { it * it }
}
val data: List<Int> = [-2, -1, 0, 1, 2]
println("Processed: " + process(data))
""".trimIndent())
}
} }

View File

@ -50,7 +50,7 @@ fun HomePage() {
""".trimIndent(), """.trimIndent(),
""" """
// Generics and type aliases // Generics and type aliases
typealias Num = Int | Real type Num = Int | Real
class Box<out T: Num>(val value: T) { class Box<out T: Num>(val value: T) {
fun get(): T = value fun get(): T = value
@ -188,9 +188,9 @@ fun HomePage() {
Div({ classes("text-center") }) { Div({ classes("text-center") }) {
H1({ classes("display-5", "fw-bold", "mb-3") }) { Text("Welcome to Lyng") } H1({ classes("display-5", "fw-bold", "mb-3") }) { Text("Welcome to Lyng") }
P({ classes("lead", "text-muted", "mb-4") }) { 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() 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") }) { Div({ classes("d-flex", "justify-content-center", "gap-2", "flex-wrap", "mb-4") }) {
// Benefits pills // Benefits pills

View File

@ -51,7 +51,7 @@ fun TryLyngPage(route: String) {
initialCode ?: """ initialCode ?: """
// Welcome to Lyng! Modern scripting with strict types and generics. // Welcome to Lyng! Modern scripting with strict types and generics.
typealias Numeric = Int | Real type Numeric = Int | Real
fun process<T: Numeric>(items: List<T>): List<T> { fun process<T: Numeric>(items: List<T>): List<T> {
items.filter { it > 0 }.map { it * it } items.filter { it > 0 }.map { it * it }