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
@@ -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." },
|
||||
|
||||
Reference in new issue
Block a user