Combine fn macros.

Toying with storing fn types in the code.
This commit is contained in:
Sage Vaillancourt 2022-03-24 23:58:54 -04:00
parent 62e430af69
commit 581dfed359
2 changed files with 17 additions and 13 deletions

View File

@ -10,17 +10,21 @@
#define array_length(_array) (sizeof(_array) / sizeof((_array)[0])) #define array_length(_array) (sizeof(_array) / sizeof((_array)[0]))
#define fn(_name, _docs, ...) static const char * const _name ## Doc = _docs; \ #define UNPACK(...) __VA_ARGS__
#define fn(_name, _docs, ...) \
static const char * const _name ## Doc = _docs; \
static const char * const _name ## Tests[] = {__VA_ARGS__}; \ static const char * const _name ## Tests[] = {__VA_ARGS__}; \
static_assert(array_length(_name ## Tests) % 2 == 0, "Array of test strings must have exactly one expected result for each test."); \ static_assert(array_length(_name ## Tests) % 2 == 0, "Array of test strings must have exactly one expected result for each test."); \
Object _name(Object* params, int length, struct Environment* env) Object _name(Object* params, int length, struct Environment* env)
#define tfn(_name, _type, _docs, ...) \
static const Type _name ## Type[] = UNPACK _type; \
fn(_name, _docs, __VA_ARGS__)
#define fnn(_name, _symbol, _docs, ...) \ #define fnn(_name, _symbol, _docs, ...) \
static const char * const _name ## Doc = _docs;\
static const char * const _name ## Symbol = _symbol; \ static const char * const _name ## Symbol = _symbol; \
static const char * const _name ## Tests[] = {__VA_ARGS__}; \ fn(_name, _docs, __VA_ARGS__)
static_assert(array_length(_name ## Tests) % 2 == 0, "Array of test strings must have exactly one expected result for each test."); \
Object _name(Object* params, int length, struct Environment* env)
struct Slice { struct Slice {
const char* text; const char* text;

View File

@ -52,8 +52,8 @@ fn(prepend,
"(pre (2 3) 1)", "( 1 2 3 )", "(pre (2 3) 1)", "( 1 2 3 )",
); );
/// LIST => NUMBER tfn(len,
fn(len, ({ TYPE_LIST, TYPE_NUMBER }),
"Returns the length of the given list, or a NOT_A_LIST error if the expression is not a list.", "Returns the length of the given list, or a NOT_A_LIST error if the expression is not a list.",
"(len (2 3))", "2", "(len (2 3))", "2",
"(len ())", "0", "(len ())", "0",