Added overloaded support for array * int
This commit is contained in:
19
vm/vm.go
19
vm/vm.go
@@ -527,6 +527,25 @@ func (vm *VM) executeBinaryOperation(op code.Opcode) error {
|
||||
|
||||
switch {
|
||||
|
||||
// [1] * 3
|
||||
case left.Type() == object.ARRAY_OBJ && right.Type() == object.INTEGER_OBJ:
|
||||
leftVal := left.(*object.Array).Elements
|
||||
rightVal := int(right.(*object.Integer).Value)
|
||||
elements := leftVal
|
||||
for i := rightVal; i > 1; i-- {
|
||||
elements = append(elements, leftVal...)
|
||||
}
|
||||
return vm.push(&object.Array{Elements: elements})
|
||||
// 3 * [1]
|
||||
case left.Type() == object.INTEGER_OBJ && right.Type() == object.ARRAY_OBJ:
|
||||
leftVal := int(left.(*object.Integer).Value)
|
||||
rightVal := right.(*object.Array).Elements
|
||||
elements := rightVal
|
||||
for i := leftVal; i > 1; i-- {
|
||||
elements = append(elements, rightVal...)
|
||||
}
|
||||
return vm.push(&object.Array{Elements: elements})
|
||||
|
||||
// " " * 4
|
||||
case left.Type() == object.STRING_OBJ && right.Type() == object.INTEGER_OBJ:
|
||||
leftVal := left.(*object.String).Value
|
||||
|
||||
Reference in New Issue
Block a user