Add 'list' and 'print' builtins
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
LISP_FUNC_1(ucl_builtin_type, state, arg) {
|
||||
switch (arg->type) {
|
||||
@@ -21,6 +22,8 @@ LISP_FUNC_1(ucl_builtin_type, state, arg) {
|
||||
return ucl_symbol_create("string");
|
||||
case UCL_TYPE_ERROR:
|
||||
return ucl_symbol_create("error");
|
||||
case UCL_TYPE_BUILTIN:
|
||||
return ucl_symbol_create("builtin");
|
||||
case UCL_TYPE_COUNT:
|
||||
assert(0);
|
||||
return NULL;
|
||||
@@ -138,7 +141,9 @@ LISP_FUNC_2(ucl_builtin_concat, state, arg0, arg1) {
|
||||
strcat(outstr, arg0->string);
|
||||
strcat(outstr, arg1->string);
|
||||
|
||||
return ucl_string_create(outstr);
|
||||
struct ucl_object *result = ucl_string_create(outstr);
|
||||
free(outstr);
|
||||
return result;
|
||||
}
|
||||
|
||||
LISP_FUNC_0(ucl_builtin_now_millis_mono, state) {
|
||||
@@ -146,6 +151,47 @@ LISP_FUNC_0(ucl_builtin_now_millis_mono, state) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void ucl_print_obj(struct ucl_object *obj) {
|
||||
switch (obj->type) {
|
||||
case UCL_TYPE_SYMBOL:
|
||||
printf("%s", obj->symbol);
|
||||
break;
|
||||
case UCL_TYPE_INT:
|
||||
printf("%d", obj->integer);
|
||||
break;
|
||||
case UCL_TYPE_STRING:
|
||||
printf("\"%s\"", obj->string);
|
||||
break;
|
||||
case UCL_TYPE_ERROR:
|
||||
printf("(error \"%s\")", obj->error);
|
||||
break;
|
||||
case UCL_TYPE_BUILTIN:
|
||||
printf("<builtin %p>", obj->builtin);
|
||||
break;
|
||||
case UCL_TYPE_CELL: {
|
||||
int first = true;
|
||||
printf("%s", "(");
|
||||
FOREACH_LIST(obj, iter, item) {
|
||||
if (!first) {
|
||||
printf(" ");
|
||||
}
|
||||
ucl_print_obj(item);
|
||||
first = false;
|
||||
}
|
||||
printf("%s", ")");
|
||||
break;
|
||||
}
|
||||
case UCL_TYPE_COUNT:
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
LISP_FUNC_1(ucl_builtin_print, state, arg0) {
|
||||
ucl_print_obj(arg0);
|
||||
|
||||
return ucl_nil_create();
|
||||
}
|
||||
|
||||
struct ucl_object *ucl_builtin_let(struct ucl_state *state, struct ucl_object *args) {
|
||||
// TODO: Check arguments
|
||||
struct ucl_object *assignments = ucl_car(args);
|
||||
@@ -180,3 +226,12 @@ struct ucl_object *ucl_builtin_let(struct ucl_state *state, struct ucl_object *a
|
||||
ucl_state_delete(let_state);
|
||||
return result;
|
||||
}
|
||||
|
||||
struct ucl_object *ucl_builtin_list(struct ucl_state *state, struct ucl_object *args) {
|
||||
struct ucl_object *head = ucl_nil_create();
|
||||
FOREACH_LIST(args, iter, item) {
|
||||
ucl_list_append(head, item);
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user