|
|
@@ -298,6 +298,8 @@ func evalIndexExpression(left, index object.Object) object.Object {
|
|
|
switch {
|
|
|
case left.Type() == object.ArrayObj && index.Type() == object.IntegerObj:
|
|
|
return evalArrayIndexExpression(left, index)
|
|
|
+ case left.Type() == object.HashObj:
|
|
|
+ return evalHashIndexExpression(left, index)
|
|
|
default:
|
|
|
return newError("index operator not supported: %s", left.Type())
|
|
|
}
|
|
|
@@ -313,6 +315,20 @@ func evalArrayIndexExpression(array, index object.Object) object.Object {
|
|
|
}
|
|
|
return arrayObj.Elements[idx]
|
|
|
}
|
|
|
+func evalHashIndexExpression(hash, index object.Object) object.Object {
|
|
|
+ hashObj := hash.(*object.Hash)
|
|
|
+
|
|
|
+ key, ok := index.(object.Hashtable)
|
|
|
+ if !ok {
|
|
|
+ return newError("unusable as hash key: %s", index.Type())
|
|
|
+ }
|
|
|
+
|
|
|
+ pair, ok := hashObj.Pairs[key.HashKey()]
|
|
|
+ if !ok {
|
|
|
+ return NULL
|
|
|
+ }
|
|
|
+ return pair.Value
|
|
|
+}
|
|
|
func evalHashLiteral(node *ast.HashLiteral, env *object.Environment) object.Object {
|
|
|
pairs := make(map[object.HashKey]object.HashPair)
|
|
|
|