diff --git a/src/builtins.c b/src/builtins.c index 3e64b28..d75e860 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -69,6 +69,13 @@ LISP_FUNC_1(ucl_builtin_cdr, state, arg) { return ucl_cdr(arg); } +LISP_FUNC_2(ucl_builtin_nth, state, n, list) { + UCL_COND_OR_RET_ERROR(n->type == UCL_TYPE_INT, "First argument to nth must be an integer"); + UCL_COND_OR_RET_ERROR(list->type == UCL_TYPE_CELL, "Second argument to nth must be a list"); + + return ucl_list_nth(list, n->integer); +} + LISP_FUNC_2(ucl_builtin_add, state, arg0, arg1) { if (arg0->type != UCL_TYPE_INT) { return ucl_error_create("Invalid type of argument 0 to 'add'"); diff --git a/src/builtins.h b/src/builtins.h index d762363..4cc65fe 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -21,6 +21,7 @@ struct ucl_object *ucl_builtin_now_millis_mono(struct ucl_state *state, struct u struct ucl_object *ucl_builtin_car(struct ucl_state *state, struct ucl_object *args); struct ucl_object *ucl_builtin_cdr(struct ucl_state *state, struct ucl_object *args); +struct ucl_object *ucl_builtin_nth(struct ucl_state *state, struct ucl_object *args); struct ucl_object *ucl_builtin_list(struct ucl_state *state, struct ucl_object *args); struct ucl_object *ucl_builtin_print(struct ucl_state *state, struct ucl_object *args); struct ucl_object *ucl_builtin_printl(struct ucl_state *state, struct ucl_object *args); diff --git a/src/main.c b/src/main.c index f1827a2..9836640 100644 --- a/src/main.c +++ b/src/main.c @@ -49,11 +49,11 @@ int main(int argc, const char **argv) { ucl_state_put(state, "list-p", ucl_builtin_create(ucl_builtin_list_p)); ucl_state_put(state, "car", ucl_builtin_create(ucl_builtin_car)); ucl_state_put(state, "cdr", ucl_builtin_create(ucl_builtin_cdr)); + ucl_state_put(state, "nth", ucl_builtin_create(ucl_builtin_nth)); // TODO: // - equality // - map // - reduce - // - nth // - booleans (e.g. not) if (argc < 2) {