summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorLLLL Colonq <llll@colonq>2025-09-27 04:21:06 -0400
committerLLLL Colonq <llll@colonq>2025-09-27 04:21:06 -0400
commitab57bc2a6d6ea9c24aa119df6efbd8a38b54c312 (patch)
treebfc7fe32f40804e7808016038a3f2f15ef9e643e /src/main.c
parent811f11463851a0f35835ac72ef09b65b1072de20 (diff)
Source location tracking
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c54
1 files changed, 41 insertions, 13 deletions
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 <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);
+ }
+ }
}
+
}