diff --git a/bin/hcl.c b/bin/hcl.c index 65f743b..8f08fe4 100644 --- a/bin/hcl.c +++ b/bin/hcl.c @@ -641,10 +641,8 @@ static int feed_loop (hcl_t* hcl, xtn_t* xtn, int verbose) print_error (hcl, "failed to feed"); if (len > 0) show_prompt (hcl, 0); - /* clear the compiled code but not executed yet */ - hcl_clearcode(hcl); - hcl_clearfnblks(hcl); - xtn->feed.ncompexprs = 0; + hcl_clearcode(hcl); /* clear the compiled code but not executed yet in advance */ + xtn->feed.ncompexprs = 0; /* next time, hcl_compile() is supposed to clear code and fnblks */ } else { diff --git a/lib/comp.c b/lib/comp.c index cb5ed3b..4aac594 100644 --- a/lib/comp.c +++ b/lib/comp.c @@ -1242,12 +1242,6 @@ static void pop_fnblk (hcl_t* hcl) } } -void hcl_clearfnblks (hcl_t* hcl) -{ - while (hcl->c->fnblk.depth >= 0) pop_fnblk (hcl); - HCL_ASSERT (hcl, hcl->c->fnblk.depth == -1); -} - /* ========================================================================= */ static HCL_INLINE int _insert_cframe (hcl_t* hcl, hcl_ooi_t index, int opcode, hcl_cnode_t* operand) { @@ -5897,7 +5891,8 @@ int hcl_compile (hcl_t* hcl, hcl_cnode_t* obj, int flags) * in the interactive mode, the information doesn't have * to get carried over. */ - hcl_clearfnblks (hcl); + while (hcl->c->fnblk.depth >= 0) pop_fnblk (hcl); + HCL_ASSERT (hcl, hcl->c->fnblk.depth == -1); /* it will be recreated below */ } if (flags & HCL_COMPILE_CLEAR_CODE) hcl_clearcode (hcl); @@ -6203,9 +6198,6 @@ int hcl_compile (hcl_t* hcl, hcl_cnode_t* obj, int flags) HCL_ASSERT (hcl, hcl->c->fnblk.depth == 0); /* ensure the virtual function block be the only one left */ hcl->code.ngtmprs = hcl->c->fnblk.info[0].tmprcnt; /* populate the number of global temporary variables */ - /* TODO: delete all !defined(CLEAR_FNBLK_ALWAYS) code - * keep only defined(CLEAR_FNBLK_ALWAYS) code. - * not clearing the top fnblk for the reuse doesn't look very beneficial */ #if defined(CLEAR_FNBLK_ALWAYS) pop_fnblk (hcl); HCL_ASSERT (hcl, hcl->c->tv.s.len == 0); diff --git a/lib/hcl.h b/lib/hcl.h index a5d1e39..18ac7af 100644 --- a/lib/hcl.h +++ b/lib/hcl.h @@ -1318,7 +1318,7 @@ struct hcl_io_udiarg_t void* handle; /** - * [OUT] place data here for #HCL_IO_READ + * [OUT] place data in c for #HCL_IO_READ and in d for #HCL_IO_READ_BYTES */ union { hcl_ooch_t c[2048]; /* TODO: resize this if necessary */ @@ -1330,6 +1330,12 @@ struct hcl_io_udiarg_t * #HCL_IO_READ or #HCL_IO_READ_BYTES */ hcl_oow_t xlen; + + /** + * Internal use only. Don't touch these. + */ + hcl_oow_t pos; + int is_byte; }; typedef struct hcl_io_udoarg_t hcl_io_udoarg_t; @@ -2568,10 +2574,6 @@ HCL_EXPORT void hcl_clearcode ( hcl_t* hcl ); -HCL_EXPORT void hcl_clearfnblks ( - hcl_t* hcl -); - #if defined(HCL_HAVE_INLINE) static HCL_INLINE hcl_code_t* hcl_getcode (hcl_t* hcl) { return &hcl->code; } static HCL_INLINE hcl_oob_t* hcl_getbcptr (hcl_t* hcl) { return hcl->code.bc.ptr; } diff --git a/lib/prim.c b/lib/prim.c index 7a57d15..e6d56d0 100644 --- a/lib/prim.c +++ b/lib/prim.c @@ -215,15 +215,77 @@ static hcl_pfrc_t pf_sprintf (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs) /* ------------------------------------------------------------------------- */ + +static int get_udi_char (hcl_t* hcl, hcl_ooch_t* ch) +{ + if (hcl->io.udi_arg.pos >= hcl->io.udi_arg.xlen) + { + hcl->io.udi_arg.pos = 0; + hcl->io.udi_arg.xlen = 0; + if (hcl->io.udi_rdr(hcl, HCL_IO_READ, &hcl->io.udi_arg) <= -1) return -1; + if (hcl->io.udi_arg.xlen <= 0) return 0; /* EOF */ + hcl->io.udi_arg.is_byte = 0; + } + + + if (hcl->io.udi_arg.is_byte) + { + /* TODO: set error */ + return -1; + } + + *ch = hcl->io.udi_arg.buf.c[hcl->io.udi_arg.pos++]; + return 1; +} + +static int get_udi_byte (hcl_t* hcl, hcl_uint8_t* bt) +{ + if (hcl->io.udi_arg.pos >= hcl->io.udi_arg.xlen) + { + hcl->io.udi_arg.pos = 0; + hcl->io.udi_arg.xlen = 0; + if (hcl->io.udi_rdr(hcl, HCL_IO_READ_BYTES, &hcl->io.udi_arg) <= -1) return -1; + if (hcl->io.udi_arg.xlen <= 0) return 0; /* EOF */ + hcl->io.udi_arg.is_byte = 1; + } + + if (!hcl->io.udi_arg.is_byte) + { + /* TODO: set error */ + return -1; + } + + *bt = hcl->io.udi_arg.buf.b[hcl->io.udi_arg.pos++]; + return 1; +} + static hcl_pfrc_t pf_getbyte (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs) { + hcl_oop_t v; + hcl_uint8_t bt; + int n; + + n = get_udi_byte(hcl, &bt); + if (n <= -1) return HCL_PF_FAILURE; + + /* return nil on EOF, or the actual character read */ + v = (n == 0)? hcl->_nil: HCL_SMOOI_TO_OOP(bt); + HCL_STACK_SETRET (hcl, nargs, v); return HCL_PF_SUCCESS; } static hcl_pfrc_t pf_getch (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs) { - /*getchar(;) - HCL_STACK_SETRET (hcl, nars, v);*/ + hcl_oop_t v; + hcl_ooch_t ch; + int n; + + n = get_udi_char(hcl, &ch); + if (n <= -1) return HCL_PF_FAILURE; + + /* return nil on EOF, or the actual character read */ + v = (n == 0)? hcl->_nil: HCL_CHAR_TO_OOP(ch); + HCL_STACK_SETRET (hcl, nargs, v); return HCL_PF_SUCCESS; }