fix: correct local variable storage and remove premature local slot allocation in vm
The compiler's storeVariable function was incorrectly emitting CODE_STORE_LOCAL for all variables, including locals that already have their value in the correct stack slot. This change renames storeVariable to defineVariable and adds logic to only emit CODE_STORE_GLOBAL for globals, while for locals it emits CODE_DUP to preserve the value through the subsequent POP. Additionally, the vm's callFunction no longer pre-allocates empty slots for locals, as they are now placed on the stack during compilation. New test files verify duplicate local/parameter detection and correct local variable ordering in block scopes.
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
bar {
|
||||
var a = "value"
|
||||
var a = "other" // expect error
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
bar(arg,
|
||||
arg) { // expect error
|
||||
"body"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
bar(a) {
|
||||
var a = "oops" // expect error
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
class Foo {
|
||||
bar {
|
||||
var a = "a"
|
||||
io.write(a) // expect: a
|
||||
var b = a + " b"
|
||||
io.write(b) // expect: a b
|
||||
var c = a + " c"
|
||||
io.write(c) // expect: a c
|
||||
var d = b + " d"
|
||||
io.write(d) // expect: a b d
|
||||
}
|
||||
}
|
||||
|
||||
Foo.new.bar
|
||||
Reference in New Issue
Block a user