functions now capture variables :D

This commit is contained in:
Neemek 2026-07-09 23:21:41 +02:00
parent 43e450c207
commit d315a53fbd
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E
5 changed files with 49 additions and 11 deletions

View file

@ -698,7 +698,13 @@ func (vm *VM) Next() bool {
vm.stack.Pop()
case InstructionConstant:
vm.stack.Push(vm.ReadConstant())
c := vm.ReadConstant()
if c, ok := c.(*FunctionValue); ok {
c.Scope = vm.scope
}
vm.stack.Push(c)
case InstructionAddFloat:
r := vm.stack.Pop().(*FloatValue).Number
@ -840,6 +846,7 @@ func (vm *VM) Next() bool {
scope: vm.scope,
})
vm.scope = f.Scope
vm.descend()
for i := len(f.Params) - 1; i >= 0; i-- {
@ -993,6 +1000,15 @@ func (vm *VM) Next() bool {
vm.stack.Push(member)
case InstructionBreakpoint:
/*
// I'm keeping this
s := vm.scope
log.Printf("breakpoint %d", vm.ip)
for s != nil {
log.Printf("%s", s.current)
s = s.parent
}
*/
vm.stack.Push(&NilValue{})
default:
@ -1122,7 +1138,7 @@ func (vm *VM) HasNext() bool {
}
func (vm *VM) GetConstant(id Bytecode) Value {
return vm.chunk.Constants[id]
return vm.chunk.Constants[id].Clone()
}
func (vm *VM) ReadConstant() Value {