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.
This commit is contained in:
Jan Nieuwenhuizen 2017-03-23 18:57:06 +01:00
parent 0685c9e7e8
commit dbd987ab19
4 changed files with 35 additions and 15 deletions

11
mlibc.c
View File

@ -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;
}

View File

@ -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;
}
"

View File

@ -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

View File

@ -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;
}