all repos — hex @ 0e251b925db4fdd6834213b960626232efa12a16

A tiny, minimalist, slightly-esoteric concatenative programming lannguage.

Implemented support for fgets for emscripten with node.
h3rald h3rald@h3rald.com
Sat, 07 Dec 2024 16:47:27 +0100
commit

0e251b925db4fdd6834213b960626232efa12a16

parent

962d30572dded1d47be20f603a7ca8ba447c223a

3 files changed, 38 insertions(+), 11 deletions(-)

jump to
M MakefileMakefile

@@ -6,10 +6,10 @@ hex: hex.c

$(CC) $(CFLAGS) $(LDFLAGS) $< -o hex web/assets/hex.wasm: hex.c - emcc -O2 -sASYNCIFY -DBROWSER -sEXPORTED_RUNTIME_METHODS=stringToUTF8 hex.c -o web/assets/hex.js --pre-js web/assets/hex-playground.js + emcc -O2 -sASYNCIFY -sEXPORTED_RUNTIME_METHODS=stringToUTF8 hex.c -o web/assets/hex.js --pre-js web/assets/hex-playground.js hex.wasm: hex.c - emcc -O2 hex.c -o hex.js + emcc -O2 -sASYNCIFY -sEXPORTED_RUNTIME_METHODS=stringToUTF8 hex.c -o hex.js --pre-js hex.node.js ape: hex.c cosmocc $(CFLAGS) $(LDFLAGS) $< -o hex
M hex.chex.c

@@ -1,6 +1,6 @@

#include "hex.h" -#if defined(__EMSCRIPTEN__) && defined(BROWSER) +#if defined(__EMSCRIPTEN__) #include <emscripten.h> EM_ASYNC_JS(char *, em_fgets, (const char *buf, size_t bufsize), {

@@ -1272,7 +1272,7 @@ {

char input[HEX_STDIN_BUFFER_SIZE]; // Buffer to store the input (adjust size if needed) char *p = input; -#if defined(__EMSCRIPTEN__) && defined(BROWSER) +#if defined(__EMSCRIPTEN__) p = em_fgets(input, 1024); #else p = fgets(input, sizeof(input), stdin);

@@ -3883,13 +3883,26 @@

fclose(file); return content; } -#if defined(__EMSCRIPTEN__) && defined(BROWSER) + +#if defined(BROWSER) +static void prompt() +{ + // no prompt needed on browser +} +#else +static void prompt() +{ + printf("> "); // Prompt + fflush(stdout); +} +#endif +#if defined(__EMSCRIPTEN__) static void do_repl(void *v_ctx) { hex_context_t *ctx = (hex_context_t *)v_ctx; + prompt(); char line[1024]; - char *p = line; p = em_fgets(line, 1024); if (!p)

@@ -3918,8 +3931,7 @@ static int do_repl(void *v_ctx)

{ hex_context_t *ctx = (hex_context_t *)v_ctx; char line[1024]; - printf("> "); // Prompt - fflush(stdout); + prompt(); if (fgets(line, sizeof(line), stdin) == NULL) { printf("\n"); // Handle EOF (Ctrl+D)

@@ -3945,12 +3957,11 @@

// REPL implementation void hex_repl(hex_context_t *ctx) { -#if defined(__EMSCRIPTEN__) && defined(BROWSER) +#if defined(__EMSCRIPTEN__) printf(" _*_ _\n"); printf(" / \\hex\\*\n"); printf(" *\\_/_/_/ v%s - WASM Build\n", HEX_VERSION); printf(" *\n"); - int fps = 0; int simulate_infinite_loop = 1; emscripten_set_main_loop_arg(do_repl, ctx, fps, simulate_infinite_loop);

@@ -4114,7 +4125,7 @@ return 0;

} } } -#if !(__EMSCRIPTEN__) && !(BROWSER) +#if !(__EMSCRIPTEN__) if (!isatty(fileno(stdin))) { ctx.settings.stack_trace_enabled = 0;
A hex.node.js

@@ -0,0 +1,16 @@

+const readline = require('readline'); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +Module.pending_fgets = []; +Module.pending_lines = []; + +rl.on('line', (line) => { + Module.pending_lines.push(line); + if (Module.pending_fgets.length > 0 && Module.pending_lines.length > 0) { + const resolver = Module.pending_fgets.shift(); + resolver(Module.pending_lines.shift()); + } +});