summaryrefslogtreecommitdiff
path: root/lib.c
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-06-26 19:27:42 +0100
committerubq323 <ubq323@ubq323.website>2024-06-26 19:27:42 +0100
commit2e62b41072738142dea9f0b5dd5d2d22455c7616 (patch)
treef47ce547de39fb69b02eb70990c9485489f1e574 /lib.c
parent6deeb9630d4b4e7d672ab851dcd4fe3d0d3d2865 (diff)
add arrays, appending, getting length, indexing
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib.c b/lib.c
index fe87abf..e070bc0 100644
--- a/lib.c
+++ b/lib.c
@@ -21,6 +21,26 @@ static Val fn_say(State *S, int nargs, Val *args) {
}
+// lists
+static Val fn_arr(State *S, int nargs, Val *args) {
+ CHECK(nargs == 0, "need 0 args to arr");
+ ObjArr *o = objarr_new(S);
+ return VAL_OBJ(o);
+}
+static Val fn_append(State *S, int nargs, Val *args) {
+ CHECK(nargs == 2, "need 2 args to append");
+ CHECK(IS_ARR(args[0]), "can only append to arr");
+ ObjArr *a = AS_ARR(args[0]);
+ objarr_append(S, a, args[1]);
+ return args[0];
+}
+static Val fn_len(State *S, int nargs, Val *args) {
+ CHECK(nargs == 1, "need 1 arg to len");
+ CHECK(IS_ARR(args[0]), "can only take length of arr");
+ ObjArr *a = AS_ARR(args[0]);
+ return VAL_NUM(a->len);
+}
+
typedef struct {
@@ -31,6 +51,10 @@ static BuiltinFunc builtin_funcs[] = {
{ "clock", fn_clock },
{ "say", fn_say },
{ "write", fn_write },
+
+ { "arr", fn_arr },
+ { "append", fn_append },
+ { "len", fn_len },
{ 0 },
};