Add set index in list, and update lib+examples+tests
This commit is contained in:
parent
09ac16a42d
commit
3bccc227ba
12 changed files with 117 additions and 60 deletions
34
core/vm.go
34
core/vm.go
|
|
@ -152,10 +152,14 @@ const (
|
|||
// 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
|
||||
// is the index. [..., container, index] -> [..., item]. Produces a new string with only the character
|
||||
// at the indexed position
|
||||
InstructionIndexString
|
||||
|
||||
// InstructionSetIndexList set the item at a given index in a list.
|
||||
// [..., item, container, index] -> [..., item]
|
||||
InstructionSetIndexList
|
||||
|
||||
// InstructionBreakpoint for debugging purposes
|
||||
InstructionBreakpoint
|
||||
)
|
||||
|
|
@ -638,7 +642,7 @@ var DefaultGlobals = map[string]Value{
|
|||
n, _ := v.Number.Float64()
|
||||
return &FloatValue{n}, nil
|
||||
case *FloatValue:
|
||||
return v.Clone(), nil
|
||||
return v.Copy(), nil
|
||||
case *StringValue:
|
||||
num, err := strconv.ParseFloat(v.Text, FloatSize)
|
||||
if err != nil {
|
||||
|
|
@ -978,7 +982,7 @@ func (vm *VM) Next() bool {
|
|||
vm.Stack.Push(v)
|
||||
|
||||
case InstructionSetLocal:
|
||||
value := vm.Stack.Peek().Clone()
|
||||
value := vm.Stack.Peek().Copy()
|
||||
name := vm.GetConstant(vm.NextByte()).(*StringValue).Text
|
||||
|
||||
vm.setVar(name, value)
|
||||
|
|
@ -986,7 +990,7 @@ func (vm *VM) Next() bool {
|
|||
case InstructionDeclareLocal:
|
||||
vm.addVar(
|
||||
vm.GetConstant(vm.NextByte()).(*StringValue).Text,
|
||||
vm.Stack.Peek().Clone(),
|
||||
vm.Stack.Peek().Copy(),
|
||||
)
|
||||
|
||||
case InstructionGetGlobal:
|
||||
|
|
@ -1070,7 +1074,7 @@ func (vm *VM) Next() bool {
|
|||
vm.Stack.Push(r, l)
|
||||
|
||||
case InstructionDuplicate:
|
||||
vm.Stack.Push(vm.Stack.Peek().Clone())
|
||||
vm.Stack.Push(vm.Stack.Peek().Copy())
|
||||
|
||||
case InstructionAccessProperty:
|
||||
source := vm.Stack.Pop()
|
||||
|
|
@ -1112,7 +1116,7 @@ func (vm *VM) Next() bool {
|
|||
vm.error(fmt.Sprintf("index %d out of bounds", n))
|
||||
}
|
||||
|
||||
vm.Stack.Push(l.Items[n].Clone())
|
||||
vm.Stack.Push(l.Items[n].Copy())
|
||||
|
||||
case InstructionIndexTuple:
|
||||
i := vm.Stack.Pop().(*IntegerValue)
|
||||
|
|
@ -1124,7 +1128,7 @@ func (vm *VM) Next() bool {
|
|||
vm.error(fmt.Sprintf("index %d out of bounds", n))
|
||||
}
|
||||
|
||||
vm.Stack.Push(t.Items[n].Clone())
|
||||
vm.Stack.Push(t.Items[n].Copy())
|
||||
|
||||
case InstructionIndexString:
|
||||
i := vm.Stack.Pop().(*IntegerValue)
|
||||
|
|
@ -1138,6 +1142,18 @@ func (vm *VM) Next() bool {
|
|||
|
||||
vm.Stack.Push(&StringValue{string(s.Text[n])})
|
||||
|
||||
case InstructionSetIndexList:
|
||||
n := vm.Stack.Pop().(*IntegerValue)
|
||||
l := vm.Stack.Pop().(*ListValue)
|
||||
|
||||
i := n.Number.Int64()
|
||||
|
||||
if i < 0 || int64(len(l.Items)) <= i {
|
||||
vm.error(fmt.Sprintf("index %d out of bounds", i))
|
||||
}
|
||||
|
||||
l.Items[i] = vm.Stack.Peek().Copy()
|
||||
|
||||
case InstructionBreakpoint:
|
||||
/*
|
||||
// I'm keeping this
|
||||
|
|
@ -1284,7 +1300,7 @@ func (vm *VM) HasNext() bool {
|
|||
}
|
||||
|
||||
func (vm *VM) GetConstant(id Bytecode) Value {
|
||||
return vm.chunk.Constants[id].Clone()
|
||||
return vm.chunk.Constants[id].Copy()
|
||||
}
|
||||
|
||||
func (vm *VM) ReadConstant() Value {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue