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
+2
-1
@@ -83,7 +83,8 @@ static const char *op_name(unsigned char op)
|
||||
"NEG", "NOT", "EQ", "NE", "LT", "LE", "GT", "GE", "GETATTR",
|
||||
"SETATTR", "NEWTABLE", "CALL", "CALLMETHOD", "JUMP", "JUMPIF",
|
||||
"JUMPIFNOT", "RETURN", "RETURN0", "POP", "PRINT", "CLOSURE",
|
||||
"GETUPVAL", "CONCAT", "DUP", "GETIDX", "SETIDX"
|
||||
"GETUPVAL", "CONCAT", "DUP", "GETIDX", "SETIDX", "YIELD", "NEXT",
|
||||
"TRYSETUP", "TRYEND", "RAISE", "FORNEXT", "PRINTEXPR"
|
||||
};
|
||||
if (op < sizeof(names) / sizeof(names[0]))
|
||||
return names[op];
|
||||
|
||||
+10
-1
@@ -55,6 +55,7 @@ typedef struct {
|
||||
int brk_ends[16][64];
|
||||
int brk_n[16];
|
||||
int nloops;
|
||||
int repl; /* REPL mode: a lone expression is echoed */
|
||||
} Compiler;
|
||||
|
||||
static void err_at(Compiler *c, int line, const char *fmt, ...)
|
||||
@@ -985,7 +986,8 @@ static void stat(Compiler *c)
|
||||
/* FALLTHROUGH */
|
||||
default:
|
||||
expr(c);
|
||||
emit(c, CAT_BYTECODE_OP_POP, 0, 0, line);
|
||||
emit(c, c->repl ? CAT_BYTECODE_OP_PRINTEXPR : CAT_BYTECODE_OP_POP,
|
||||
0, 0, line);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1016,12 +1018,19 @@ static void block(Compiler *c)
|
||||
|
||||
CAT_BYTECODE_Function *cat_compiler_compile(const char *source,
|
||||
CAT_ERROR_Error *err)
|
||||
{
|
||||
return cat_compiler_compile_mode(source, err, 0);
|
||||
}
|
||||
|
||||
CAT_BYTECODE_Function *cat_compiler_compile_mode(const char *source,
|
||||
CAT_ERROR_Error *err, int repl)
|
||||
{
|
||||
Compiler c;
|
||||
CAT_BYTECODE_Function *fn;
|
||||
|
||||
memset(&c, 0, sizeof(c));
|
||||
c.err = err;
|
||||
c.repl = repl;
|
||||
cat_lexer_init(&c.lx, source);
|
||||
fn = cat_bytecode_function_new("main");
|
||||
memset(&c.stack[0], 0, sizeof(c.stack[0]));
|
||||
|
||||
@@ -71,7 +71,10 @@ static int lf_tostring(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
(void)vm;
|
||||
*out = cat_types_ascii_text_new(nargs > 0 ? value_to_text(args[0]) : "");
|
||||
/* `tostring` is the display form (no quotes); the REPL echo uses the
|
||||
* repr form. Both honour `_repr_`. */
|
||||
*out = (nargs > 0) ? cat_types_ascii_text_new(cat_value_str_text(args[0]))
|
||||
: cat_types_ascii_text_new("");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -575,7 +578,7 @@ int cat_stdlib_stdlib_install(CAT_VM_State *vm)
|
||||
int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int, CAT_VALUE_Value **);
|
||||
const char *doc; } std[] = {
|
||||
{ "print", lf_print, "print(a, ...) -- escreve os argumentos em stdout." },
|
||||
{ "tostring", lf_tostring, "tostring(v) -- representa\u00e7\u00e3o textual de v." },
|
||||
{ "tostring", lf_tostring, "tostring(v) -- representa\u00e7\u00e3o textual de v (usa `_repr_` se houver)." },
|
||||
{ "tonumber", lf_tonumber, "tonumber(s) -- converte texto em n\u00famero (ou vazio se inv\u00e1lido)." },
|
||||
{ "type", lf_type, "type(v) -- nome do tipo de v." },
|
||||
{ "len", lf_len, "len(v) -- tamanho de texto ou n\u00famero de membros de um objeto." },
|
||||
|
||||
+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)
|
||||
{
|
||||
|
||||
@@ -468,6 +468,18 @@ int cat_vm_frame_run(CAT_VM_State *vm, CAT_VM_Frame *f, CAT_VALUE_Value **out)
|
||||
print_value(v);
|
||||
break;
|
||||
}
|
||||
case CAT_BYTECODE_OP_PRINTEXPR: {
|
||||
/* REPL echo: show the value's representation. A call that
|
||||
* yields no value (prints, mutators) returns the empty
|
||||
* object, which is not echoed. */
|
||||
CAT_VALUE_Value *v = stack[--sp];
|
||||
CAT_VALUE_Value *s;
|
||||
if (v == NULL || cat_types_empty_is(v))
|
||||
break;
|
||||
s = cat_value_repr(v);
|
||||
printf("%s\n", cat_types_text_bytes(s));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user