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:
Minus committed 2026-11-17 13:14:05 +00:00
1 parent c7a87d05e2
commit de847e7793
11 files changed
+268 -5

No files matched your search

+10 -1
View File
@@ -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]));