Files
monkey/object/builtin_pop.go
Chuck Smith 99cea83f57
Some checks failed
Test / build (push) Waiting to run
Build / build (push) Has been cancelled
Add tons of builtin helpers and array operations.
2024-03-24 12:11:46 -04:00

26 lines
503 B
Go

package object
// Pop ...
func Pop(args ...Object) Object {
if len(args) != 1 {
return newError("wrong number of arguments. got=%d, want=1",
len(args))
}
if args[0].Type() != ARRAY_OBJ {
return newError("argument to `pop` must be array, got %s",
args[0].Type())
}
arr := args[0].(*Array)
length := len(arr.Elements)
if length == 0 {
return newError("cannot pop from an empty array")
}
element := arr.Elements[length-1]
arr.Elements = arr.Elements[:length-1]
return element
}