string like array access
This commit is contained in:
14
vm/vm.go
14
vm/vm.go
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user