apfl/webpage/playground/playground.c
Laria Carolin Chabowski c5fbd92f75 Various cleanup tasks, mostly simplifying the REPL
Instead of passing a flag through the parser and tokenizer for telling
the input source if we need further input or not, we steal a trick from
Lua: In the REPL, we just continue to read lines and append them to the
input, until the input was loaded with no "unexpected EOF" error. After
all, when we didn't expect an EOF is exactly the scenario, when we need
more input.

Doing things this way simplifies a bunch of places and lets us remove
the ugly source_reader and iterative_runner concepts.

To allow the REPL to see the error that happened during loading required
some smaller refactorings, but those were honestly for the better
anyway.

I also decided to get rid of the token_source concept, the parser now
gets the tokenizer directly. This also made things a bit simpler, also
I want to soon-ish implement string interpolation, and for that the
parser needs to do more with the tokenizer than just reading the next
token.

One last thing: This also cleans up the web playground and makes the
playground and REPL share a bunch of code. Nice!
2025-11-30 22:23:40 +01:00

39 lines
941 B
C

#include <emscripten.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "../../src/repl.h"
EM_JS(void, web_writer, (int error, int handle, const unsigned char* str, int size), {
window.playground_write(handle, !!error, UTF8ToString(str, size));
});
static bool
web_fmt_write_ok(void *opaque, const unsigned char *buf, size_t len)
{
web_writer(false, (int)opaque, buf, len);
return true;
}
static bool
web_fmt_write_err(void *opaque, const unsigned char *buf, size_t len)
{
web_writer(true, (int)opaque, buf, len);
return true;
}
repl
repl_new_for_playground(void)
{
repl r = repl_new(apfl_allocator_default());
if (r == NULL) {
return NULL;
}
repl_set_w_out(r, (struct apfl_io_writer){.write = web_fmt_write_ok, .opaque = (void *)r});
repl_set_w_err(r, (struct apfl_io_writer){.write = web_fmt_write_err, .opaque = (void *)r});
return r;
}