1
0
Fork 0
forked from fun/fun

More REPL enhancements. Type :help in the REPL to get more information.

This commit is contained in:
Johannes Findeisen 2025-09-15 01:13:13 +02:00
commit 08e7c9a623
5 changed files with 168 additions and 9 deletions

View file

@ -90,3 +90,25 @@ int value_is_truthy(const Value *v) {
}
}
/* allocate a printable C string for the value; caller must free */
char *value_to_string_alloc(const Value *v) {
if (!v) return strdup("nil");
char buf[128];
switch (v->type) {
case VAL_INT: {
char tmp[64];
snprintf(tmp, sizeof(tmp), "%" PRId64, v->i);
return strdup(tmp);
}
case VAL_STRING:
return strdup(v->s ? v->s : "");
case VAL_FUNCTION: {
snprintf(buf, sizeof(buf), "<function@%p>", (void*)v->fn);
return strdup(buf);
}
case VAL_NIL:
default:
return strdup("nil");
}
}