Add mapcar builtin, more tests, fixes
This commit is contained in:
@@ -182,3 +182,15 @@ struct ucl_object *ucl_builtin_list(struct ucl_state *state, struct ucl_object *
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
LISP_FUNC_2(ucl_builtin_mapcar, state, fun, elems) {
|
||||
// TODO: Support arbitrary number of 'elems' lists
|
||||
struct ucl_object *result = ucl_nil_create();
|
||||
FOREACH_LIST(elems, iter, elem) {
|
||||
struct ucl_object *form = ucl_tuple_create(fun, elem);
|
||||
struct ucl_object *value = ucl_evaluate(state, form);
|
||||
UCL_RET_IF_ERROR(value);
|
||||
ucl_list_append(result, value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@ struct ucl_object *ucl_builtin_car(struct ucl_state *state, struct ucl_object *a
|
||||
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_mapcar(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);
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ struct ucl_object *ucl_evaluate_builtin_form(struct ucl_state *state, struct ucl
|
||||
|
||||
FOREACH_LIST(list, iter, item) {
|
||||
struct ucl_object *obj = ucl_evaluate(state, item);
|
||||
UCL_RET_IF_ERROR(obj);
|
||||
ucl_list_append(evaluated_list, obj);
|
||||
};
|
||||
|
||||
@@ -47,20 +48,7 @@ struct ucl_object *ucl_evaluate_special_form(struct ucl_state *state, struct ucl
|
||||
// TODO: Recursively eval args
|
||||
const char *fun_sym = ucl_car(list)->symbol;
|
||||
|
||||
struct ucl_object *fun = ucl_state_get(state, ucl_car(list)->symbol);
|
||||
struct ucl_object *args = ucl_cdr(list);
|
||||
struct ucl_object *result = NULL;
|
||||
|
||||
result = fun->special(state, args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
struct ucl_object *ucl_evaluate_lisp_form(struct ucl_state *state, struct ucl_object *list) {
|
||||
// TODO: Recursively eval args
|
||||
const char *fun_sym = ucl_car(list)->symbol;
|
||||
|
||||
struct ucl_object *fun = ucl_state_get(state, ucl_car(list)->symbol);
|
||||
struct ucl_object *fun = ucl_state_get(state, fun_sym);
|
||||
struct ucl_object *args = ucl_cdr(list);
|
||||
struct ucl_object *result = NULL;
|
||||
|
||||
@@ -74,11 +62,7 @@ struct ucl_object *ucl_evaluate_list(struct ucl_state *state, struct ucl_object
|
||||
return list;
|
||||
}
|
||||
|
||||
struct ucl_object *fun_sym = ucl_car(list);
|
||||
if (fun_sym->type != UCL_TYPE_SYMBOL) {
|
||||
return ucl_error_create("Unknown function symbol");
|
||||
}
|
||||
struct ucl_object *fun = ucl_state_get(state, fun_sym->symbol);
|
||||
struct ucl_object *fun = ucl_evaluate(state, ucl_car(list));
|
||||
UCL_RET_IF_ERROR(fun);
|
||||
|
||||
if (fun->type == UCL_TYPE_SPECIAL) {
|
||||
|
||||
@@ -26,7 +26,6 @@ int main(int argc, const char **argv) {
|
||||
|
||||
ucl_state_put(state, "print", ucl_builtin_create(ucl_builtin_print));
|
||||
ucl_state_put(state, "printl", ucl_builtin_create(ucl_builtin_printl));
|
||||
ucl_state_put(state, "list", ucl_builtin_create(ucl_builtin_list));
|
||||
// TODO:
|
||||
// - object -> string
|
||||
// - formatted printing?
|
||||
@@ -47,9 +46,13 @@ int main(int argc, const char **argv) {
|
||||
ucl_state_put(state, "string-p", ucl_builtin_create(ucl_builtin_string_p));
|
||||
ucl_state_put(state, "int-p", ucl_builtin_create(ucl_builtin_int_p));
|
||||
ucl_state_put(state, "list-p", ucl_builtin_create(ucl_builtin_list_p));
|
||||
|
||||
ucl_state_put(state, "list", ucl_builtin_create(ucl_builtin_list));
|
||||
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));
|
||||
ucl_state_put(state, "mapcar", ucl_builtin_create(ucl_builtin_mapcar));
|
||||
|
||||
// TODO:
|
||||
// - equality
|
||||
// - map
|
||||
|
||||
@@ -27,7 +27,7 @@ struct ucl_object *ucl_special_let(struct ucl_state *state, struct ucl_object *a
|
||||
ucl_state_put(let_state, sym->symbol, value);
|
||||
}
|
||||
|
||||
struct ucl_object *result = ucl_progn(state, expressions);
|
||||
struct ucl_object *result = ucl_progn(let_state, expressions);
|
||||
ucl_state_delete(let_state);
|
||||
return result;
|
||||
}
|
||||
@@ -95,7 +95,7 @@ struct ucl_object *ucl_special_setq(struct ucl_state *state, struct ucl_object *
|
||||
|
||||
ucl_state_put(root_state, sym->symbol, value);
|
||||
|
||||
return sym;
|
||||
return value;
|
||||
}
|
||||
|
||||
struct ucl_object *ucl_special_progn(struct ucl_state *state, struct ucl_object *args) {
|
||||
|
||||
@@ -73,7 +73,7 @@ struct ucl_object *ucl_list_length(struct ucl_object *list) {
|
||||
struct ucl_object *ucl_list_nth(struct ucl_object *list, int n) {
|
||||
UCL_COND_OR_RET_ERROR(
|
||||
list != NULL && list->type == UCL_TYPE_CELL,
|
||||
"Invalid type of argument 0 to 'ucl_list_'");
|
||||
"Invalid type of argument 0 to 'ucl_list_nth'");
|
||||
|
||||
int length = ucl_list_length(list)->integer;
|
||||
UCL_COND_OR_RET_ERROR(length > n, "Position n >= list length in ucl_list_nth");
|
||||
|
||||
Reference in New Issue
Block a user