aboutsummaryrefslogtreecommitdiff
path: root/src/value.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2024-11-16 17:20:23 +0200
committerkartofen <mladenovnasko0@gmail.com>2024-11-16 17:20:23 +0200
commit1945dbf8345a2b59e9057d21e38c78913272bdaa (patch)
tree6f908d5feb358d5deb37e3137684208408a52186 /src/value.c
parent0dd38d08551ac2fcff53eb604f6363f37b25aef9 (diff)
cleanup and minor refactoringHEADmaster
Diffstat (limited to 'src/value.c')
-rw-r--r--src/value.c56
1 files changed, 34 insertions, 22 deletions
diff --git a/src/value.c b/src/value.c
index 94a7acd..547da96 100644
--- a/src/value.c
+++ b/src/value.c
@@ -1,21 +1,27 @@
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "value.h"
+
#include "lexer.h"
-#include "mempool.h"
-MEMPOOL_GENERATE(value, struct value, 64)
-#define NOT_IMPLEMENTED() die("Not Implemented. ABORTING")
+// #ifdef ENABLE_MEMPOOL
+ #include "mempool.h"
+ MEMPOOL_GENERATE(value, struct value, 64)
+ #define value_alloc() value_mempool_allocate()
+ #define value_free(v) value_mempool_free(v)
+// #else
+ // #define value_alloc() malloc(sizeof(struct value))
+ // #define value_free(v) free(v)
+// #endif
const char * const value_type_string[] = {
VALUE_TYPES(TO_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)")) \
@@ -34,9 +40,9 @@ const char * const value_type_string[] = {
X(VALUE_ATOM, if(NOREFS(v)) free(vvalue_atom(v))) \
X(VALUE_STR, if(NOREFS(v)) free(vvalue_str(v))) \
X(VALUE_INT, (void)NOREFS(v)) \
- X(VALUE_CONS, (void)NOREFS(v); \
+ X(VALUE_CONS, if(NOREFS(v)) { \
value_destroy(vvalue_cons(v).left); \
- value_destroy(vvalue_cons(v).right)) \
+ value_destroy(vvalue_cons(v).right);}) \
X(VALUE_PROC, if(NOREFS(v)) proc_destroy(&vvalue_proc(v))) \
X(VALUE_MACRO, if(NOREFS(v)) proc_destroy(&vvalue_proc(v))) \
X(VALUE_PROC_BUILTIN, (void)NOREFS(v))
@@ -63,12 +69,6 @@ const char * const value_type_string[] = {
X(VALUE_MACRO, proc) \
X(VALUE_PROC_BUILTIN, proc_builtin)
-// #define value_alloc() malloc(sizeof(struct value))
-// #define value_dealloc(v) free(v)
-#define value_alloc() value_mempool_allocate()
-#define value_free(v) value_mempool_free(v)
-
-
static char *str_alloc_copy(char *src);
static int cons_print(char *buf, size_t buf_sz, struct cons *cons);
@@ -123,14 +123,8 @@ value_t value_from_token(struct token *token)
value_t value_copy(value_t value)
{
if(!value) return value;
-
+
value_inc_refs(value);
-
- if(vvalue_type(value) == VALUE_CONS) {
- value_copy(vvalue_cons(value).left);
- value_copy(vvalue_cons(value).right);
- }
-
return value;
}
@@ -145,6 +139,24 @@ int value_string(value_t value, size_t buf_sz, char *buf)
return 0;
}
+char *value_static_string(value_t value)
+{
+ static char str[1024] = {0};
+
+ if(value_string(value, sizeof(str), str) > 0)
+ return str;
+ return NULL;
+}
+
+#ifdef DEBUG
+void value_print(value_t value)
+{
+ printf("%-12s %s", value ?
+ value_type_string[vvalue_type(value)] : "",
+ value_static_string(value));
+}
+#endif
+
static char *str_alloc_copy(char *src)
{
if(!src) return src;
@@ -204,10 +216,10 @@ exit:
static int proc_print(char *buf, size_t buf_sz, struct proc *proc)
{
- return 0;
(void)buf; (void)buf_sz;
(void)proc;
NOT_IMPLEMENTED();
+ return 0;
}
static void proc_destroy(struct proc *proc)
@@ -215,6 +227,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);
- toklist_destroy(proc->body);
+ toklist_dealloc(proc->body);
env_destroy(proc->parent_env);
}