simpler vm
This commit is contained in:
@@ -70,31 +70,33 @@ func (ao *Array) Less(i, j int) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (ao *Array) Add(other Object) (Object, error) {
|
||||
if other.Type() != ao.Type() {
|
||||
return nil, NewBinaryOpError(ao, other, "+")
|
||||
func (a *Array) Add(other Object) (Object, error) {
|
||||
switch obj := other.(type) {
|
||||
case *Array:
|
||||
var elements []Object
|
||||
elements = append(elements, a.Elements...)
|
||||
elements = append(elements, obj.Elements...)
|
||||
|
||||
return &Array{Elements: elements}, nil
|
||||
default:
|
||||
return nil, NewBinaryOpError(a, other, "+")
|
||||
}
|
||||
|
||||
var elements []Object
|
||||
elements = append(elements, ao.Elements...)
|
||||
elements = append(elements, other.(*Array).Elements...)
|
||||
|
||||
return &Array{Elements: elements}, nil
|
||||
|
||||
}
|
||||
|
||||
func (ao *Array) Mul(other Object) (Object, error) {
|
||||
if other.Type() != IntegerType {
|
||||
return nil, NewBinaryOpError(ao, other, "*")
|
||||
func (a *Array) Mul(other Object) (Object, error) {
|
||||
switch obj := other.(type) {
|
||||
case Integer:
|
||||
var elements []Object
|
||||
N := int(obj.Value)
|
||||
for i := 0; i < N; i++ {
|
||||
elements = append(elements, a.Elements...)
|
||||
}
|
||||
|
||||
return &Array{Elements: elements}, nil
|
||||
default:
|
||||
return nil, NewBinaryOpError(a, other, "*")
|
||||
}
|
||||
|
||||
var elements []Object
|
||||
N := int(other.(Integer).Value)
|
||||
for i := 0; i < N; i++ {
|
||||
elements = append(elements, ao.Elements...)
|
||||
}
|
||||
|
||||
return &Array{Elements: elements}, nil
|
||||
}
|
||||
|
||||
func (ao *Array) Get(index Object) (Object, error) {
|
||||
|
||||
Reference in New Issue
Block a user