REPL: persistent state + echo; add the _repr_ protocol
- REPL now runs every line on the SAME vm, so 'x = 50' then 'x'
persists across lines (was re-creating a vm per line)
- a lone expression echoes its value (new PRINTEXPR opcode), and a
call yielding no value (the empty object) is not echoed
- compiler: cat_compiler_compile_mode(src, err, repl) emits PRINTEXPR
for a lone expression in REPL mode
- _repr_ protocol: cat_value_repr honours a member (text value
used verbatim, or a callable invoked with the value as self); a
default by family otherwise. tostring is the display form (text
unquoted); the REPL echo is the repr form (text quoted)
- list/object default representations ([a, b], {k = v})
- tests: tests/repr.cat + REPL checks (92 total, all green)
This commit is contained in:
1 parent
c7a87d05e2
commit
de847e7793
11 files changed
+268
-5
No files matched your search
+147
@@ -14,10 +14,14 @@
|
||||
#include "cat/types/bool.h"
|
||||
#include "cat/types/int.h"
|
||||
#include "cat/types/float.h"
|
||||
#include "cat/types/number.h"
|
||||
#include "cat/types/list.h"
|
||||
#include "cat/types/table.h"
|
||||
#include "cat/types/text.h"
|
||||
#include "cat/types/ascii_text.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -27,6 +31,149 @@ const char *cat_value_type_name(CAT_VALUE_Value *v)
|
||||
return "<null>";
|
||||
return v->vtable->type_name;
|
||||
}
|
||||
/* Default representation by family (no `_repr_` lookup). Caller does
|
||||
* not free the returned bytes. Uses a ring of static buffers so nested
|
||||
* calls (an object's fields) do not clobber each other. */
|
||||
static size_t repr_into(char *out, size_t cap, CAT_VALUE_Value *v,
|
||||
int quote_text);
|
||||
|
||||
/* Resolve `_repr_` if present: returns the value to format (a text
|
||||
* result from a callable/value `_repr_`), or `v` itself otherwise.
|
||||
* Never returns NULL. */
|
||||
static CAT_VALUE_Value *repr_resolve(CAT_VALUE_Value *v)
|
||||
{
|
||||
CAT_ERROR_Error e;
|
||||
CAT_VALUE_Value *r = NULL;
|
||||
|
||||
if (v == NULL)
|
||||
return NULL;
|
||||
cat_error_clear(&e);
|
||||
if (cat_value_get_attribute(v, "_repr_", &r, &e) == 0 && r != NULL &&
|
||||
r != v) {
|
||||
if (r->vtable != NULL &&
|
||||
r->vtable->kind == CAT_VALUE_KIND_FUNCTION) {
|
||||
CAT_VALUE_Value *out = NULL;
|
||||
CAT_VALUE_Value *callargs[1];
|
||||
callargs[0] = v;
|
||||
cat_error_clear(&e);
|
||||
if (cat_value_call(r, callargs, 1, &out, &e) == 0 &&
|
||||
out != NULL && out != v)
|
||||
return out;
|
||||
return NULL;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
const char *cat_value_repr_text(CAT_VALUE_Value *v)
|
||||
{
|
||||
static char ring[8][512];
|
||||
static int slot = 0;
|
||||
char *buf = ring[slot];
|
||||
|
||||
slot = (slot + 1) % 8;
|
||||
repr_into(buf, sizeof(ring[0]), v, 1);
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* Display form (no quoting of text): what `tostring` produces. */
|
||||
const char *cat_value_str_text(CAT_VALUE_Value *v)
|
||||
{
|
||||
static char ring[8][512];
|
||||
static int slot = 0;
|
||||
char *buf = ring[slot];
|
||||
|
||||
slot = (slot + 1) % 8;
|
||||
repr_into(buf, sizeof(ring[0]), v, 0);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static size_t repr_into(char *out, size_t cap, CAT_VALUE_Value *v,
|
||||
int quote_text)
|
||||
{
|
||||
if (cap == 0)
|
||||
return 0;
|
||||
/* Honour `_repr_` first: its text result is used verbatim (no
|
||||
* quotes); a non-text result is formatted normally. */
|
||||
if (v != NULL) {
|
||||
CAT_VALUE_Value *r = repr_resolve(v);
|
||||
if (r != NULL && r != v) {
|
||||
if (r->vtable != NULL &&
|
||||
r->vtable->kind == CAT_VALUE_KIND_TEXT) {
|
||||
snprintf(out, cap, "%s", cat_types_text_bytes(r));
|
||||
return strlen(out);
|
||||
}
|
||||
v = r;
|
||||
} else if (r == NULL) {
|
||||
out[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (v == NULL || v == cat_types_empty()) {
|
||||
snprintf(out, cap, "empty");
|
||||
return strlen(out);
|
||||
}
|
||||
if (v->vtable != NULL && v->vtable->kind == CAT_VALUE_KIND_TEXT) {
|
||||
if (quote_text)
|
||||
snprintf(out, cap, "\"%s\"", cat_types_text_bytes(v));
|
||||
else
|
||||
snprintf(out, cap, "%s", cat_types_text_bytes(v));
|
||||
return strlen(out);
|
||||
}
|
||||
if (v->vtable != NULL && v->vtable->kind == CAT_VALUE_KIND_NUMBER) {
|
||||
if (((CAT_TYPES_NUMBER_Number *)v)->is_float)
|
||||
snprintf(out, cap, "%g", cat_types_number_as_double(v));
|
||||
else
|
||||
snprintf(out, cap, "%lld", cat_types_number_as_int(v));
|
||||
return strlen(out);
|
||||
}
|
||||
if (v->vtable != NULL && v->vtable->kind == CAT_VALUE_KIND_BOOL) {
|
||||
snprintf(out, cap, "%s", ((CAT_TYPES_BOOL_Bool *)v)->v
|
||||
? "true" : "false");
|
||||
return strlen(out);
|
||||
}
|
||||
if (cat_types_list_is(v)) {
|
||||
int i, n = cat_types_list_count(v);
|
||||
size_t off = 0;
|
||||
if (off < cap - 1) out[off++] = '[';
|
||||
for (i = 0; i < n && off < cap - 24; i++) {
|
||||
char item[256];
|
||||
repr_into(item, sizeof(item), cat_types_list_at(v, i), quote_text);
|
||||
int w = snprintf(out + off, cap - off, "%s%s",
|
||||
i ? ", " : "", item);
|
||||
if (w > 0) off += (size_t)w;
|
||||
}
|
||||
if (off < cap - 1) out[off++] = ']';
|
||||
out[off] = '\0';
|
||||
return off;
|
||||
}
|
||||
if (v->vtable != NULL && v->vtable == cat_types_table_vtable()) {
|
||||
int i, n = cat_types_table_count(v);
|
||||
size_t off = 0;
|
||||
if (off < cap - 1) out[off++] = '{';
|
||||
for (i = 0; i < n && off < cap - 40; i++) {
|
||||
char item[256];
|
||||
repr_into(item, sizeof(item), cat_types_table_value_at(v, i),
|
||||
quote_text);
|
||||
int w = snprintf(out + off, cap - off, "%s%s = %s",
|
||||
i ? ", " : "", cat_types_table_key_at(v, i), item);
|
||||
if (w > 0) off += (size_t)w;
|
||||
}
|
||||
if (off < cap - 1) out[off++] = '}';
|
||||
out[off] = '\0';
|
||||
return off;
|
||||
}
|
||||
snprintf(out, cap, "<%s>", cat_value_type_name(v));
|
||||
return strlen(out);
|
||||
}
|
||||
|
||||
/* The textual representation of a value, as an ascii_text value:
|
||||
* the repr form (text quoted). Honours `_repr_`. */
|
||||
CAT_VALUE_Value *cat_value_repr(CAT_VALUE_Value *v)
|
||||
{
|
||||
return cat_types_ascii_text_new(cat_value_repr_text(v));
|
||||
}
|
||||
|
||||
static CAT_VALUE_Vtable *vt(CAT_VALUE_Value *v)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user