Add lots of utilties, initial builtins
This commit is contained in:
60
src/builtins.h
Normal file
60
src/builtins.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "nihilispm_utility.h"
|
||||
|
||||
#define LISP_FUNC_0(func_name) \
|
||||
static struct nl_object *func_name##_impl(); \
|
||||
struct nl_object *func_name(struct nl_object *args) { \
|
||||
if (args->cell.car != NULL) { \
|
||||
return NULL; \
|
||||
} \
|
||||
return func_name##_impl(); \
|
||||
} \
|
||||
static struct nl_object *func_name##_impl()
|
||||
|
||||
#define LISP_FUNC_1(func_name, arg0_name) \
|
||||
static struct nl_object *func_name##_impl(struct nl_object *arg0_name); \
|
||||
struct nl_object *func_name(struct nl_object *args) { \
|
||||
struct nl_object *len_obj = nl_list_length(args); \
|
||||
if (len_obj->type != NL_TYPE_INT) { \
|
||||
return NULL; \
|
||||
} \
|
||||
if (len_obj->integer != 1) { \
|
||||
return NULL; \
|
||||
} \
|
||||
struct nl_object *arg0 = nl_car(args); \
|
||||
return func_name##_impl(arg0); \
|
||||
} \
|
||||
static struct nl_object *func_name##_impl(struct nl_object *arg0_name)
|
||||
|
||||
// TODO: Unroll the args more efficiently, this is O(n^2)
|
||||
#define LISP_FUNC_2(func_name, arg0_name, arg1_name) \
|
||||
static struct nl_object *func_name##_impl(struct nl_object *arg0_name, struct nl_object *arg1_name); \
|
||||
struct nl_object *func_name(struct nl_object *args) { \
|
||||
struct nl_object *len_obj = nl_list_length(args); \
|
||||
if (len_obj->type != NL_TYPE_INT) { \
|
||||
return NULL; \
|
||||
} \
|
||||
if (len_obj->integer != 2) { \
|
||||
return NULL; \
|
||||
} \
|
||||
struct nl_object *arg0 = nl_list_nth(args, 0); \
|
||||
struct nl_object *arg1 = nl_list_nth(args, 1); \
|
||||
return func_name##_impl(arg0, arg1); \
|
||||
} \
|
||||
static struct nl_object *func_name##_impl(struct nl_object *arg0_name, struct nl_object *arg1_name)
|
||||
|
||||
|
||||
struct nl_object *nl_builtin_hello_world(struct nl_object *args);
|
||||
struct nl_object *nl_builtin_type(struct nl_object *args);
|
||||
struct nl_object *nl_builtin_symbol_p(struct nl_object *args);
|
||||
struct nl_object *nl_builtin_string_p(struct nl_object *args);
|
||||
struct nl_object *nl_builtin_int_p(struct nl_object *args);
|
||||
struct nl_object *nl_builtin_list_p(struct nl_object *args);
|
||||
|
||||
struct nl_object *nl_builtin_add(struct nl_object *args);
|
||||
struct nl_object *nl_builtin_sub(struct nl_object *args);
|
||||
struct nl_object *nl_builtin_mul(struct nl_object *args);
|
||||
struct nl_object *nl_builtin_div(struct nl_object *args);
|
||||
struct nl_object *nl_builtin_mod(struct nl_object *args);
|
||||
struct nl_object *nl_builtin_concat(struct nl_object *args);
|
||||
|
||||
struct nl_object *nl_builtin_now_millis_mono(struct nl_object *args);
|
||||
Reference in New Issue
Block a user