fix if result typing + fix indexing

This commit is contained in:
Neemek 2026-07-13 21:29:57 +02:00
parent 0bc8b2ef55
commit 7cb2be8415
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E
2 changed files with 38 additions and 15 deletions

View file

@ -143,6 +143,10 @@ const (
// InstructionIndexTuple index into a tuple. The lower item is the container, and the top item
// is the index. [..., container, index] -> [..., item]
InstructionIndexTuple
// InstructionIndexString index into a string. The lower item is the container, and the top item
// is the index. [..., container, index] -> [..., item]. Produces a new string with the character
// at the position
InstructionIndexString
// InstructionBreakpoint for debugging purposes
InstructionBreakpoint
@ -1070,7 +1074,7 @@ func (vm *VM) Next() bool {
n := int(i.Number.Int64())
if 0 < n || n >= 10 {
if n < 0 || len(l.Items) <= n {
vm.error(fmt.Sprintf("index %d out of bounds", n))
}
@ -1078,15 +1082,27 @@ func (vm *VM) Next() bool {
case InstructionIndexTuple:
i := vm.Stack.Pop().(*IntegerValue)
l := vm.Stack.Pop().(*TupleValue)
t := vm.Stack.Pop().(*TupleValue)
n := int(i.Number.Int64())
if 0 < n || n >= len(l.Items) {
if n < 0 || len(t.Items) <= n {
vm.error(fmt.Sprintf("index %d out of bounds", n))
}
vm.Stack.Push(l.Items[n].Clone())
vm.Stack.Push(t.Items[n].Clone())
case InstructionIndexString:
i := vm.Stack.Pop().(*IntegerValue)
s := vm.Stack.Pop().(*StringValue)
n := int(i.Number.Int64())
if n < 0 || len(s.Text) <= n {
vm.error(fmt.Sprintf("index %d out of bounds", n))
}
vm.Stack.Push(&StringValue{string(s.Text[n])})
case InstructionBreakpoint:
/*