From 57dd49a3dd4e06230dccde12d383e5138d70194e Mon Sep 17 00:00:00 2001 From: kanersps <12296463+kanersps@users.noreply.github.com> Date: Tue, 28 Sep 2021 14:00:00 +0200 Subject: [PATCH 1/2] feat: append function --- models/object/builtin.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/models/object/builtin.go b/models/object/builtin.go index 3cebbb2..5bc1648 100644 --- a/models/object/builtin.go +++ b/models/object/builtin.go @@ -54,4 +54,29 @@ var Builtins = []struct { }, }, }, + { + "append", + &BuiltinFunction{ + Function: func(args []Object) Object { + if len(args) <= 1 { + return &Error{Message: fmt.Sprintf("wrong number of arguments. expected=%d. got=%d", 2, len(args))} + } + + if args[0].Type() != "ARRAY" { + return &Error{Message: fmt.Sprintf("wrong first argument. expected=%q. got=%q", "ARRAY", args[0].Type())} + } + + array := args[0].(*Array) + + newArray := &Array{Elements: array.Elements} + + args = args[1:] + + newArray.Elements = append(newArray.Elements, args...) + + return newArray + }, + }, + }, + {}, } From 65114f70e38780d39627e78a4cd125782560734a Mon Sep 17 00:00:00 2001 From: kanersps <12296463+kanersps@users.noreply.github.com> Date: Tue, 28 Sep 2021 14:06:25 +0200 Subject: [PATCH 2/2] feat: slice function --- models/object/builtin.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/models/object/builtin.go b/models/object/builtin.go index 5bc1648..f479b97 100644 --- a/models/object/builtin.go +++ b/models/object/builtin.go @@ -78,5 +78,31 @@ var Builtins = []struct { }, }, }, - {}, + { + "slice", + &BuiltinFunction{ + Function: func(args []Object) Object { + if len(args) <= 2 { + return &Error{Message: fmt.Sprintf("wrong number of arguments. expected=%d. got=%d", 3, len(args))} + } + + array, ok := args[0].(*Array) + if !ok { + return &Error{Message: fmt.Sprintf("wrong argument. expected=\"ARRAY\". got=%q", args[0].Type())} + } + + start, ok := args[1].(*Integer) + if !ok { + return &Error{Message: fmt.Sprintf("wrong argument. expected=\"INTEGER\". got=%q", args[0].Type())} + } + + end, ok := args[2].(*Integer) + if !ok { + return &Error{Message: fmt.Sprintf("wrong argument. expected=\"INTEGER\". got=%q", args[0].Type())} + } + + return &Array{Elements: array.Elements[start.Value:end.Value]} + }, + }, + }, }