From 76171dbdee09c6ffe81a7918fad681fa0bf4db36 Mon Sep 17 00:00:00 2001 From: Gabriel Pariat Date: Sun, 20 Feb 2022 12:53:37 -0500 Subject: [PATCH] repl! --- implementations/c/src/lisp.c | 14 +++++++++++--- implementations/c/src/lisp.h | 1 + implementations/c/src/main.c | 1 - implementations/c/src/reader.c | 18 +++++++++--------- implementations/c/tests/check_ptlisp.log | 5 ----- implementations/c/tests/ptlisp-test.c | 2 +- 6 files changed, 22 insertions(+), 19 deletions(-) diff --git a/implementations/c/src/lisp.c b/implementations/c/src/lisp.c index a9e4e96..c346dc9 100644 --- a/implementations/c/src/lisp.c +++ b/implementations/c/src/lisp.c @@ -198,6 +198,11 @@ Pointer fn_fn(Pointer args, Pointer env) { return func(args, env); } +Pointer exit_fn(Pointer args, Pointer env) { + (void) args; (void) env; + exit(1); +} + #define SET_FUNC(s, fn) environment_set(NIL, symbol1(s), native_func(fn)) #define SET_FORM(s, fn) environment_set(NIL, symbol1(s), special_form(fn)) @@ -226,6 +231,7 @@ void init(void) { SET_FUNC("~", lognot_fn); SET_FUNC("peek-char", peek_char_fn); SET_FUNC("read-char", read_char_fn); + SET_FUNC("exit", exit_fn); SET_FORM("if", if_fn); SET_FORM("let", let_fn); @@ -238,7 +244,9 @@ void init(void) { } void repl(void) { - Pointer readed = read_fn(NIL, NIL); - print(eval(readed, NIL), stdout); - printf("\n"); + while(true) { + printf(">>> "); + print(eval(read_fn(NIL, NIL), NIL), stdout); + printf("\n"); + } } diff --git a/implementations/c/src/lisp.h b/implementations/c/src/lisp.h index 2953db8..6e1b21b 100644 --- a/implementations/c/src/lisp.h +++ b/implementations/c/src/lisp.h @@ -195,6 +195,7 @@ Pointer read_fn(Pointer args, Pointer env); Pointer set_reader_macro_fn(Pointer args, Pointer env); Pointer print_fn(Pointer args, Pointer env); Pointer not_fn(Pointer args, Pointer env); +Pointer exit_fn(Pointer args, Pointer env); /* SPECIAL FORMS */ Pointer if_fn(Pointer args, Pointer env); diff --git a/implementations/c/src/main.c b/implementations/c/src/main.c index 175fecf..da1f976 100644 --- a/implementations/c/src/main.c +++ b/implementations/c/src/main.c @@ -3,7 +3,6 @@ #include int main() { - printf(">>> "); init(); repl(); return 0; diff --git a/implementations/c/src/reader.c b/implementations/c/src/reader.c index 590e8ec..4cc365d 100644 --- a/implementations/c/src/reader.c +++ b/implementations/c/src/reader.c @@ -11,24 +11,24 @@ Pointer character(Char c) { } unsigned get_utf8(FILE* s) { - unsigned c = getc(s); + unsigned c = fgetc(s); if (c & 0x80 && c & 0x40) { switch(c >> 3 & 0x07) { case 0: case 1: case 2: case 3: - c |= getc(s) << 8; + c |= fgetc(s) << 8; break; case 4: case 5: - c |= getc(s) << 8; - c |= getc(s) << 16; + c |= fgetc(s) << 8; + c |= fgetc(s) << 16; break; case 6: - c |= getc(s) << 8; - c |= getc(s) << 16; - c |= getc(s) << 24; + c |= fgetc(s) << 8; + c |= fgetc(s) << 16; + c |= fgetc(s) << 24; break; default: break; } @@ -59,12 +59,12 @@ Char peek_char(Pointer type, Stream stream) { return unget_utf8(get_utf8(stream), stream); } if (type == T) { - while(!feof(stream) && (c = get_utf8(stream)) == ' '); + while(isspace((char) (c = get_utf8(stream))) && !feof(stream)); if (feof(stream)) return 0; return unget_utf8(c, stream); } if (TYPE(type) == CHAR) { - while(!feof(stream) && (c = get_utf8(stream)) != CHAR(type)); + while((c = get_utf8(stream)) != CHAR(type) && !feof(stream)); if (feof(stream)) return 0; return unget_utf8(c, stream); } diff --git a/implementations/c/tests/check_ptlisp.log b/implementations/c/tests/check_ptlisp.log index 933bc4d..7673696 100644 --- a/implementations/c/tests/check_ptlisp.log +++ b/implementations/c/tests/check_ptlisp.log @@ -8,11 +8,6 @@ Running suite(s): Memory Evaluator Lisp Reader -reader-test.c: 29 -reader-test.c: 32 -reader-test.c: 34 -reader-test.c: 40 -reader-test.c: 46: 2 Printer 100%: Checks: 87, Failures: 0, Errors: 0 PASS check_ptlisp (exit status: 0) diff --git a/implementations/c/tests/ptlisp-test.c b/implementations/c/tests/ptlisp-test.c index 211d6e4..3bdd369 100644 --- a/implementations/c/tests/ptlisp-test.c +++ b/implementations/c/tests/ptlisp-test.c @@ -16,7 +16,7 @@ int main(void) { srunner_add_suite(sr, make_reader_test_suite()); srunner_add_suite(sr, make_printer_test_suite()); - srunner_set_fork_status(sr, CK_NOFORK); + /* srunner_set_fork_status(sr, CK_NOFORK); */ srunner_run_all(sr, CK_NORMAL); nf = srunner_ntests_failed(sr); srunner_free(sr);