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

@@ -297,6 +297,8 @@ func (vm *VM) Run() error {
func (vm *VM) executeIndexExpressions(left, index object.Object) error {
switch {
case left.Type() == object.STRING_OBJ && index.Type() == object.INTEGER_OBJ:
return vm.executeStringIndex(left, index)
case left.Type() == object.ARRAY_OBJ && index.Type() == object.INTEGER_OBJ:
return vm.executeArrayIndex(left, index)
case left.Type() == object.HASH_OBJ:
@@ -567,6 +569,18 @@ func (vm *VM) pushClosure(constIndex, numFree int) error {
return vm.push(closure)
}
func (vm *VM) executeStringIndex(str, index object.Object) error {
stringObject := str.(*object.String)
i := index.(*object.Integer).Value
max := int64(len(stringObject.Value) - 1)
if i < 0 || i > max {
return vm.push(&object.String{Value: ""})
}
return vm.push(&object.String{Value: string(stringObject.Value[i])})
}
func nativeBoolToBooleanObject(input bool) *object.Boolean {
if input {
return True