Skip to content
This repository was archived by the owner on Oct 3, 2021. It is now read-only.

Commit 4fd70d9

Browse files
authored
Merge pull request #5 from looplanguage/feat_array_helpers
feat: array helpers
2 parents 9e1023c + 65114f7 commit 4fd70d9

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

models/object/builtin.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,55 @@ var Builtins = []struct {
5454
},
5555
},
5656
},
57+
{
58+
"append",
59+
&BuiltinFunction{
60+
Function: func(args []Object) Object {
61+
if len(args) <= 1 {
62+
return &Error{Message: fmt.Sprintf("wrong number of arguments. expected=%d. got=%d", 2, len(args))}
63+
}
64+
65+
if args[0].Type() != "ARRAY" {
66+
return &Error{Message: fmt.Sprintf("wrong first argument. expected=%q. got=%q", "ARRAY", args[0].Type())}
67+
}
68+
69+
array := args[0].(*Array)
70+
71+
newArray := &Array{Elements: array.Elements}
72+
73+
args = args[1:]
74+
75+
newArray.Elements = append(newArray.Elements, args...)
76+
77+
return newArray
78+
},
79+
},
80+
},
81+
{
82+
"slice",
83+
&BuiltinFunction{
84+
Function: func(args []Object) Object {
85+
if len(args) <= 2 {
86+
return &Error{Message: fmt.Sprintf("wrong number of arguments. expected=%d. got=%d", 3, len(args))}
87+
}
88+
89+
array, ok := args[0].(*Array)
90+
if !ok {
91+
return &Error{Message: fmt.Sprintf("wrong argument. expected=\"ARRAY\". got=%q", args[0].Type())}
92+
}
93+
94+
start, ok := args[1].(*Integer)
95+
if !ok {
96+
return &Error{Message: fmt.Sprintf("wrong argument. expected=\"INTEGER\". got=%q", args[0].Type())}
97+
}
98+
99+
end, ok := args[2].(*Integer)
100+
if !ok {
101+
return &Error{Message: fmt.Sprintf("wrong argument. expected=\"INTEGER\". got=%q", args[0].Type())}
102+
}
103+
104+
return &Array{Elements: array.Elements[start.Value:end.Value]}
105+
},
106+
},
107+
},
57108
}

0 commit comments

Comments
 (0)