hash
Some checks failed
Build / build (push) Failing after 1m13s
Test / build (push) Failing after 2m1s

This commit is contained in:
Chuck Smith
2024-02-26 16:59:37 -05:00
parent 8721665bc1
commit 0a1201f1bc
5 changed files with 152 additions and 0 deletions

View File

@@ -145,12 +145,47 @@ func (vm *VM) Run() error {
return err
}
case code.OpHash:
numElements := int(code.ReadUint16(vm.instructions[ip+1:]))
ip += 2
hash, err := vm.buildHash(vm.sp-numElements, vm.sp)
if err != nil {
return err
}
vm.sp = vm.sp - numElements
err = vm.push(hash)
if err != nil {
return err
}
}
}
return nil
}
func (vm *VM) buildHash(startIndex, endIndex int) (object.Object, error) {
hashedPairs := make(map[object.HashKey]object.HashPair)
for i := startIndex; i < endIndex; i += 2 {
key := vm.stack[i]
value := vm.stack[i+1]
pair := object.HashPair{Key: key, Value: value}
hashKey, ok := key.(object.Hashable)
if !ok {
return nil, fmt.Errorf("unusable as hash key: %s", key.Type())
}
hashedPairs[hashKey.HashKey()] = pair
}
return &object.Hash{Pairs: hashedPairs}, nil
}
func (vm *VM) buildArray(startIndex, endIndex int) object.Object {
elements := make([]object.Object, endIndex-startIndex)