From 658e69635a08c21ad4b2670e8f067fe33f2da644 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 15 Apr 2024 13:14:49 +0900 Subject: [PATCH] lib/read.c - updated code to reset some internal data upon an error so that another call to hcl_feed() and variants don't suffer --- bin/main.c | 12 ++++++++++++ lib/read.c | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/bin/main.c b/bin/main.c index be48b72..1b5ad05 100644 --- a/bin/main.c +++ b/bin/main.c @@ -642,6 +642,7 @@ static int feed_loop (hcl_t* hcl, xtn_t* xtn, int verbose) while (1) { int n; + hcl_oow_t pos; /* read a while line regardless of the actual expression */ n = get_line(hcl, xtn, fp); @@ -649,6 +650,7 @@ static int feed_loop (hcl_t* hcl, xtn_t* xtn, int verbose) if (n == 0) break; /* feed the line */ + #if 0 while (xtn->feed.pos < xtn->feed.len) { hcl_bch_t c = xtn->feed.buf[xtn->feed.pos++]; @@ -658,6 +660,16 @@ static int feed_loop (hcl_t* hcl, xtn_t* xtn, int verbose) show_prompt (hcl, 0); } } + #else + pos = xtn->feed.pos; + /* do this before calling hcl_feedbchars() so that the callback sees the updated value */ + xtn->feed.pos = xtn->feed.len; + if (hcl_feedbchars(hcl, &xtn->feed.buf[pos], xtn->feed.len - pos) <= -1) + { + print_error (hcl, "failed to feed"); + show_prompt (hcl, 0); + } + #endif } } else diff --git a/lib/read.c b/lib/read.c index f7bc2f9..1fccca1 100644 --- a/lib/read.c +++ b/lib/read.c @@ -167,7 +167,12 @@ static struct HCL_AID(HCL_CONCODE_VLIST) { HCL_TOK_VBAR, HCL_SYNERR_VBAR, VOCA_VLIST } /* VLIST | | */ }; +/* ----------------------------------------------------------------- */ + static int init_compiler (hcl_t* hcl); +static void feed_continue (hcl_t* hcl, hcl_flx_state_t state); + +/* ----------------------------------------------------------------- */ static HCL_INLINE int is_spacechar (hcl_ooci_t c) { @@ -1250,7 +1255,7 @@ static int feed_process_token (hcl_t* hcl) /* this function composes an s-expression non-recursively * by manipulating its own stack. */ -/*hcl_logbfmt (hcl, HCL_LOG_STDERR, "TOKEN => [%.*js] type=%d LOC=%d.%d\n", TOKEN_NAME_LEN(hcl), TOKEN_NAME_PTR(hcl), TOKEN_TYPE(hcl), TOKEN_LOC(hcl)->line, TOKEN_LOC(hcl)->colm);*/ +/*hcl_logbfmt (hcl, HCL_LOG_STDERR, "TOKEN [%d] EOL[%d]=> [%.*js] type=%d LOC=%d.%d\n", TOKEN_TYPE(hcl), HCL_TOK_EOL, TOKEN_NAME_LEN(hcl), TOKEN_NAME_PTR(hcl), TOKEN_TYPE(hcl), TOKEN_LOC(hcl)->line, TOKEN_LOC(hcl)->colm);*/ if (frd->expect_include_file) { /* the #include directive is an exception to the general expression rule. @@ -1776,7 +1781,7 @@ static int feed_process_token (hcl_t* hcl) int n; /* upon exit, we must be at the top level */ - HCL_ASSERT (hcl, frd->level == 0); + HCL_ASSERT (hcl, frd->flagv & AT_BEGINNING); HCL_ASSERT (hcl, hcl->c->r.st == HCL_NULL); HCL_ASSERT (hcl, frd->obj != HCL_NULL); @@ -1815,9 +1820,11 @@ oops: hcl_freecnode (hcl, frd->obj); frd->obj = HCL_NULL; } + HCL_MEMSET (frd, 0, HCL_SIZEOF(*frd)); /* clean up the reader stack for a list */ feed_clean_up_reader_stack (hcl); + feed_continue (hcl, HCL_FLX_START); return -1; } @@ -1921,10 +1928,9 @@ static int feed_wrap_up_with_str (hcl_t* hcl, const hcl_ooch_t* str, hcl_oow_t l return feed_wrap_up(hcl, type); } -static int feed_continue (hcl_t* hcl, hcl_flx_state_t state) +static void feed_continue (hcl_t* hcl, hcl_flx_state_t state) { hcl->c->feed.lx.state = state; - return 0; } static int feed_continue_with_char (hcl_t* hcl, hcl_ooci_t c, hcl_flx_state_t state) @@ -1937,7 +1943,7 @@ static int feed_continue_with_char (hcl_t* hcl, hcl_ooci_t c, hcl_flx_state_t st #define FEED_WRAP_UP(hcl, type) do { if (feed_wrap_up(hcl, type) <= -1) return -1; } while(0) #define FEED_WRAP_UP_WITH_CHAR(hcl, c, type) do { if (feed_wrap_up_with_char(hcl, c, type) <= -1) return -1; } while(0) #define FEED_WRAP_UP_WITH_CHARS(hcl, str, len, type) do { if (feed_wrap_up_with_str(hcl, str, len, type) <= -1) return -1; } while(0) -#define FEED_CONTINUE(hcl, state) do { if (feed_continue(hcl, state) <= -1) return -1; } while(0) +#define FEED_CONTINUE(hcl, state) (feed_continue(hcl, state)) #define FEED_CONTINUE_WITH_CHAR(hcl, c, state) do { if (feed_continue_with_char(hcl, c, state) <= -1) return -1; } while(0) /* ------------------------------------------------------------------------ */ @@ -3311,6 +3317,7 @@ oops: * leave_list() error in flx_hmarked_ident() before a full cnode is processed */ feed_clean_up_reader_stack (hcl); + feed_continue (hcl, HCL_FLX_START); return -1; }