diff options
author | kartofen <mladenovnasko0@gmail.com> | 2024-08-30 17:01:28 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2024-08-30 17:01:28 +0300 |
commit | e1ceef73192f0300ff9b10ba9a16475fbebeaa5f (patch) | |
tree | 0cb2bc5336a522b965c1d171b433044591721e20 /src/builtin.h | |
parent | de3a062bfc206bf0373f96f4f6cc8c74ffcbab48 (diff) |
proper repl, stylistic changes, removed trailing whitespace
Diffstat (limited to 'src/builtin.h')
-rw-r--r-- | src/builtin.h | 78 |
1 files changed, 34 insertions, 44 deletions
diff --git a/src/builtin.h b/src/builtin.h index 43d7ee2..51dd89c 100644 --- a/src/builtin.h +++ b/src/builtin.h @@ -8,17 +8,16 @@ X(cons, "cons", 2) \ X(car, "car", 1) \ X(cdr, "cdr", 1) \ - X(display,"display", 1) \ + X(display,"display", 1) \ + X(is_nil, "nil?", 1) \ // Number of builtin procedures #define PLUS_ONE(_symbol, _name, _argc) 1 + #define BUILTIN_PROCEDURES PROCEDURES(PLUS_ONE) 0 // Forward decalration of the procedures -#define DECLARE_PROCEDURE(proc) value_t proc(value_t *args) #define FORWARD_DECLARATION(symbol, _name, _argc) \ - DECLARE_PROCEDURE(symbol); - + value_t symbol(value_t *args); PROCEDURES(FORWARD_DECLARATION) // Fill procedure struct for the value_t @@ -39,40 +38,34 @@ const char *builtin_proc_name_list[] = { // ----- Definitions ----- -#define ASSERT_TYPE(proc, args, pos, vtype, fail) \ - if(args[pos]->type != vtype) { \ - err("Expected arg %d of %s to be %s instead of %s", \ - pos, #proc, "", ""); \ - fail; \ +#define ASSERT_TYPE(proc, args, pos, vtype, fail) \ + if(args[pos]->type != vtype) { \ + err("Expected arg %d of '%s' to be %s instead of %s", \ + pos, #proc, \ + value_type_string[vtype], \ + value_type_string[args[pos]->type]); \ + fail; \ } -#define PROC_ASSERT_TYPE(pos, vtype, fail) \ - ASSERT_TYPE(P, args, pos, vtype, fail) - -#define P plus -DECLARE_PROCEDURE(P) +value_t plus(value_t *args) { - PROC_ASSERT_TYPE(0, VALUE_INT, return VALUE_EMPTY); - PROC_ASSERT_TYPE(1, VALUE_INT, return VALUE_EMPTY); + ASSERT_TYPE(plus, args, 0, VALUE_INT, return VALUE_EMPTY); + ASSERT_TYPE(plus, args, 1, VALUE_INT, return VALUE_EMPTY); int sum = args[0]->value.num + args[1]->value.num; return value_create(VALUE_INT, &sum); } -#undef P -#define P minus -DECLARE_PROCEDURE(P) +value_t minus(value_t *args) { - PROC_ASSERT_TYPE(0, VALUE_INT, return VALUE_EMPTY); - PROC_ASSERT_TYPE(1, VALUE_INT, return VALUE_EMPTY); + ASSERT_TYPE(minus, args, 0, VALUE_INT, return VALUE_EMPTY); + ASSERT_TYPE(minus, args, 1, VALUE_INT, return VALUE_EMPTY); int difference = args[0]->value.num - args[1]->value.num; return value_create(VALUE_INT, &difference); } -#undef P -#define P equal -DECLARE_PROCEDURE(P) +value_t equal(value_t *args) { int f = 0; int t = 1; @@ -90,48 +83,45 @@ l_false: l_true: return value_create(VALUE_INT, &t); } -#undef P -#define P cons -DECLARE_PROCEDURE(P) +value_t cons(value_t *args) { struct cons cons = {value_copy(args[0]), value_copy(args[1])}; return value_create(VALUE_CONS, &cons); } -#undef P -#define P car -DECLARE_PROCEDURE(P) +value_t car(value_t *args) { - PROC_ASSERT_TYPE(0, VALUE_CONS, return VALUE_EMPTY); + ASSERT_TYPE(car, args, 0, VALUE_CONS, return VALUE_EMPTY); value_t left = value_copy(args[0]->value.cons.left); return left; } -#undef P -#define P cdr -DECLARE_PROCEDURE(P) +value_t cdr(value_t *args) { - PROC_ASSERT_TYPE(0, VALUE_CONS, return VALUE_EMPTY); + ASSERT_TYPE(cdr, args, 0, VALUE_CONS, return VALUE_EMPTY); value_t right = value_copy(args[0]->value.cons.right); return right; } -#undef P -#define P display -DECLARE_PROCEDURE(P) +value_t display(value_t *args) { char buf[256]; - value_string(args[0], (sizeof(buf)/sizeof(*buf)), buf); + value_string(args[0], sizeof(buf), buf); + + // if(bytes > sizeof(buf)) ... - #ifdef DEBUG - info("%s", buf); - #else printf("%s\n", buf); - #endif return value_copy(args[0]); } -#undef P + +value_t is_nil(value_t *args) +{ + int f = 0, t = 1; + if(args[0]->type == VALUE_NIL) + return value_create(VALUE_INT, &t); + return value_create(VALUE_INT, &f); +} |