From 2e62b41072738142dea9f0b5dd5d2d22455c7616 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Wed, 26 Jun 2024 19:27:42 +0100 Subject: add arrays, appending, getting length, indexing --- lib.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'lib.c') 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 }, }; -- cgit v1.2.3