From dbd987ab19685876bbc1df4c1d0deac7771221c4 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Thu, 23 Mar 2017 18:57:06 +0100 Subject: [PATCH] mescc: Have ungetc remember 2 positions. * doc/examples/t.c (read_test): Test it. * doc/examples/mini-mes.c: * mlibc.c (getchar, ungetc): Support 2 ungetc positions. * module/mes/libc.mes (getchar, ungetc): Likewise. --- mlibc.c | 11 +++++------ module/mes/libc.mes | 22 +++++++++++++++------- scaffold/mini-mes.c | 2 ++ scaffold/t.c | 15 +++++++++++++-- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/mlibc.c b/mlibc.c index 7d4ebf09..e4442dd8 100644 --- a/mlibc.c +++ b/mlibc.c @@ -222,6 +222,7 @@ assert_fail (char* s) int ungetc_char = -1; +char ungetc_buf[2]; int getchar () @@ -235,10 +236,8 @@ getchar () i = c; } else - { - i = ungetc_char; - ungetc_char = -1; - } + i = ungetc_buf[ungetc_char--]; + if (i < 0) i += 256; return i; } @@ -246,8 +245,8 @@ getchar () int ungetc (int c, int fd) { - assert (ungetc_char == -1); - ungetc_char = c; + assert (ungetc_char < 2); + ungetc_buf[++ungetc_char] = c; return c; } diff --git a/module/mes/libc.mes b/module/mes/libc.mes index 05029875..4f575782 100644 --- a/module/mes/libc.mes +++ b/module/mes/libc.mes @@ -56,9 +56,6 @@ strlen (char const* s) (define getchar (let* ((ast (with-input-from-string " -int g_stdin; -int ungetc_char = -1; - #if 0 int getchar () @@ -71,6 +68,9 @@ getchar () } #endif +int g_stdin = 0; +int ungetc_char = -1; +char ungetc_buf[2]; int getchar () { @@ -84,8 +84,11 @@ getchar () } else { - i = ungetc_char; - ungetc_char = -1; + //FIXME + //i = ungetc_buf[ungetc_char--]; + i = ungetc_buf[ungetc_char]; + //ungetc_char--; + ungetc_char = ungetc_char - 1; } if (i < 0) i += 256; return i; @@ -121,8 +124,13 @@ assert_fail (char* s) int ungetc (int c, int fd) { - assert (ungetc_char == -1); - ungetc_char = c; + //FIXME + //assert (ungetc_char < 2); + assert (ungetc_char == -1 || ungetc_char < 2); + //FIXME + //ungetc_buf[++ungetc_char] = c; + ungetc_char++; + ungetc_buf[ungetc_char] = c; return c; } " diff --git a/scaffold/mini-mes.c b/scaffold/mini-mes.c index 3abe2615..def353aa 100644 --- a/scaffold/mini-mes.c +++ b/scaffold/mini-mes.c @@ -26,6 +26,8 @@ #if __MESCC__ //void *g_malloc_base = 0; char *g_malloc_base = 0; +// int ungetc_char = -1; +// char ungetc_buf[2]; #endif #define MES_MINI 1 diff --git a/scaffold/t.c b/scaffold/t.c index db43cd8e..86ffd739 100644 --- a/scaffold/t.c +++ b/scaffold/t.c @@ -147,10 +147,9 @@ get () int read_test () { - puts ("read test\n"); char *p = (char*)g_chars; int i = 0; - puts ("t: read 0123456789\n"); + puts ("t: read 0123456789\nt: "); int c = get (); while (i < 10) { *p++ = c; @@ -160,6 +159,18 @@ read_test () } puts ("\n"); if (strcmp (g_chars, "0123456789")) return 1; + + puts ("t: ungetc ('A') == getchar ()\n"); + ungetc ('A', STDIN); + if (getchar () != 'A') return 1; + ungetc (0, STDIN); + //ungetc ('\1', STDIN); + ungetc (1, STDIN); + puts ("t: ungetc ();ungetc ();getchar ();getchar ()\n"); + if (getchar () != 1) return 1; + //if (getchar () != '\0') return 1; + if (getchar () != 0) return 1; + return 0; }