Skip to content
This repository was archived by the owner on Oct 3, 2021. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions models/object/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,55 @@ 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
},
},
},
{
"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]}
},
},
},
}