1
0
Fork 0
forked from fun/fun

Some more Tk fun. (0.37.18)

This commit is contained in:
Johannes Findeisen 2025-12-23 23:19:20 +01:00
commit 689a716499
13 changed files with 282 additions and 12 deletions

View file

@ -35,7 +35,7 @@ typedef enum {
OP_EQ, // a == b -> push 1/0
OP_NEQ, // a != b -> push 1/0
OP_POP, // dApache-2.0ard top of stack
OP_POP, // discard top of stack
OP_JUMP, // unconditional jump
OP_JUMP_IF_FALSE, // jump if top of stack is false (0)
@ -196,15 +196,6 @@ typedef enum {
OP_XML_NAME, // pops node handle; pushes string (node name)
OP_XML_TEXT, // pops node handle; pushes string (node text)
// 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
// Sockets (UNIX platforms)
OP_SOCK_TCP_LISTEN, // pops backlog, port; returns listen fd (>0) or 0
OP_SOCK_TCP_ACCEPT, // pops listen fd; returns client fd (>0) or 0
@ -218,6 +209,21 @@ typedef enum {
// process control
OP_EXIT, // pops code (or uses operand) and terminates script with exit code
// 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
// 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

View file

@ -757,6 +757,14 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
free(name);
return 1;
}
if (strcmp(name, "os_list_dir") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "os_list_dir expects (path)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after os_list_dir arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_OS_LIST_DIR, 0);
free(name);
return 1;
}
/* JSON builtins */
if (strcmp(name, "json_parse") == 0) {
(*pos)++; /* '(' */
@ -853,6 +861,33 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
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)) { parser_fail(*pos, "json_from_file expects (path)"); free(name); return 0; }

View file

@ -741,6 +741,7 @@ void vm_run(VM *vm, Bytecode *entry) {
#include "vm/tk/label.c"
#include "vm/tk/button.c"
#include "vm/tk/pack.c"
#include "vm/tk/bind.c"
#endif
/* SQLite ops */
@ -783,6 +784,7 @@ void vm_run(VM *vm, Bytecode *entry) {
#include "vm/typeof.c"
#include "vm/uclamp.c"
#include "vm/sclamp.c"
#include "vm/os/list_dir.c"
default:
if (!opcode_is_valid(inst.op)) {

View file

@ -49,6 +49,9 @@ static const char *opcode_names[] = {
"XML_PARSE","XML_ROOT","XML_NAME","XML_TEXT",
"SOCK_TCP_LISTEN","SOCK_TCP_ACCEPT","SOCK_TCP_CONNECT","SOCK_SEND","SOCK_RECV","SOCK_CLOSE","SOCK_UNIX_LISTEN","SOCK_UNIX_CONNECT",
"EXIT",
"OS_LIST_DIR",
"TK_BIND",
"TK_EVAL","TK_RESULT","TK_LOOP","TK_WM_TITLE","TK_LABEL","TK_BUTTON","TK_PACK",
"TRY_PUSH","TRY_POP","THROW"
};

49
src/vm/os/list_dir.c Normal file
View file

@ -0,0 +1,49 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-12-23
*/
case OP_OS_LIST_DIR: {
/* pops path string; pushes array of strings */
Value pathv = pop_value(vm);
char *path = value_to_string_alloc(&pathv);
free_value(pathv);
Value arr = make_array_from_values(NULL, 0);
if (path) {
/*
* Using 'ls -1' as a fallback to avoid dirent.h conflicts on some systems.
* We escape the path minimally for the shell.
*/
size_t slen = strlen(path) + 16;
char *cmd = (char*)malloc(slen);
if (cmd) {
snprintf(cmd, slen, "ls -1 \"%s\"", path);
FILE *fp = popen(cmd, "r");
if (fp) {
char line[1024];
while (fgets(line, sizeof(line), fp)) {
/* Strip trailing newline */
size_t l = strlen(line);
if (l > 0 && line[l-1] == '\n') line[l-1] = '\0';
if (l > 1 && line[l-2] == '\r') line[l-2] = '\0';
if (line[0] != '\0') {
array_push(&arr, make_string(line));
}
}
pclose(fp);
}
free(cmd);
}
free(path);
}
push_value(vm, arr);
break;
}

54
src/vm/tk/bind.c Normal file
View file

@ -0,0 +1,54 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-12-23
*/
/* TK_BIND */
case OP_TK_BIND: {
/* stack: ..., id, event, command -> rc */
Value cmdv = pop_value(vm);
Value eventv = pop_value(vm);
Value idv = pop_value(vm);
char *cmd = value_to_string_alloc(&cmdv);
char *event = value_to_string_alloc(&eventv);
char *id = value_to_string_alloc(&idv);
free_value(cmdv);
free_value(eventv);
free_value(idv);
if (!id || !event || !cmd) {
if (id) free(id);
if (event) free(event);
if (cmd) free(cmd);
push_value(vm, make_int(-1));
break;
}
/*
* Construct: bind .id <event> {command}
* Note: for now, command is just raw Tcl as well,
* but could be extended to call Fun functions if we had a callback mechanism.
*/
size_t slen = strlen(id) + strlen(event) + strlen(cmd) + 32;
char *script = (char*)malloc(slen);
if (!script) {
free(id); free(event); free(cmd);
push_value(vm, make_int(-1));
break;
}
snprintf(script, slen, "bind .%s %s {%s}", id, event, cmd);
int rc = fun_tk_eval_script(script);
free(script);
free(id);
free(event);
free(cmd);
push_value(vm, make_int(rc));
break;
}