Add nth builtin

This commit is contained in:
2022-11-04 22:44:17 -04:00
parent a5ef5c9fa0
commit f9ace289f1
3 changed files with 9 additions and 1 deletions

View File

@@ -69,6 +69,13 @@ LISP_FUNC_1(ucl_builtin_cdr, state, arg) {
return ucl_cdr(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) { LISP_FUNC_2(ucl_builtin_add, state, arg0, arg1) {
if (arg0->type != UCL_TYPE_INT) { if (arg0->type != UCL_TYPE_INT) {
return ucl_error_create("Invalid type of argument 0 to 'add'"); return ucl_error_create("Invalid type of argument 0 to 'add'");

View File

@@ -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_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_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_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_print(struct ucl_state *state, struct ucl_object *args);
struct ucl_object *ucl_builtin_printl(struct ucl_state *state, struct ucl_object *args); struct ucl_object *ucl_builtin_printl(struct ucl_state *state, struct ucl_object *args);

View File

@@ -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, "list-p", ucl_builtin_create(ucl_builtin_list_p));
ucl_state_put(state, "car", ucl_builtin_create(ucl_builtin_car)); 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, "cdr", ucl_builtin_create(ucl_builtin_cdr));
ucl_state_put(state, "nth", ucl_builtin_create(ucl_builtin_nth));
// TODO: // TODO:
// - equality // - equality
// - map // - map
// - reduce // - reduce
// - nth
// - booleans (e.g. not) // - booleans (e.g. not)
if (argc < 2) { if (argc < 2) {