From aa0485fe33204169724d020f9c5e3b91e558ab1e Mon Sep 17 00:00:00 2001 From: ubq323 Date: Tue, 23 Jul 2024 17:00:56 +0100 Subject: add pend builtin function --- lib.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index e070bc0..a1ca311 100644 --- a/lib.c +++ b/lib.c @@ -40,7 +40,21 @@ static Val fn_len(State *S, int nargs, Val *args) { ObjArr *a = AS_ARR(args[0]); return VAL_NUM(a->len); } - +static Val fn_pend(State *S, int nargs, Val *args) { + CHECK(nargs > 0, "need at least 1 arg to ,"); + ObjArr *a = objarr_new(S); + for (int i = 0; i < nargs; i++) { + Val v = args[i]; + if (!IS_ARR(v)) + objarr_append(S, a, v); + else { + ObjArr *b = AS_ARR(v); + for (int j = 0; j < b->len; j++) + objarr_append(S, a, b->d[j]); + } + } + return VAL_OBJ(a); +} typedef struct { @@ -55,6 +69,7 @@ static BuiltinFunc builtin_funcs[] = { { "arr", fn_arr }, { "append", fn_append }, { "len", fn_len }, + { ",", fn_pend }, { 0 }, }; -- cgit v1.2.3