rearrange builtins
This commit is contained in:
28
builtins/push.go
Normal file
28
builtins/push.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
|
||||
// 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())
|
||||
}
|
||||
|
||||
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}
|
||||
}
|
||||
Reference in New Issue
Block a user