string like array access
Some checks failed
Build / build (push) Failing after 1m25s
Test / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-15 14:14:34 -04:00
parent 997f0865f4
commit d7938e59e4
4 changed files with 83 additions and 0 deletions

View File

@@ -376,6 +376,8 @@ func unwrapReturnValue(obj object.Object) object.Object {
func evalIndexExpression(left, index object.Object) object.Object {
switch {
case left.Type() == object.STRING_OBJ && index.Type() == object.INTEGER_OBJ:
return evalStringIndexExpression(left, index)
case left.Type() == object.ARRAY_OBJ && index.Type() == object.INTEGER_OBJ:
return evalArrayIndexExpression(left, index)
case left.Type() == object.HASH_OBJ:
@@ -385,6 +387,18 @@ func evalIndexExpression(left, index object.Object) object.Object {
}
}
func evalStringIndexExpression(str, index object.Object) object.Object {
stringObject := str.(*object.String)
idx := index.(*object.Integer).Value
max := int64((len(stringObject.Value)) - 1)
if idx < 0 || idx > max {
return &object.String{Value: ""}
}
return &object.String{Value: string(stringObject.Value[idx])}
}
func evalHashIndexExpression(hash, index object.Object) object.Object {
hashObject := hash.(*object.Hash)