diff options
| author | LLLL Colonq <llll@colonq> | 2026-02-24 18:53:54 -0500 |
|---|---|---|
| committer | LLLL Colonq <llll@colonq> | 2026-02-24 18:53:54 -0500 |
| commit | a525fadf516bc5aae2c0ec648d3b8c22e9f86293 (patch) | |
| tree | 79f585f2ab2130c001529fbf46d88f9196336b25 /include | |
| parent | 2b47c650a161fe2c2c4c7f4d74a19c2c6fe6021e (diff) | |
Add PIT_DEFAULT_MAIN
Diffstat (limited to 'include')
| -rw-r--r-- | include/lcq/pit/runtime.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/include/lcq/pit/runtime.h b/include/lcq/pit/runtime.h index 41ca3ec..02a2249 100644 --- a/include/lcq/pit/runtime.h +++ b/include/lcq/pit/runtime.h @@ -224,4 +224,50 @@ void *pit_nativedata_get(pit_runtime *rt, pit_value tag, pit_value v); pit_value pit_expand_macros(pit_runtime *rt, pit_value top); pit_value pit_eval(pit_runtime *rt, pit_value e); +/* repl / file loading */ +#define PIT_DEFAULT_MAIN(rt) \ + if (argc < 2) { /* run repl */ \ + char buf[1024] = {0}; \ + i64 len = 0; \ + pit_runtime_freeze(rt); \ + if (pit_runtime_print_error(rt)) { exit(1); } \ + setbuf(stdout, NULL); \ + printf("> "); \ + while (len < (i64) sizeof(buf) && (buf[len++] = (char) getchar()) != EOF) { \ + if (buf[len - 1] == '\n') { \ + pit_value bs, prog, res; \ + buf[len - 1] = 0; \ + bs = pit_bytes_new_cstr(rt, buf); \ + prog = pit_read_bytes(rt, bs); \ + res = pit_eval(rt, prog); \ + if (pit_runtime_print_error(rt)) { \ + rt->error = PIT_NIL; \ + printf("> "); \ + } else { \ + char dumpbuf[1024] = {0}; \ + pit_dump(rt, dumpbuf, sizeof(dumpbuf) - 1, res, true); \ + printf("%s\n> ", dumpbuf); \ + } \ + len = 0; \ + } \ + } \ + } else { /* run file */ \ + pit_value bs = pit_bytes_new_file(rt, argv[1]); \ + pit_lexer lex; \ + pit_parser parse; \ + bool eof = false; \ + pit_value p = PIT_NIL; \ + if (!pit_lexer_from_bytes(rt, &lex, bs)) { \ + pit_error(rt, "failed to initialize lexer"); \ + } \ + pit_parser_from_lexer(&parse, &lex); \ + while (p = pit_parse(rt, &parse, &eof), !eof) { \ + if (pit_runtime_print_error(rt)) exit(1); \ + pit_eval(rt, p); \ + if (pit_runtime_print_error(rt)) exit(1); \ + } \ + if (pit_runtime_print_error(rt)) exit(1); \ + } \ + return 0; + #endif |
