Add progn special form
This commit is contained in:
20
src/main.c
20
src/main.c
@@ -17,15 +17,28 @@ int main(int argc, const char **argv) {
|
|||||||
ucl_state_put(state, "if", ucl_special_create(ucl_special_if));
|
ucl_state_put(state, "if", ucl_special_create(ucl_special_if));
|
||||||
ucl_state_put(state, "defun", ucl_special_create(ucl_special_defun));
|
ucl_state_put(state, "defun", ucl_special_create(ucl_special_defun));
|
||||||
ucl_state_put(state, "setq", ucl_special_create(ucl_special_setq));
|
ucl_state_put(state, "setq", ucl_special_create(ucl_special_setq));
|
||||||
|
ucl_state_put(state, "progn", ucl_special_create(ucl_special_progn));
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// - progn
|
||||||
|
// - quote
|
||||||
|
// - iteration
|
||||||
|
// - lambda
|
||||||
|
|
||||||
ucl_state_put(state, "print", ucl_builtin_create(ucl_builtin_print));
|
ucl_state_put(state, "print", ucl_builtin_create(ucl_builtin_print));
|
||||||
ucl_state_put(state, "list", ucl_builtin_create(ucl_builtin_list));
|
ucl_state_put(state, "list", ucl_builtin_create(ucl_builtin_list));
|
||||||
|
// TODO:
|
||||||
|
// - print with newline
|
||||||
|
// - object -> string
|
||||||
|
// - formatted printing?
|
||||||
|
|
||||||
ucl_state_put(state, "+", ucl_builtin_create(ucl_builtin_add));
|
ucl_state_put(state, "+", ucl_builtin_create(ucl_builtin_add));
|
||||||
ucl_state_put(state, "-", ucl_builtin_create(ucl_builtin_sub));
|
ucl_state_put(state, "-", ucl_builtin_create(ucl_builtin_sub));
|
||||||
ucl_state_put(state, "*", ucl_builtin_create(ucl_builtin_mul));
|
ucl_state_put(state, "*", ucl_builtin_create(ucl_builtin_mul));
|
||||||
ucl_state_put(state, "/", ucl_builtin_create(ucl_builtin_div));
|
ucl_state_put(state, "/", ucl_builtin_create(ucl_builtin_div));
|
||||||
ucl_state_put(state, "%", ucl_builtin_create(ucl_builtin_mod));
|
ucl_state_put(state, "%", ucl_builtin_create(ucl_builtin_mod));
|
||||||
|
// TODO:
|
||||||
|
// - Floats or nah?
|
||||||
|
|
||||||
ucl_state_put(state, "concat", ucl_builtin_create(ucl_builtin_concat));
|
ucl_state_put(state, "concat", ucl_builtin_create(ucl_builtin_concat));
|
||||||
|
|
||||||
@@ -35,6 +48,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, "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, "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-p", ucl_builtin_create(ucl_builtin_list_p));
|
||||||
|
// TODO:
|
||||||
|
// - equality
|
||||||
|
// - map
|
||||||
|
// - reduce
|
||||||
|
// - car
|
||||||
|
// - cdr
|
||||||
|
// - nth
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
while (1) {
|
while (1) {
|
||||||
|
|||||||
@@ -44,13 +44,7 @@ struct ucl_object *ucl_special_if(struct ucl_state *state, struct ucl_object *ar
|
|||||||
return ucl_evaluate(state, true_form);
|
return ucl_evaluate(state, true_form);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ucl_object *result = NULL;
|
return ucl_progn(state, false_forms);
|
||||||
|
|
||||||
FOREACH_LIST(false_forms, iter, form) {
|
|
||||||
result = ucl_evaluate(state, form);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (result == NULL) ? ucl_nil_create() : result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -80,13 +74,18 @@ struct ucl_object *ucl_special_defun(struct ucl_state *state, struct ucl_object
|
|||||||
struct ucl_object *ucl_special_setq(struct ucl_state *state, struct ucl_object *args) {
|
struct ucl_object *ucl_special_setq(struct ucl_state *state, struct ucl_object *args) {
|
||||||
// TODO: Check arguments
|
// TODO: Check arguments
|
||||||
struct ucl_object *sym = ucl_car(args);
|
struct ucl_object *sym = ucl_car(args);
|
||||||
|
struct ucl_state *root_state = ucl_state_get_root(state);
|
||||||
if (sym->type != UCL_TYPE_SYMBOL) {
|
if (sym->type != UCL_TYPE_SYMBOL) {
|
||||||
return ucl_error_create("First argument to defun must be a symbol");
|
return ucl_error_create("First argument to defun must be a symbol");
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ucl_object *value = ucl_evaluate(state, ucl_list_nth(args, 1));
|
struct ucl_object *value = ucl_evaluate(state, ucl_list_nth(args, 1));
|
||||||
|
|
||||||
ucl_state_put(ucl_state_get_root(state), sym->symbol, value);
|
ucl_state_put(root_state, sym->symbol, value);
|
||||||
|
|
||||||
return sym;
|
return sym;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ucl_object *ucl_special_progn(struct ucl_state *state, struct ucl_object *args) {
|
||||||
|
return ucl_progn(state, args);
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,5 +7,6 @@ struct ucl_object *ucl_special_let(struct ucl_state *state, struct ucl_object *a
|
|||||||
struct ucl_object *ucl_special_if(struct ucl_state *state, struct ucl_object *args);
|
struct ucl_object *ucl_special_if(struct ucl_state *state, struct ucl_object *args);
|
||||||
struct ucl_object *ucl_special_defun(struct ucl_state *state, struct ucl_object *args);
|
struct ucl_object *ucl_special_defun(struct ucl_state *state, struct ucl_object *args);
|
||||||
struct ucl_object *ucl_special_setq(struct ucl_state *state, struct ucl_object *args);
|
struct ucl_object *ucl_special_setq(struct ucl_state *state, struct ucl_object *args);
|
||||||
|
struct ucl_object *ucl_special_progn(struct ucl_state *state, struct ucl_object *args);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user