From f887ba78a6fc834958e993d17307994643dd7c51 Mon Sep 17 00:00:00 2001 From: Gabriel Pariat Date: Sun, 20 Feb 2022 18:57:21 -0500 Subject: [PATCH] fix peek-char --- implementations/c/src/lisp.c | 10 +++++++++- implementations/c/src/reader.c | 13 +++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/implementations/c/src/lisp.c b/implementations/c/src/lisp.c index 95001d2..e0349c5 100644 --- a/implementations/c/src/lisp.c +++ b/implementations/c/src/lisp.c @@ -302,6 +302,7 @@ void init(void) { SET_FUNC("~", lognot_fn); SET_FUNC("peek-char", peek_char_fn); SET_FUNC("read-char", read_char_fn); + SET_FUNC("read", read_fn); SET_FUNC("exit", exit_fn); SET_FUNC("=", eq_fn); SET_FUNC("<", lt_fn); @@ -323,7 +324,14 @@ void init(void) { void repl(void) { while(true) { printf(">>> "); - print(eval(read_fn(NIL, NIL), NIL), stdout); + Pointer reader = read_fn(NIL, NIL); + char c; + while((c = getc(stdin)) && isspace(c) && c != '\n'); + if (c != '\n') ungetc(c, stdin); + /* read_char_fn(NIL, NIL); */ + /* read_char_fn(NIL, NIL); */ + /* printf("eof?: %d", feof(stdin)); */ + print(eval(reader, NIL), stdout); printf("\n"); } } diff --git a/implementations/c/src/reader.c b/implementations/c/src/reader.c index 768b0bb..b3c821f 100644 --- a/implementations/c/src/reader.c +++ b/implementations/c/src/reader.c @@ -85,6 +85,10 @@ Pointer peek_char_fn(Pointer args, Pointer env) { } return character(c); + /* printf("%s: %d\n", __FILE__, __LINE__); */ + /* char c = getc(stream); */ + /* printf("%s: %d: %c\n", __FILE__, __LINE__, c); */ + /* return character(ungetc(c, stream)); */ } Pointer read_char_fn(Pointer args, Pointer env) { @@ -139,9 +143,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); + /* Stream stream = STREAM(streamPtr); */ Pointer result = read1(streamPtr, env); - peek_char(T, stream); + /* peek_char(T, stream); */ + /* getc(stream); */ + /* char c; */ + /* getc(stream); */ + /* while((c = getc(stream)) >= 0 && c == ' ') ungetc(c, stream); */ + /* if (c != EOF) ungetc(c, stream); */ return result; }