From f197671868dac9731f892262de9bec222a48d472 Mon Sep 17 00:00:00 2001 From: Yaossg Date: Sat, 16 Nov 2024 08:39:45 +0800 Subject: [PATCH] no eof --- boot-lib.c | 6 +----- boot-lib.h | 1 - boot.c | 15 +++++++++------ 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/boot-lib.c b/boot-lib.c index 02192e8..093dedd 100644 --- a/boot-lib.c +++ b/boot-lib.c @@ -12,8 +12,4 @@ int eprintf(const char format[], ...) { void ungetchar(int ch) { ungetc(ch, stdin); -} - -int eof() { - return feof(stdin); -} +} \ No newline at end of file diff --git a/boot-lib.h b/boot-lib.h index 4ed452f..c6d147a 100644 --- a/boot-lib.h +++ b/boot-lib.h @@ -8,5 +8,4 @@ void exit(int status); // ext void ungetchar(int ch); -int eof(); int eprintf(const char* format, ...); diff --git a/boot.c b/boot.c index 33ff623..0072543 100644 --- a/boot.c +++ b/boot.c @@ -72,7 +72,7 @@ const int TOKEN_STRING = 150; int parse_int(int ch) { int num = ch - '0'; - while (!eof() && is_digit(ch = getchar())) { + while (is_digit(ch = getchar())) { num = num * 10; num = num + ch - '0'; } @@ -118,7 +118,11 @@ int string_lut_size; int parse_string() { int offset = string_offset; int ch; - while (!eof() && (ch = getchar()) != '"') { + while ((ch = getchar()) != '"') { + if (ch == -1 || ch == '\n') { + eprintf("expecting '\"'\n"); + exit(1); + } if (ch == '\\') { ch = get_escaped_char(); } @@ -136,7 +140,7 @@ int id_lut_size; int parse_id(int ch) { int offset = id_offset; id_table[id_offset++] = ch; - while (!eof() && is_id_cont(ch = getchar())) { + while (is_id_cont(ch = getchar())) { id_table[id_offset++] = ch; } ungetchar(ch); @@ -1278,6 +1282,7 @@ void parse_top_level() { eprintf("unexpected token: %d\n", token_type); exit(1); } + parse_top_level(); } void dump_string_table() { @@ -1311,9 +1316,7 @@ void dump_string_table() { } int main() { - while (!eof()) { - parse_top_level(); - } + parse_top_level(); dump_string_table(); return 0; }