type checking and error handling for builtins improved.
This commit is contained in:
@@ -1,25 +1,22 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Rest ...
|
||||
func Rest(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != object.ARRAY_OBJ {
|
||||
return newError("argument to `rest` must be array, got %s",
|
||||
args[0].Type())
|
||||
if err := typing.Check(
|
||||
"rest", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.ARRAY_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
arr := args[0].(*object.Array)
|
||||
length := len(arr.Elements)
|
||||
if length > 0 {
|
||||
newElements := make([]object.Object, length-1, length-1)
|
||||
copy(newElements, arr.Elements[1:length])
|
||||
return &object.Array{Elements: newElements}
|
||||
}
|
||||
|
||||
return nil
|
||||
newArray := arr.Copy()
|
||||
newArray.PopLeft()
|
||||
return newArray
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user