From ab57bc2a6d6ea9c24aa119df6efbd8a38b54c312 Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Sat, 27 Sep 2025 04:21:06 -0400 Subject: Source location tracking --- src/main.c | 54 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 13 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 071cf7e..ef57b81 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#include #include #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); + } + } } + } -- cgit v1.2.3