diff options
| author | LLLL Colonq <llll@colonq> | 2025-09-27 04:21:06 -0400 |
|---|---|---|
| committer | LLLL Colonq <llll@colonq> | 2025-09-27 04:21:06 -0400 |
| commit | ab57bc2a6d6ea9c24aa119df6efbd8a38b54c312 (patch) | |
| tree | bfc7fe32f40804e7808016038a3f2f15ef9e643e /src/main.c | |
| parent | 811f11463851a0f35835ac72ef09b65b1072de20 (diff) | |
Source location tracking
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 54 |
1 files changed, 41 insertions, 13 deletions
@@ -1,3 +1,4 @@ +#include <stdlib.h> #include <stdio.h> #include "utils.h" @@ -6,19 +7,46 @@ #include "runtime.h" int main(int argc, char **argv) { - if (argc < 2) pit_panic("usage: %s FILE", argv[0]); - pit_runtime *rt = pit_runtime_new(); - pit_check_error_maybe_panic(rt); - - pit_lexer *lex = pit_lex_file(argv[1]); - pit_parser *parse = pit_parser_from_lexer(lex); - - bool eof = false; - while (!eof) { - pit_value program = pit_parse(rt, parse, &eof); - pit_check_error_maybe_panic(rt); - pit_eval(rt, program); - pit_check_error_maybe_panic(rt); + if (argc < 2) { // run repl + pit_runtime_freeze(rt); + setbuf(stdout, NULL); + printf("> "); + char buf[1024] = {0}; + i64 len = 0; + while (len < (i64) sizeof(buf) && (buf[len++] = getchar()) != EOF) { + if (buf[len - 1] == '\n') { + buf[len - 1] = 0; + pit_value bs = pit_bytes_new_cstr(rt, buf); + pit_value prog = pit_read_bytes(rt, bs); + pit_value 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; + if (!pit_lexer_from_bytes(rt, &lex, bs)) { + pit_error(rt, "failed to initialize lexer"); + } + pit_parser parse; + pit_parser_from_lexer(&parse, &lex); + bool eof = false; + pit_value p = PIT_NIL; + while (p = pit_parse(rt, &parse, &eof), !eof) { + pit_eval(rt, p); + if (pit_runtime_print_error(rt)) { + exit(1); + } + } } + } |
