commit 6c59b6321eacbed957c968a8509a8ada02b6db2c
parent 64106aab37d2ec0151ef87b83cbaa1dd2481d178
Author: bsandro <email@bsandro.tech>
Date: Wed, 29 Jun 2022 00:23:57 +0300
array builtin functions: first, last, push, pop, rpush, rpop
Diffstat:
M | eval/builtins.go | | | 97 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 97 insertions(+), 0 deletions(-)
diff --git a/eval/builtins.go b/eval/builtins.go
@@ -18,4 +18,101 @@ var builtins = map[string]*object.Builtin{
}
},
},
+ "first": &object.Builtin{
+ Fn: func(args ...object.Object) object.Object {
+ if len(args) != 1 {
+ return newError("invalid number of arguments")
+ }
+ if args[0].Type() != object.ARRAY_OBJ {
+ return newError("argument has to be an array")
+ }
+ arr := args[0].(*object.Array)
+ if len(arr.Elements) > 0 {
+ return arr.Elements[0]
+ }
+ return NULL
+ },
+ },
+ "last": &object.Builtin{
+ Fn: func(args ...object.Object) object.Object {
+ if len(args) != 1 {
+ return newError("invalid number of arguments")
+ }
+ if args[0].Type() != object.ARRAY_OBJ {
+ return newError("argument has to be an array")
+ }
+ arr := args[0].(*object.Array)
+ arrLen := len(arr.Elements)
+ if arrLen > 0 {
+ return arr.Elements[arrLen-1]
+ }
+ return NULL
+ },
+ },
+ "push": &object.Builtin{
+ Fn: func(args ...object.Object) object.Object {
+ if len(args) != 2 {
+ return newError("invalid number of arguments")
+ }
+ if args[0].Type() != object.ARRAY_OBJ {
+ return newError("argument has to be an array")
+ }
+ arr := args[0].(*object.Array)
+ newElements := append(arr.Elements, args[1])
+ return &object.Array{Elements: newElements}
+ },
+ },
+ "pop": &object.Builtin{
+ Fn: func(args ...object.Object) object.Object {
+ if len(args) != 1 {
+ return newError("invalid number of arguments")
+ }
+ if args[0].Type() != object.ARRAY_OBJ {
+ return newError("argument has to be an array")
+ }
+ arr := args[0].(*object.Array)
+ arrLen := len(arr.Elements)
+ if arrLen > 1 {
+ newElements := make([]object.Object, arrLen-1, arrLen-1)
+ copy(newElements, arr.Elements[:arrLen-1])
+ return &object.Array{Elements: newElements}
+ } else {
+ var emptyArr []object.Object
+ return &object.Array{Elements: emptyArr}
+ }
+ },
+ },
+ "rpush": &object.Builtin{
+ Fn: func(args ...object.Object) object.Object {
+ if len(args) != 2 {
+ return newError("invalid number of arguments")
+ }
+ if args[0].Type() != object.ARRAY_OBJ {
+ return newError("argument has to be an array")
+ }
+ arr := args[0].(*object.Array)
+ newElements := append([]object.Object{args[1]}, arr.Elements...)
+ return &object.Array{Elements: newElements}
+ },
+ },
+ "rpop": &object.Builtin{
+ Fn: func(args ...object.Object) object.Object {
+ if len(args) != 1 {
+ return newError("invalid number of arguments")
+ }
+ if args[0].Type() != object.ARRAY_OBJ {
+ return newError("argument has to be an array")
+ }
+ arr := args[0].(*object.Array)
+ arrLen := len(arr.Elements)
+ if arrLen > 1 {
+ newElements := make([]object.Object, arrLen-1, arrLen-1)
+ copy(newElements, arr.Elements[1:arrLen])
+ return &object.Array{Elements: newElements}
+ } else {
+ var emptyArr []object.Object
+ return &object.Array{Elements: emptyArr}
+ }
+ },
+ },
}