1
0
Fork 0
forked from fun/fun

Opcode cleanup. (0.41.3)

This commit is contained in:
Johannes Findeisen 2026-04-27 03:17:31 +02:00
commit be5f2b1981
6 changed files with 31 additions and 421 deletions

View file

@ -309,20 +309,6 @@ static const char *opcode_name(OpCode op) {
return "XML_NAME";
case OP_XML_TEXT:
return "XML_TEXT";
case OP_TK_EVAL:
return "TK_EVAL";
case OP_TK_RESULT:
return "TK_RESULT";
case OP_TK_LOOP:
return "TK_LOOP";
case OP_TK_WM_TITLE:
return "TK_WM_TITLE";
case OP_TK_LABEL:
return "TK_LABEL";
case OP_TK_BUTTON:
return "TK_BUTTON";
case OP_TK_PACK:
return "TK_PACK";
case OP_FLOOR:
return "FLOOR";
case OP_CEIL:

View file

@ -221,9 +221,6 @@ typedef enum {
// OS additions
OP_OS_LIST_DIR, // pops path string; pushes array of strings
// Tk additions
OP_TK_BIND, // pops command, event, id; binds event to command
// Serial communication (termios)
OP_SERIAL_OPEN, // pops baud_rate (int), path (string); returns fd (int) or 0
OP_SERIAL_CONFIG, // pops flow_control, stop_bits, parity, data_bits, fd; returns 1/0
@ -231,15 +228,6 @@ typedef enum {
OP_SERIAL_RECV, // pops maxlen (int), fd; returns data (string)
OP_SERIAL_CLOSE, // pops fd; returns 1/0
// Tk (Tcl/Tk) optional minimal API
OP_TK_EVAL, // pops script string; pushes int rc (0 = OK)
OP_TK_RESULT, // pushes string: last Tcl result
OP_TK_LOOP, // enters Tk event loop; pushes Nil when done
OP_TK_WM_TITLE, // pops title string; sets window title; pushes rc
OP_TK_LABEL, // pops text, id; creates/updates label .id; pushes rc
OP_TK_BUTTON, // pops text, id; creates/updates button .id; pushes rc
OP_TK_PACK, // pops id; packs .id; pushes rc
// exceptions (minimal)
OP_TRY_PUSH, // operand = handler ip; push handler onto try-stack
OP_TRY_POP, // pop current handler
@ -279,21 +267,6 @@ typedef enum {
// C++ demo opcode(s)
OP_CPP_ADD, // pops b, a; pushes (a + b)
/* Notcurses TUI (optional) */
OP_NC_INIT, // initializes Notcurses; returns 1 on success, 0 on failure
OP_NC_SHUTDOWN, // shuts down Notcurses; returns 0
OP_NC_CLEAR, // clears screen/plane; returns 0
OP_NC_DRAW_TEXT, // pops text, x, y; draws; returns 0
OP_NC_GETCH, // pops timeout_ms; returns codepoint or -1 on timeout/error
OP_NC_GET_SIZE, // pushes [rows:int, cols:int] or -1 when unavailable
OP_NC_SET_STYLE, // pops style:int, bg_rgb:int, fg_rgb:int; returns 0/-1
OP_NC_DRAW_CHAR, // pops ch:int, x:int, y:int; returns 0/-1
OP_NC_DRAW_HLINE,// pops len:int, x:int, y:int, ch:int; returns 0/-1
OP_NC_DRAW_VLINE,// pops len:int, x:int, y:int, ch:int; returns 0/-1
OP_NC_BOX, // pops x:int, y:int, w:int, h:int, style:int; returns 0/-1
OP_NC_FILL, // pops x:int, y:int, w:int, h:int, ch:int; returns 0/-1
OP_NC_RENDER // forces a render; returns 0/-1
} OpCode;
typedef struct {

View file

@ -1461,165 +1461,6 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
free(name);
return 1;
}
/* Tk (GUI) builtins (no raw Tcl exposed) */
if (strcmp(name, "tk_loop") == 0) {
(*pos)++; /* '(' */
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "tk_loop expects ()");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_TK_LOOP, 0);
free(name);
return 1;
}
if (strcmp(name, "tk_title") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) {
parser_fail(*pos, "tk_title expects (title:string)");
free(name);
return 0;
}
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "Expected ')' after tk_title arg");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_TK_WM_TITLE, 0);
free(name);
return 1;
}
if (strcmp(name, "tk_label") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) {
parser_fail(*pos, "tk_label expects (id:string, text:string)");
free(name);
return 0;
}
if (!consume_char(src, len, pos, ',')) {
parser_fail(*pos, "tk_label expects 2 args");
free(name);
return 0;
}
if (!emit_expression(bc, src, len, pos)) {
parser_fail(*pos, "tk_label expects (id:string, text:string)");
free(name);
return 0;
}
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "Expected ')' after tk_label args");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_TK_LABEL, 0);
free(name);
return 1;
}
if (strcmp(name, "tk_button") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) {
parser_fail(*pos, "tk_button expects (id:string, text:string)");
free(name);
return 0;
}
if (!consume_char(src, len, pos, ',')) {
parser_fail(*pos, "tk_button expects 2 args");
free(name);
return 0;
}
if (!emit_expression(bc, src, len, pos)) {
parser_fail(*pos, "tk_button expects (id:string, text:string)");
free(name);
return 0;
}
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "Expected ')' after tk_button args");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_TK_BUTTON, 0);
free(name);
return 1;
}
if (strcmp(name, "tk_pack") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) {
parser_fail(*pos, "tk_pack expects (id:string)");
free(name);
return 0;
}
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "Expected ')' after tk_pack arg");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_TK_PACK, 0);
free(name);
return 1;
}
if (strcmp(name, "tk_bind") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) {
parser_fail(*pos, "tk_bind expects (id, event, cmd)");
free(name);
return 0;
}
if (!consume_char(src, len, pos, ',')) {
parser_fail(*pos, "tk_bind expects 3 args");
free(name);
return 0;
}
if (!emit_expression(bc, src, len, pos)) {
parser_fail(*pos, "tk_bind expects 3 args");
free(name);
return 0;
}
if (!consume_char(src, len, pos, ',')) {
parser_fail(*pos, "tk_bind expects 3 args");
free(name);
return 0;
}
if (!emit_expression(bc, src, len, pos)) {
parser_fail(*pos, "tk_bind expects 3 args");
free(name);
return 0;
}
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "Expected ')' after tk_bind args");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_TK_BIND, 0);
free(name);
return 1;
}
if (strcmp(name, "tk_eval") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) {
parser_fail(*pos, "tk_eval expects (script)");
free(name);
return 0;
}
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "Expected ')' after tk_eval arg");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_TK_EVAL, 0);
free(name);
return 1;
}
if (strcmp(name, "tk_result") == 0) {
(*pos)++; /* '(' */
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "tk_result expects ()");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_TK_RESULT, 0);
free(name);
return 1;
}
if (strcmp(name, "json_from_file") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) {
@ -2340,206 +2181,7 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
free(name);
return 1;
}
/* Notcurses builtins (optional) */
if (strcmp(name, "nc_init") == 0) {
(*pos)++; /* '(' */
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "nc_init expects ()");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_NC_INIT, 0);
free(name);
return 1;
}
if (strcmp(name, "nc_shutdown") == 0) {
(*pos)++; /* '(' */
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "nc_shutdown expects ()");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_NC_SHUTDOWN, 0);
free(name);
return 1;
}
if (strcmp(name, "nc_clear") == 0) {
(*pos)++; /* '(' */
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "nc_clear expects ()");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_NC_CLEAR, 0);
free(name);
return 1;
}
if (strcmp(name, "nc_draw_text") == 0) {
(*pos)++; /* '(' */
/* (y, x, text) -> push y, x, text, then opcode will pop text,x,y */
if (!emit_expression(bc, src, len, pos)) {
parser_fail(*pos, "nc_draw_text expects (y, x, text)");
free(name);
return 0;
}
if (!consume_char(src, len, pos, ',')) {
parser_fail(*pos, "nc_draw_text expects (y, x, text)");
free(name);
return 0;
}
if (!emit_expression(bc, src, len, pos)) {
parser_fail(*pos, "nc_draw_text expects (y, x, text)");
free(name);
return 0;
}
if (!consume_char(src, len, pos, ',')) {
parser_fail(*pos, "nc_draw_text expects (y, x, text)");
free(name);
return 0;
}
if (!emit_expression(bc, src, len, pos)) {
parser_fail(*pos, "nc_draw_text expects (y, x, text)");
free(name);
return 0;
}
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "Expected ')' after nc_draw_text args");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_NC_DRAW_TEXT, 0);
free(name);
return 1;
}
if (strcmp(name, "nc_getch") == 0) {
(*pos)++; /* '(' */
/* (timeout_ms) */
if (!emit_expression(bc, src, len, pos)) {
parser_fail(*pos, "nc_getch expects (timeout_ms)");
free(name);
return 0;
}
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "Expected ')' after nc_getch arg");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_NC_GETCH, 0);
free(name);
return 1;
}
if (strcmp(name, "nc_get_size") == 0) {
(*pos)++; /* '(' */
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "nc_get_size expects ()");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_NC_GET_SIZE, 0);
free(name);
return 1;
}
if (strcmp(name, "nc_set_style") == 0) {
(*pos)++; /* '(' */
/* (style, bg_rgb, fg_rgb) */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_set_style expects (style, bg_rgb, fg_rgb)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_set_style expects (style, bg_rgb, fg_rgb)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_set_style expects (style, bg_rgb, fg_rgb)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_set_style expects (style, bg_rgb, fg_rgb)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_set_style expects (style, bg_rgb, fg_rgb)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after nc_set_style args"); free(name); return 0; }
bytecode_add_instruction(bc, OP_NC_SET_STYLE, 0);
free(name);
return 1;
}
if (strcmp(name, "nc_draw_char") == 0) {
(*pos)++; /* '(' */
/* (y, x, ch) */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_draw_char expects (y, x, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_draw_char expects (y, x, ch)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_draw_char expects (y, x, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_draw_char expects (y, x, ch)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_draw_char expects (y, x, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after nc_draw_char args"); free(name); return 0; }
bytecode_add_instruction(bc, OP_NC_DRAW_CHAR, 0);
free(name);
return 1;
}
if (strcmp(name, "nc_draw_hline") == 0) {
(*pos)++; /* '(' */
/* (y, x, len, ch) */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_draw_hline expects (y, x, len, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_draw_hline expects (y, x, len, ch)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_draw_hline expects (y, x, len, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_draw_hline expects (y, x, len, ch)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_draw_hline expects (y, x, len, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_draw_hline expects (y, x, len, ch)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_draw_hline expects (y, x, len, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after nc_draw_hline args"); free(name); return 0; }
bytecode_add_instruction(bc, OP_NC_DRAW_HLINE, 0);
free(name);
return 1;
}
if (strcmp(name, "nc_draw_vline") == 0) {
(*pos)++; /* '(' */
/* (y, x, len, ch) */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_draw_vline expects (y, x, len, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_draw_vline expects (y, x, len, ch)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_draw_vline expects (y, x, len, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_draw_vline expects (y, x, len, ch)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_draw_vline expects (y, x, len, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_draw_vline expects (y, x, len, ch)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_draw_vline expects (y, x, len, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after nc_draw_vline args"); free(name); return 0; }
bytecode_add_instruction(bc, OP_NC_DRAW_VLINE, 0);
free(name);
return 1;
}
if (strcmp(name, "nc_box") == 0) {
(*pos)++; /* '(' */
/* (x, y, w, h, style) -- wrapper may reorder */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_box expects (x, y, w, h, style)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_box expects (x, y, w, h, style)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_box expects (x, y, w, h, style)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_box expects (x, y, w, h, style)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_box expects (x, y, w, h, style)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_box expects (x, y, w, h, style)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_box expects (x, y, w, h, style)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_box expects (x, y, w, h, style)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_box expects (x, y, w, h, style)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after nc_box args"); free(name); return 0; }
bytecode_add_instruction(bc, OP_NC_BOX, 0);
free(name);
return 1;
}
if (strcmp(name, "nc_fill") == 0) {
(*pos)++; /* '(' */
/* (x, y, w, h, ch) */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_fill expects (x, y, w, h, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_fill expects (x, y, w, h, ch)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_fill expects (x, y, w, h, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_fill expects (x, y, w, h, ch)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_fill expects (x, y, w, h, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_fill expects (x, y, w, h, ch)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_fill expects (x, y, w, h, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "nc_fill expects (x, y, w, h, ch)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "nc_fill expects (x, y, w, h, ch)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after nc_fill args"); free(name); return 0; }
bytecode_add_instruction(bc, OP_NC_FILL, 0);
free(name);
return 1;
}
if (strcmp(name, "nc_render") == 0) {
(*pos)++; /* '(' */
if (!consume_char(src, len, pos, ')')) {
parser_fail(*pos, "nc_render expects ()");
free(name);
return 0;
}
bytecode_add_instruction(bc, OP_NC_RENDER, 0);
free(name);
return 1;
}
/* PCSC builtins (optional) */
if (strcmp(name, "pcsc_release") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) {

View file

@ -66,19 +66,13 @@ static const char *opcode_names[] = {
"FD_SET_NONBLOCK", "FD_POLL_READ", "FD_POLL_WRITE",
"EXIT",
"OS_LIST_DIR",
"TK_BIND",
"SERIAL_OPEN", "SERIAL_CONFIG", "SERIAL_SEND", "SERIAL_RECV", "SERIAL_CLOSE",
"TK_EVAL", "TK_RESULT", "TK_LOOP", "TK_WM_TITLE", "TK_LABEL", "TK_BUTTON", "TK_PACK",
"TRY_PUSH", "TRY_POP", "THROW",
"FMIN", "FMAX",
/* Rust FFI demo */
"RUST_HELLO", "RUST_HELLO_ARGS", "RUST_HELLO_ARGS_RETURN", "RUST_GET_SP", "RUST_SET_EXIT",
/* C++ demo */
"CPP_ADD",
/* Notcurses TUI (optional) */
"NC_INIT", "NC_SHUTDOWN", "NC_CLEAR", "NC_DRAW_TEXT", "NC_GETCH",
"NC_GET_SIZE", "NC_SET_STYLE", "NC_DRAW_CHAR", "NC_DRAW_HLINE", "NC_DRAW_VLINE",
"NC_BOX", "NC_FILL", "NC_RENDER"};
"CPP_ADD"};
typedef struct {
Bytecode *fn;
@ -162,7 +156,7 @@ void vm_debug_request_finish(VM *vm);
void vm_debug_request_continue(VM *vm);
static inline int opcode_is_valid(int op) {
return op >= OP_NOP && op <= OP_NC_RENDER; // all current opcodes (including optional NC_*)
return op >= OP_NOP && op <= OP_CPP_ADD; // all current opcodes
}
/* --- Minimal C ABI helpers for FFI (Rust opcode experiments) --- */