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
@@ -64,6 +64,7 @@ enum CAT_BYTECODE_Op {
|
||||
CAT_BYTECODE_OP_TRYEND, /* pop the innermost handler */
|
||||
CAT_BYTECODE_OP_RAISE, /* pop value -> raise it */
|
||||
CAT_BYTECODE_OP_FORNEXT, /* A: exit target; pop it -> push next or jump */
|
||||
CAT_BYTECODE_OP_PRINTEXPR, /* pop, print its repr (REPL echo) */
|
||||
};
|
||||
|
||||
/* One instruction: opcode + three integer operands. */
|
||||
|
||||
@@ -17,4 +17,9 @@
|
||||
CAT_BYTECODE_Function *cat_compiler_compile(const char *source,
|
||||
CAT_ERROR_Error *err);
|
||||
|
||||
/* Like cat_compiler_compile, but a lone expression statement is echoed
|
||||
* (emits PRINTEXPR). Used by the REPL. */
|
||||
CAT_BYTECODE_Function *cat_compiler_compile_mode(const char *source,
|
||||
CAT_ERROR_Error *err, int repl);
|
||||
|
||||
#endif /* CAT_COMPILER_H */
|
||||
@@ -195,4 +195,12 @@ int cat_value_slot_set(CAT_VALUE_Value *v, const CAT_VALUE_Slot *s,
|
||||
|
||||
const char *cat_value_type_name(CAT_VALUE_Value *v);
|
||||
|
||||
/* Textual representation. `cat_value_repr` honours a `_repr_` member
|
||||
* (callable or value); `cat_value_repr_text` is the default by family.
|
||||
* The bytes from repr_text are static; repr returns an ascii_text value. */
|
||||
CAT_VALUE_Value *cat_value_repr(CAT_VALUE_Value *v);
|
||||
const char *cat_value_repr_text(CAT_VALUE_Value *v);
|
||||
/* Display form: like repr_text but text values are not quoted. */
|
||||
const char *cat_value_str_text(CAT_VALUE_Value *v);
|
||||
|
||||
#endif /* CAT_VALUE_H */
|
||||
Reference in new issue
Block a user