diff options
author | kartofen <mladenovnasko0@gmail.com> | 2024-09-01 00:44:56 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2024-09-01 00:44:56 +0300 |
commit | 329166705de225bc766e56cc77765430065c456d (patch) | |
tree | 050b12b3a202cf43e9850903bd5b8bcc8ec67d7c /src/value.c | |
parent | e1ceef73192f0300ff9b10ba9a16475fbebeaa5f (diff) |
linked list and macros
Diffstat (limited to 'src/value.c')
-rw-r--r-- | src/value.c | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/src/value.c b/src/value.c index 54c0d20..9a7f9b6 100644 --- a/src/value.c +++ b/src/value.c @@ -14,26 +14,28 @@ const char * const value_type_string[] = { #define VALUE(_value) (_value)->value #define FN(fn, ...) return fn(buf, buf_sz, __VA_ARGS__) -#define VALUE_STRING_TABLE(X, v, buf, buf_sz) \ - X(VALUE_NIL, FN(snprintf, "(nil)")) \ - X(VALUE_ATOM, FN(snprintf, "%s", VALUE(v).atom)) \ - X(VALUE_STR, FN(snprintf, "%s", VALUE(v).str)) \ - X(VALUE_INT, FN(snprintf, "%d", VALUE(v).num)) \ - X(VALUE_CONS, FN(cons_print, &VALUE(v).cons)) \ - X(VALUE_PROC, FN(proc_print, &VALUE(v).proc)) \ - X(VALUE_PROC_BUILTIN, \ +#define VALUE_STRING_TABLE(X, v, buf, buf_sz) \ + X(VALUE_NIL, FN(snprintf, "(nil)")) \ + X(VALUE_ATOM, FN(snprintf, "%s", VALUE(v).atom)) \ + X(VALUE_STR, FN(snprintf, "%s", VALUE(v).str)) \ + X(VALUE_INT, FN(snprintf, "%d", VALUE(v).num)) \ + X(VALUE_CONS, FN(cons_print, &VALUE(v).cons)) \ + X(VALUE_PROC, FN(proc_print, &VALUE(v).proc)) \ + X(VALUE_MACRO, FN(proc_print, &VALUE(v).proc)) \ + X(VALUE_PROC_BUILTIN, \ FN(snprintf, "%p", *(void **)&VALUE(v).proc_builtin.proc)) #define NOREFS(value) (--(value)->refs == 0) -#define VALUE_DESTROY_TABLE(X, v) \ - X(VALUE_NIL, (void)NOREFS(v)) \ - X(VALUE_ATOM, if(NOREFS(v)) free(VALUE(v).atom)) \ - X(VALUE_STR, if(NOREFS(v)) free(VALUE(v).str)) \ - X(VALUE_INT, (void)NOREFS(v)) \ - X(VALUE_CONS, (void)NOREFS(v); \ - value_destroy(VALUE(v).cons.left); \ - value_destroy(VALUE(v).cons.right)) \ - X(VALUE_PROC, if(NOREFS(v)) proc_destroy(&VALUE(v).proc)) \ +#define VALUE_DESTROY_TABLE(X, v) \ + X(VALUE_NIL, (void)NOREFS(v)) \ + X(VALUE_ATOM, if(NOREFS(v)) free(VALUE(v).atom)) \ + X(VALUE_STR, if(NOREFS(v)) free(VALUE(v).str)) \ + X(VALUE_INT, (void)NOREFS(v)) \ + X(VALUE_CONS, (void)NOREFS(v); \ + value_destroy(VALUE(v).cons.left); \ + value_destroy(VALUE(v).cons.right)) \ + X(VALUE_PROC, if(NOREFS(v)) proc_destroy(&VALUE(v).proc)) \ + X(VALUE_MACRO, if(NOREFS(v)) proc_destroy(&VALUE(v).proc)) \ X(VALUE_PROC_BUILTIN, (void)NOREFS(v)) #define CREATE(vtype, value) return value_create(vtype, value) @@ -49,12 +51,13 @@ const char * const value_type_string[] = { #define CASE_APPLY(vtype, apply) \ case vtype: ; apply; break; -#define VALUE_MEMBER_TABLE(X) \ - X(VALUE_ATOM, atom) \ - X(VALUE_STR, str) \ - X(VALUE_INT, num) \ - X(VALUE_CONS, cons) \ - X(VALUE_PROC, proc) \ +#define VALUE_MEMBER_TABLE(X) \ + X(VALUE_ATOM, atom) \ + X(VALUE_STR, str) \ + X(VALUE_INT, num) \ + X(VALUE_CONS, cons) \ + X(VALUE_PROC, proc) \ + X(VALUE_MACRO, proc) \ X(VALUE_PROC_BUILTIN, proc_builtin) @@ -162,7 +165,7 @@ static int cons_print(char *buf, size_t buf_sz, struct cons *cons) value_t right = cons->right; while(right->type == VALUE_CONS) { SET_CHAR(' '); - SET_VALUE_STRING(right->value.cons.left) + SET_VALUE_STRING(right->value.cons.left); right = right->value.cons.right; } @@ -189,7 +192,7 @@ static int cons_print(char *buf, size_t buf_sz, struct cons *cons) } exit: - return (int)offset; + return (int)offset-1; // -1 because of \0 } static int proc_print(char *buf, size_t buf_sz, struct proc *proc) @@ -205,8 +208,6 @@ static void proc_destroy(struct proc *proc) for(size_t i = 0; i < proc->argc; i++) value_destroy(proc->arg_keys[i]); free(proc->arg_keys); - for(size_t i = 0; i < proc->body_len; i++) token_dealloc(&proc->body[i]); - free(proc->body); - + toklist_destroy(proc->body); env_destroy(proc->parent_env); } |