From d99a5142784045de6cfae77e2fea3ce3616d8212 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 4 Feb 2024 21:35:31 +0900 Subject: [PATCH] WIP - introducing the BINOP token --- lib/hcl-prv.h | 9 +++++++ lib/read.c | 64 ++++++++++++++++++++++++++++++++++++++++++------- t/Makefile.am | 2 ++ t/Makefile.in | 2 ++ t/feed-5008.err | 3 +++ t/feed-5009.err | 3 +++ 6 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 t/feed-5008.err create mode 100644 t/feed-5009.err diff --git a/lib/hcl-prv.h b/lib/hcl-prv.h index 7c08cce..78bb7b3 100644 --- a/lib/hcl-prv.h +++ b/lib/hcl-prv.h @@ -178,6 +178,7 @@ enum hcl_tok_type_t HCL_TOK_SELF, HCL_TOK_SUPER, + HCL_TOK_BINOP, HCL_TOK_IDENT, HCL_TOK_IDENT_DOTTED, HCL_TOK_IDENT_DOTTED_CLA, @@ -628,6 +629,12 @@ struct hcl_flx_pi_t int is_cla; /* class-level accrssor. prefixed with self/super */ }; +typedef struct hcl_flx_binop_t hcl_flx_binop_t; +struct hcl_flx_binop_t +{ + hcl_oow_t _not_used; +}; + typedef struct hcl_flx_pn_t hcl_flx_pn_t; struct hcl_flx_pn_t { @@ -685,6 +692,7 @@ enum hcl_flx_state_t HCL_FLX_HMARKED_IDENT, /* hash-marked identifier like #include, etc */ HCL_FLX_HMARKED_NUMBER, /* hash-marked number - radixed number like #xABCD */ HCL_FLX_PLAIN_IDENT, /* plain identifier */ + HCL_FLX_BINOP, /* binary operator */ HCL_FLX_PLAIN_NUMBER, /* plain number */ HCL_FLX_QUOTED_TOKEN, /* string, character */ HCL_FLX_SIGNED_TOKEN, /* prefixed with + or - */ @@ -776,6 +784,7 @@ struct hcl_compiler_t hcl_flx_hb_t hb; /* #b ... */ hcl_flx_hn_t hn; /* hash-marked number - radixed number */ hcl_flx_pi_t pi; /* plain identifier */ + hcl_flx_binop_t binop; /* binary operator */ hcl_flx_pn_t pn; /* plain number */ hcl_flx_qt_t qt; /* quoted token */ hcl_flx_st_t st; /* signed token */ diff --git a/lib/read.c b/lib/read.c index 328dfa5..4a147c2 100644 --- a/lib/read.c +++ b/lib/read.c @@ -181,12 +181,6 @@ static HCL_INLINE int is_linebreak (hcl_ooci_t c) return c == '\n'; /* make sure this is one of the space chars in is_spacechar() */ } -static HCL_INLINE int is_alphachar (hcl_ooci_t c) -{ -/* TODO: support full unicode */ - return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); -} - static HCL_INLINE int is_digitchar (hcl_ooci_t c) { /* TODO: support full unicode */ @@ -199,11 +193,20 @@ static HCL_INLINE int is_xdigitchar (hcl_ooci_t c) return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); } +#if 0 +static HCL_INLINE int is_alphachar (hcl_ooci_t c) +{ +/* TODO: support full unicode */ + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); +} + + static HCL_INLINE int is_alnumchar (hcl_ooci_t c) { /* TODO: support full unicode */ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); } +#endif static HCL_INLINE int is_delimchar (hcl_ooci_t c) { @@ -213,6 +216,12 @@ static HCL_INLINE int is_delimchar (hcl_ooci_t c) c == '#' || c == '\"' || c == '\'' || c == '\\' || is_spacechar(c) || c == HCL_OOCI_EOF; } +static HCL_INLINE int is_binopchar (hcl_ooci_t c) +{ + return c == '&' || c == '*' || c == '+' || c == '-' || c == '/' || c == '%' || + c == '<' || c == '>' || c == '=' || c == '@' || c == '|' || c == '~'; +} + /* TODO: remove this use the one in comp.c */ static int copy_string_to (hcl_t* hcl, const hcl_oocs_t* src, hcl_oocs_t* dst, hcl_oow_t* dst_capa, int append, hcl_ooch_t add_delim) { @@ -1526,6 +1535,7 @@ static int feed_process_token (hcl_t* hcl) frd->obj = hcl_makecnodebstrlit(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; + case HCL_TOK_BINOP: /* TODO: handle this specially as a binary operator */ case HCL_TOK_IDENT: frd->obj = hcl_makecnodesymbol(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; @@ -1747,6 +1757,7 @@ static int feed_continue_with_char (hcl_t* hcl, hcl_ooci_t c, hcl_flx_state_t st #define FLX_HB(hcl) (&((hcl)->c->feed.lx.u.hb)) #define FLX_HN(hcl) (&((hcl)->c->feed.lx.u.hn)) #define FLX_PI(hcl) (&((hcl)->c->feed.lx.u.pi)) +#define FLX_BINOP(hcl) (&((hcl)->c->feed.lx.u.binop)) #define FLX_PN(hcl) (&((hcl)->c->feed.lx.u.pn)) #define FLX_QT(hcl) (&((hcl)->c->feed.lx.u.qt)) #define FLX_ST(hcl) (&((hcl)->c->feed.lx.u.st)) @@ -1793,6 +1804,11 @@ static HCL_INLINE void init_flx_pi (hcl_flx_pi_t* pi) HCL_MEMSET (pi, 0, HCL_SIZEOF(*pi)); } +static HCL_INLINE void init_flx_binop (hcl_flx_binop_t* binop) +{ + HCL_MEMSET (binop, 0, HCL_SIZEOF(*binop)); +} + static HCL_INLINE void init_flx_pn (hcl_flx_pn_t* pn) { HCL_MEMSET (pn, 0, HCL_SIZEOF(*pn)); @@ -1906,9 +1922,16 @@ static int flx_start (hcl_t* hcl, hcl_ooci_t c) goto consumed; default: - /* TODO: limit the identifier characters and cause syntax error for other characters.. */ - init_flx_pi (FLX_PI(hcl)); - FEED_CONTINUE (hcl, HCL_FLX_PLAIN_IDENT); + if (is_binopchar(c)) + { + init_flx_binop (FLX_BINOP(hcl)); + FEED_CONTINUE (hcl, HCL_FLX_BINOP); + } + else + { + init_flx_pi (FLX_PI(hcl)); + FEED_CONTINUE (hcl, HCL_FLX_PLAIN_IDENT); + } goto not_consumed; } @@ -2377,6 +2400,28 @@ not_consumed: return 0; } +static int flx_binop (hcl_t* hcl, hcl_ooci_t c) /* identifier */ +{ + hcl_flx_binop_t* binop = FLX_BINOP(hcl); + + if (is_binopchar(c)) + { + ADD_TOKEN_CHAR (hcl, c); + goto consumed; + } + else + { + FEED_WRAP_UP (hcl, HCL_TOK_BINOP); + goto not_consumed; + } + +consumed: + return 1; + +not_consumed: + return 0; +} + static int flx_plain_number (hcl_t* hcl, hcl_ooci_t c) /* number */ { hcl_flx_pn_t* pn = FLX_PN(hcl); @@ -2729,6 +2774,7 @@ static int feed_char (hcl_t* hcl, hcl_ooci_t c) case HCL_FLX_HMARKED_IDENT: return flx_hmarked_ident(hcl, c); case HCL_FLX_HMARKED_NUMBER: return flx_hmarked_number(hcl, c); case HCL_FLX_PLAIN_IDENT: return flx_plain_ident(hcl, c); + case HCL_FLX_BINOP: return flx_binop(hcl, c); case HCL_FLX_PLAIN_NUMBER: return flx_plain_number(hcl, c); case HCL_FLX_QUOTED_TOKEN: return flx_quoted_token(hcl, c); case HCL_FLX_SIGNED_TOKEN: return flx_signed_token(hcl, c); diff --git a/t/Makefile.am b/t/Makefile.am index ad8bd46..f3ac5bc 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -22,6 +22,8 @@ check_ERRORS = \ feed-5005.err \ feed-5006.err \ feed-5007.err \ + feed-5008.err \ + feed-5009.err \ mlist-5001.err \ var-5001.err \ var-5002.err \ diff --git a/t/Makefile.in b/t/Makefile.in index 8f25d3c..9145492 100644 --- a/t/Makefile.in +++ b/t/Makefile.in @@ -493,6 +493,8 @@ check_ERRORS = \ feed-5005.err \ feed-5006.err \ feed-5007.err \ + feed-5008.err \ + feed-5009.err \ mlist-5001.err \ var-5001.err \ var-5002.err \ diff --git a/t/feed-5008.err b/t/feed-5008.err new file mode 100644 index 0000000..7070959 --- /dev/null +++ b/t/feed-5008.err @@ -0,0 +1,3 @@ +defun :: fun1() { ##ERROR: syntax error - function name not symbol in defun + return 10; +}; diff --git a/t/feed-5009.err b/t/feed-5009.err new file mode 100644 index 0000000..b745f7e --- /dev/null +++ b/t/feed-5009.err @@ -0,0 +1,3 @@ +defun :* fun1() { ##ERROR: syntax error - function name not symbol in defun + return 10; +};