simpler vm
Some checks failed
Test / build (push) Waiting to run
Build / build (push) Successful in 11m8s
Publish Image / publish (push) Has been cancelled

This commit is contained in:
2024-04-01 17:21:55 -04:00
parent f9e6e164b0
commit 803f330e82
6 changed files with 246 additions and 317 deletions

View File

@@ -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) {