From 67751a2f472c03c1dd632d39b53f9c6c5967dcb1 Mon Sep 17 00:00:00 2001 From: Gabriel Pariat Date: Mon, 21 Feb 2022 06:20:42 -0500 Subject: [PATCH] handling reader macro --- implementations/c/src/lisp.c | 13 ++++++++++++- implementations/c/src/lisp.h | 1 - implementations/c/src/reader.c | 14 +++----------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/implementations/c/src/lisp.c b/implementations/c/src/lisp.c index a11a3dc..89ab7fb 100644 --- a/implementations/c/src/lisp.c +++ b/implementations/c/src/lisp.c @@ -303,12 +303,14 @@ void init(void) { SET_FUNC("peek-char", peek_char_fn); SET_FUNC("read-char", read_char_fn); SET_FUNC("read", read_fn); + SET_FUNC("set-reader-macro", set_reader_macro_fn); SET_FUNC("exit", exit_fn); SET_FUNC("=", eq_fn); SET_FUNC("<", lt_fn); SET_FUNC(">", gt_fn); SET_FUNC("<=", le_fn); SET_FUNC(">=", ge_fn); + SET_FUNC("print", print_fn); SET_FORM("if", if_fn); SET_FORM("let", let_fn); @@ -321,10 +323,19 @@ void init(void) { SET_FORM("defmacro", defmacro_fn); } +/* +(set-reader-macro \' (fn (s c) (list quote (read s)))) + */ + void repl(void) { while(true) { printf(">>> "); - print(eval(read_fn(NIL, NIL), NIL), stdout); + Pointer readed = read_fn(NIL, NIL); + char c; + while((c = getc(stdin)) && isspace(c) && c != '\n'); + if (c != '\n') ungetc(c, stdin); + print(eval(readed, NIL), stdout); + printf("\n"); } } diff --git a/implementations/c/src/lisp.h b/implementations/c/src/lisp.h index 3095e2b..5ea34fe 100644 --- a/implementations/c/src/lisp.h +++ b/implementations/c/src/lisp.h @@ -144,7 +144,6 @@ Pointer string_clear(Pointer str); Pointer environment_get(Pointer env, Pointer key); Pointer environment_set(Pointer env, Pointer key, Pointer value); -Pointer set_reader_macro(Pointer c, Pointer fn); Char get_utf8(FILE* s); Char unget_utf8(Char c, FILE* s); Char peek_char(Pointer type, Stream stream); diff --git a/implementations/c/src/reader.c b/implementations/c/src/reader.c index b7c1f30..f816a64 100644 --- a/implementations/c/src/reader.c +++ b/implementations/c/src/reader.c @@ -114,7 +114,7 @@ Pointer read1(Pointer streamPtr, Pointer env) { case NATIVE_FUNC: return NATIVE_FUNC(macro)(LIST(streamPtr, character(get_utf8(stream))), env); case FUNC: - return eval_fn(LIST(macro, streamPtr, character(get_utf8(stream))), env); + return eval(LIST(macro, streamPtr, character(get_utf8(stream))), env); default: break; } } @@ -139,22 +139,14 @@ Pointer read1(Pointer streamPtr, Pointer env) { Pointer read_fn(Pointer args, Pointer env) { Pointer streamPtr = CAR(args); streamPtr = streamPtr == NIL ? environment_get(env, STANDARD_INPUT) : streamPtr; - Stream stream = STREAM(streamPtr); Pointer result = read1(streamPtr, env); - char c; - while((c = getc(stream)) && isspace(c) && c != '\n'); - if (c != '\n') ungetc(c, stream); return result; } -Pointer set_reader_macro(Pointer c, Pointer fn) { - READTABLE = table_set(READTABLE, c, fn); - return T; -} - Pointer set_reader_macro_fn(Pointer args, Pointer env) { (void) env; - return set_reader_macro(CAR(args), CAR(CDR (args))); + READTABLE = table_set(READTABLE, CHAR(CAR(args)), CAR(CDR(args))); + return T; } static bool return_char(Stream stream) {