From 8a277c77c0deecfaa4d52d29536074b0139c34bb Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 22 Sep 2024 22:04:18 +0900 Subject: [PATCH] ongoing work to support 0x, 0o, 0b --- lib/comp.c | 8 +++---- lib/fmt.c | 12 +++++----- lib/read.c | 61 +++++++++++++++---------------------------------- t/feed-5001.err | 4 ++-- 4 files changed, 30 insertions(+), 55 deletions(-) diff --git a/lib/comp.c b/lib/comp.c index 4b297e9..2d6d538 100644 --- a/lib/comp.c +++ b/lib/comp.c @@ -4740,22 +4740,22 @@ static hcl_oop_t string_to_num (hcl_t* hcl, hcl_oocs_t* str, const hcl_loc_t* lo #else if (radixed) { - /* #xFF80, #b1111 */ + /* 0xFF80, 0b1111 */ HCL_ASSERT (hcl, ptr < end); - if (*ptr != '#') + if (/**ptr != '#' &&*/ *ptr != '0') { hcl_setsynerrbfmt(hcl, HCL_SYNERR_RADIX, loc, str, "radixed number not starting with #"); return HCL_NULL; } - ptr++; /* skip '#' */ + ptr++; /* skip '0' */ if (*ptr == 'x') base = 16; else if (*ptr == 'o') base = 8; else if (*ptr == 'b') base = 2; else { - hcl_setsynerrbfmt (hcl, HCL_SYNERR_RADIX, loc, str, "invalid radix specifier %c", *ptr); + hcl_setsynerrbfmt (hcl, HCL_SYNERR_RADIX, loc, HCL_NULL, "invalid radix specifier %c in %js", *ptr, str); return HCL_NULL; } ptr++; diff --git a/lib/fmt.c b/lib/fmt.c index e0ab519..5378d6f 100644 --- a/lib/fmt.c +++ b/lib/fmt.c @@ -1365,17 +1365,17 @@ static int fmt_outv (hcl_t* hcl, hcl_fmtout_t* fmtout, va_list ap) { if (base == 2) { - PUT_OOCH (hcl, fmtout, '#', 1); + PUT_OOCH (hcl, fmtout, '0', 1); PUT_OOCH (hcl, fmtout, 'b', 1); } if (base == 8) { - PUT_OOCH (hcl, fmtout, '#', 1); + PUT_OOCH (hcl, fmtout, '0', 1); PUT_OOCH (hcl, fmtout, 'o', 1); } else if (base == 16) { - PUT_OOCH (hcl, fmtout, '#', 1); + PUT_OOCH (hcl, fmtout, '0', 1); PUT_OOCH (hcl, fmtout, 'x', 1); } } @@ -2713,17 +2713,17 @@ static HCL_INLINE int format_stack_args (hcl_t* hcl, hcl_fmtout_t* fmtout, hcl_o { if (radix == 2) { - PUT_OOCH (hcl, fmtout, '#', 1); + PUT_OOCH (hcl, fmtout, '0', 1); PUT_OOCH (hcl, fmtout, 'b', 1); } if (radix == 8) { - PUT_OOCH (hcl, fmtout, '#', 1); + PUT_OOCH (hcl, fmtout, '0', 1); PUT_OOCH (hcl, fmtout, 'o', 1); } else if (radix == 16) { - PUT_OOCH (hcl, fmtout, '#', 1); + PUT_OOCH (hcl, fmtout, '0', 1); PUT_OOCH (hcl, fmtout, 'x', 1); } } diff --git a/lib/read.c b/lib/read.c index e0aae0d..64d1673 100644 --- a/lib/read.c +++ b/lib/read.c @@ -1308,6 +1308,18 @@ static int feed_end_include (hcl_t* hcl) return 1; /* ended the included file successfully */ } +static void feed_reset_reader_state (hcl_t* hcl) +{ + hcl_frd_t* frd = &hcl->c->feed.rd; + + if (frd->obj) + { + hcl_freecnode (hcl, frd->obj); + frd->obj = HCL_NULL; + } + HCL_MEMSET (frd, 0, HCL_SIZEOF(*frd)); +} + static void feed_clean_up_reader_stack (hcl_t* hcl) { /* clean up the reader stack for a list */ @@ -2010,12 +2022,7 @@ ok: return 0; oops: - if (frd->obj) - { - hcl_freecnode (hcl, frd->obj); - frd->obj = HCL_NULL; - } - HCL_MEMSET (frd, 0, HCL_SIZEOF(*frd)); + feed_reset_reader_state (hcl); /* clean up the reader stack for a list */ feed_clean_up_reader_stack (hcl); @@ -3267,43 +3274,6 @@ static int flx_signed_token (hcl_t* hcl, hcl_ooci_t c) { hcl_flx_st_t* st = FLX_ST(hcl); - if (st->char_count == 0 && c == '#') - { - ADD_TOKEN_CHAR (hcl, c); - st->hmarked = 1; - st->char_count++; - goto consumed; - } - - if (st->hmarked) - { - HCL_ASSERT (hcl, st->char_count == 1); - - if (c == 'b' || c == 'o' || c == 'x') - { - init_flx_hn (FLX_HN(hcl), HCL_TOK_RADNUMLIT, HCL_SYNERR_NUMLIT, (c == 'b'? 2: (c == 'o'? 8: 16))); - FEED_CONTINUE_WITH_CHAR (hcl, c, HCL_FLX_HMARKED_NUMBER); - goto consumed; - } - else - { - /* at this point, the token name buffer holds +# or -# */ - HCL_ASSERT (hcl, TOKEN_NAME_LEN(hcl) == 2); - TOKEN_NAME_LEN(hcl)--; /* remove the ending # from the name buffer */ - FEED_WRAP_UP (hcl, HCL_TOK_IDENT); - - /* reset the token information as if it enters HMARKED_TOKEN from START */ - reset_flx_token (hcl); - - /* the current character is on the same line as the hash mark, the column must be greater than 1 */ - HCL_ASSERT (hcl, FLX_LOC(hcl)->colm > 1); - FLX_LOC(hcl)->colm--; /* move back one character location by decrementing the column number */ - ADD_TOKEN_CHAR (hcl, '#'); - FEED_CONTINUE (hcl, HCL_FLX_HMARKED_TOKEN); - goto not_consumed; - } - } - HCL_ASSERT (hcl, st->char_count == 0); if (is_digitchar(c)) { @@ -3330,6 +3300,9 @@ static int flx_signed_token (hcl_t* hcl, hcl_ooci_t c) FEED_CONTINUE (hcl, HCL_FLX_PLAIN_IDENT); goto not_consumed; #else + /* the leading sign must be + or - and must be one of the binop chars. */ + HCL_ASSERT (hcl, is_binop_char(st->sign_c));/* must be + or - and they must be one of the binop chars. */ + /* switch to binop mode */ init_flx_binop (FLX_BINOP(hcl)); HCL_ASSERT (hcl, TOKEN_NAME_LEN(hcl) == 1); @@ -3705,6 +3678,8 @@ int hcl_feed (hcl_t* hcl, const hcl_ooch_t* data, hcl_oow_t len) return 0; oops: + feed_reset_reader_state (hcl); + /* if enter_list() is in feed_process_token(), the stack grows. * leave_list() pops an element off the stack. the stack can be * not empty if an error occurs outside feed_process_token() after diff --git a/t/feed-5001.err b/t/feed-5001.err index 8aef76e..4aa2438 100644 --- a/t/feed-5001.err +++ b/t/feed-5001.err @@ -74,7 +74,7 @@ k := #[10 ; 20 ]; ##ERROR: syntax error - unexpected semicolon ## a code point greater than 255 is illegal in the character literal prefix fixed with b. -printf "[%c] [#x%x] [%d]\n" '★' '★' #x2605; +printf "[%c] [0x%x] [%d]\n" '★' '★' 0x2605; printf "[%c]\n" b'★'; ##ERROR: syntax error - wrong character literal --- @@ -82,7 +82,7 @@ printf "[%c]\n" b'★'; ##ERROR: syntax error - wrong character literal ## #b can be followed by [ or binary digits. printf "%O\n" #b[ 10 20 30 ]; -printf "%010b\n" #b0101; +printf "%010b\n" 0b0101; printf "%O\n" #bxy; ##ERROR: syntax error - neither valid radixed number nor valid directive '#bxy' ---