Skip to content

Commit

Permalink
feat: adds hash access by key(index)
Browse files Browse the repository at this point in the history
Signed-off-by: maxwellgithinji <[email protected]>
  • Loading branch information
maxwellgithinji committed Feb 8, 2024
1 parent 3955fed commit 6305082
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ func evalIndexExpression(left, index object.Object) object.Object {

return evalArrayIndexExpression(left, index)

case left.Type() == object.HASH_OBJECT:
return evalHashIndexExpression(left, index)

default:
return newError("index operator not supported: %s", left.Type())
}
Expand Down Expand Up @@ -452,3 +455,22 @@ func evalHashLiteral(node *ast.HashLiteral, env *object.Environment) object.Obje

return &object.Hash{Pairs: pairs}
}

// evalHashIndexExpression evaluates indices for a hash expression
func evalHashIndexExpression(hash, index object.Object) object.Object {
hashObject := hash.(*object.Hash)

key, ok := index.(object.Hashable)
if !ok {
return newError("unusable as hash key: %s", index.Type())
}

pair, ok := hashObject.Pairs[key.HashKey()]

if !ok {
return NULL
}

return pair.Value

}

0 comments on commit 6305082

Please sign in to comment.