hash
This commit is contained in:
35
vm/vm.go
35
vm/vm.go
@@ -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)
|
||||
|
||||
|
||||
@@ -79,6 +79,29 @@ func testExpectedObject(t *testing.T, expected interface{}, actual object.Object
|
||||
}
|
||||
}
|
||||
|
||||
case map[object.HashKey]int64:
|
||||
hash, ok := actual.(*object.Hash)
|
||||
if !ok {
|
||||
t.Errorf("object not Hash: %T (%+v)", actual, actual)
|
||||
}
|
||||
|
||||
if len(hash.Pairs) != len(expected) {
|
||||
t.Errorf("wrong num of Pairs. want=%d, got=%d", len(expected), len(hash.Pairs))
|
||||
return
|
||||
}
|
||||
|
||||
for expectedKey, expectedValue := range expected {
|
||||
pair, ok := hash.Pairs[expectedKey]
|
||||
if !ok {
|
||||
t.Errorf("no pair for given key in Pairs")
|
||||
}
|
||||
|
||||
err := testIntegerObject(expectedValue, pair.Value)
|
||||
if err != nil {
|
||||
t.Errorf("testIntgerObject failed: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
case *object.Null:
|
||||
if actual != Null {
|
||||
t.Errorf("object is not Null: %T (%+v)", actual, actual)
|
||||
@@ -232,3 +255,27 @@ func TestArrayLiterals(t *testing.T) {
|
||||
|
||||
runVmTests(t, tests)
|
||||
}
|
||||
|
||||
func TestHashLiterals(t *testing.T) {
|
||||
tests := []vmTestCase{
|
||||
{
|
||||
"{}", map[object.HashKey]int64{},
|
||||
},
|
||||
{
|
||||
"{1: 2, 2: 3}",
|
||||
map[object.HashKey]int64{
|
||||
(&object.Integer{Value: 1}).HashKey(): 2,
|
||||
(&object.Integer{Value: 2}).HashKey(): 3,
|
||||
},
|
||||
},
|
||||
{
|
||||
"{1 + 1: 2 * 2, 3 + 3: 4 * 4}",
|
||||
map[object.HashKey]int64{
|
||||
(&object.Integer{Value: 2}).HashKey(): 4,
|
||||
(&object.Integer{Value: 6}).HashKey(): 16,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
runVmTests(t, tests)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user