type checking and error handling for builtins improved.
Some checks failed
Build / build (push) Successful in 11m16s
Test / build (push) Failing after 17m0s

This commit is contained in:
Chuck Smith
2024-03-25 16:18:08 -04:00
parent 1c99d2198b
commit 6d234099d1
52 changed files with 650 additions and 433 deletions

View File

@@ -1,28 +1,22 @@
package builtins
import "monkey/object"
import (
"monkey/object"
"monkey/typing"
)
// Push ...
func Push(args ...object.Object) object.Object {
if len(args) != 2 {
return newError("wrong number of arguments. got=%d, want=2",
len(args))
}
if args[0].Type() != object.ARRAY_OBJ {
return newError("argument to `push` must be array, got %s",
args[0].Type())
if err := typing.Check(
"push", args,
typing.ExactArgs(2),
typing.WithTypes(object.ARRAY_OBJ),
); err != nil {
return newError(err.Error())
}
arr := args[0].(*object.Array)
length := len(arr.Elements)
newElements := make([]object.Object, length+1)
copy(newElements, arr.Elements)
if immutable, ok := args[1].(object.Immutable); ok {
newElements[length] = immutable.Clone()
} else {
newElements[length] = args[1]
}
return &object.Array{Elements: newElements}
newArray := arr.Copy()
newArray.Append(args[1])
return newArray
}