fixed mutability
This commit is contained in:
21
vm/vm.go
21
vm/vm.go
@@ -157,22 +157,23 @@ func (vm *VM) Run() error {
|
||||
vm.currentFrame().ip += 2
|
||||
|
||||
ref := vm.pop()
|
||||
if mutable, ok := ref.(object.Mutable); ok {
|
||||
vm.globals[globalIndex] = mutable.Clone()
|
||||
if immutable, ok := ref.(object.Immutable); ok {
|
||||
vm.globals[globalIndex] = immutable.Clone()
|
||||
} else {
|
||||
vm.globals[globalIndex] = ref
|
||||
}
|
||||
|
||||
case code.OpAssign:
|
||||
ident := vm.pop()
|
||||
val := vm.pop()
|
||||
case code.OpAssignGlobal:
|
||||
globalIndex := code.ReadUint16(ins[ip+1:])
|
||||
vm.currentFrame().ip += 2
|
||||
vm.globals[globalIndex] = vm.pop()
|
||||
|
||||
obj, ok := ident.(object.Mutable)
|
||||
if !ok {
|
||||
return fmt.Errorf("cannot assign to %s", ident.Type())
|
||||
}
|
||||
case code.OpAssignLocal:
|
||||
localIndex := code.ReadUint8(ins[ip+1:])
|
||||
vm.currentFrame().ip += 1
|
||||
|
||||
obj.Set(val)
|
||||
frame := vm.currentFrame()
|
||||
vm.stack[frame.basePointer+int(localIndex)] = vm.pop()
|
||||
|
||||
case code.OpGetGlobal:
|
||||
globalIndex := code.ReadUint16(ins[ip+1:])
|
||||
|
||||
Reference in New Issue
Block a user