From 9f4bf7485da7168cefd1cc5343f99aea53304590 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 28 Nov 2023 01:44:13 +0900 Subject: [PATCH] enhanced the compiler to prohibit variable declaration after 'do' if it's the head of block expression --- lib/chr.c | 14 ++++----- lib/cnode.c | 85 ++++++++++++++++++++++++++------------------------- lib/comp.c | 49 ++++++++++++++++++++--------- lib/hcl-prv.h | 50 +++++++++++++++++------------- lib/read.c | 43 +++++++++++++------------- t/Makefile.am | 1 + t/Makefile.in | 1 + t/do-01.err | 3 ++ 8 files changed, 140 insertions(+), 106 deletions(-) create mode 100644 t/do-01.err diff --git a/lib/chr.c b/lib/chr.c index 40ef053..b2cca9a 100644 --- a/lib/chr.c +++ b/lib/chr.c @@ -107,7 +107,7 @@ int hcl_is_uch_blank (hcl_uch_t c) hcl_uch_t hcl_to_uch_upper (hcl_uch_t c) { hcl_uchu_t uc = (hcl_uchu_t)c; - if (uc >= 0 && uc <= UCH_CASE_MAX) + if (uc >= 0 && uc <= UCH_CASE_MAX) { uch_case_page_t* page; page = uch_case_map[UCH_CASE_MAP_INDEX(uc)]; @@ -119,7 +119,7 @@ hcl_uch_t hcl_to_uch_upper (hcl_uch_t c) hcl_uch_t hcl_to_uch_lower (hcl_uch_t c) { hcl_uchu_t uc = (hcl_uchu_t)c; - if (uc >= 0 && uc <= UCH_CASE_MAX) + if (uc >= 0 && uc <= UCH_CASE_MAX) { uch_case_page_t* page; page = uch_case_map[UCH_CASE_MAP_INDEX(uc)]; @@ -184,16 +184,16 @@ hcl_bch_t hcl_to_bch_lower (hcl_bch_t c) /* ----------------------------------------------------------------------- */ /* - * See http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c + * See http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c */ -struct interval +struct interval { int first; int last; }; /* auxiliary function for binary search in interval table */ -static int bisearch(hcl_uch_t ucs, const struct interval *table, int max) +static int bisearch(hcl_uch_t ucs, const struct interval *table, int max) { int min = 0; int mid; @@ -318,7 +318,7 @@ int hcl_get_ucwidth (hcl_uch_t uc) (uc >= 0xff00 && uc <= 0xff60) || /* Fullwidth Forms */ (uc >= 0xffe0 && uc <= 0xffe6) #if (HCL_SIZEOF_UCH_T > 2) - || + || (uc >= 0x20000 && uc <= 0x2fffd) || (uc >= 0x30000 && uc <= 0x3fffd) #endif @@ -328,5 +328,5 @@ int hcl_get_ucwidth (hcl_uch_t uc) } } - return 1; + return 1; } diff --git a/lib/cnode.c b/lib/cnode.c index 884328e..6d65154 100644 --- a/lib/cnode.c +++ b/lib/cnode.c @@ -24,7 +24,7 @@ #include "hcl-prv.h" -static hcl_cnode_t* make_cnode (hcl_t* hcl, hcl_cnode_type_t type, const hcl_loc_t* loc, const hcl_oocs_t* tok) +static hcl_cnode_t* make_cnode (hcl_t* hcl, hcl_cnode_type_t type, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { hcl_cnode_t* cnode; hcl_oocs_t empty; @@ -36,10 +36,11 @@ static hcl_cnode_t* make_cnode (hcl_t* hcl, hcl_cnode_type_t type, const hcl_loc empty.len = 0; tok = ∅ } - cnode = hcl_callocmem(hcl, HCL_SIZEOF(*cnode) + HCL_SIZEOF(*tok->ptr) * (tok->len + 1)); + cnode = (hcl_cnode_t*)hcl_callocmem(hcl, HCL_SIZEOF(*cnode) + HCL_SIZEOF(*tok->ptr) * (tok->len + 1)); if (HCL_UNLIKELY(!cnode)) return HCL_NULL; cnode->cn_type = type; + cnode->cn_flags = flags; cnode->cn_loc = *loc; cnode->cn_tok.ptr = (hcl_ooch_t*)(cnode + 1); @@ -50,123 +51,123 @@ static hcl_cnode_t* make_cnode (hcl_t* hcl, hcl_cnode_type_t type, const hcl_loc return cnode; } -hcl_cnode_t* hcl_makecnodenil (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok) +hcl_cnode_t* hcl_makecnodenil (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { - return make_cnode(hcl, HCL_CNODE_NIL, loc, tok); + return make_cnode(hcl, HCL_CNODE_NIL, flags, loc, tok); } -hcl_cnode_t* hcl_makecnodetrue (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok) +hcl_cnode_t* hcl_makecnodetrue (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { - return make_cnode(hcl, HCL_CNODE_TRUE, loc, tok); + return make_cnode(hcl, HCL_CNODE_TRUE, flags, loc, tok); } -hcl_cnode_t* hcl_makecnodefalse (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok) +hcl_cnode_t* hcl_makecnodefalse (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { - return make_cnode(hcl, HCL_CNODE_FALSE, loc, tok); + return make_cnode(hcl, HCL_CNODE_FALSE, flags, loc, tok); } -hcl_cnode_t* hcl_makecnodeself (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok) +hcl_cnode_t* hcl_makecnodeself (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { - return make_cnode(hcl, HCL_CNODE_SELF, loc, tok); + return make_cnode(hcl, HCL_CNODE_SELF, flags, loc, tok); } -hcl_cnode_t* hcl_makecnodesuper (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok) +hcl_cnode_t* hcl_makecnodesuper (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { - return make_cnode(hcl, HCL_CNODE_SUPER, loc, tok); + return make_cnode(hcl, HCL_CNODE_SUPER, flags, loc, tok); } -hcl_cnode_t* hcl_makecnodeellipsis (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok) +hcl_cnode_t* hcl_makecnodeellipsis (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { - return make_cnode(hcl, HCL_CNODE_ELLIPSIS, loc, tok); + return make_cnode(hcl, HCL_CNODE_ELLIPSIS, flags, loc, tok); } -hcl_cnode_t* hcl_makecnodetrpcolons (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok) +hcl_cnode_t* hcl_makecnodetrpcolons (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { - return make_cnode(hcl, HCL_CNODE_TRPCOLONS, loc, tok); + return make_cnode(hcl, HCL_CNODE_TRPCOLONS, flags, loc, tok); } -hcl_cnode_t* hcl_makecnodedcstar (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok) +hcl_cnode_t* hcl_makecnodedcstar (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { - return make_cnode(hcl, HCL_CNODE_DCSTAR, loc, tok); + return make_cnode(hcl, HCL_CNODE_DCSTAR, flags, loc, tok); } -hcl_cnode_t* hcl_makecnodecharlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok, const hcl_ooch_t v) +hcl_cnode_t* hcl_makecnodecharlit (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok, const hcl_ooch_t v) { - hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_CHARLIT, loc, tok); + hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_CHARLIT, flags, loc, tok); if (HCL_UNLIKELY(!c)) return HCL_NULL; c->u.charlit.v = v; return c; } -hcl_cnode_t* hcl_makecnodesymbol (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok) +hcl_cnode_t* hcl_makecnodesymbol (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { - hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_SYMBOL, loc, tok); + hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_SYMBOL, flags, loc, tok); if (HCL_UNLIKELY(!c)) return HCL_NULL; c->u.symbol.syncode = hcl_getsyncodebyoocs_noseterr(hcl, tok); return c; } -hcl_cnode_t* hcl_makecnodedsymbol (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok) +hcl_cnode_t* hcl_makecnodedsymbol (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { - return make_cnode(hcl, HCL_CNODE_DSYMBOL, loc, tok); + return make_cnode(hcl, HCL_CNODE_DSYMBOL, flags, loc, tok); } -hcl_cnode_t* hcl_makecnodestrlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok) +hcl_cnode_t* hcl_makecnodestrlit (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { - return make_cnode(hcl, HCL_CNODE_STRLIT, loc, tok); + return make_cnode(hcl, HCL_CNODE_STRLIT, flags, loc, tok); } -hcl_cnode_t* hcl_makecnodenumlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok) +hcl_cnode_t* hcl_makecnodenumlit (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { - return make_cnode(hcl, HCL_CNODE_NUMLIT, loc, tok); + return make_cnode(hcl, HCL_CNODE_NUMLIT, flags, loc, tok); } -hcl_cnode_t* hcl_makecnoderadnumlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok) +hcl_cnode_t* hcl_makecnoderadnumlit (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { - return make_cnode(hcl, HCL_CNODE_RADNUMLIT, loc, tok); + return make_cnode(hcl, HCL_CNODE_RADNUMLIT, flags, loc, tok); } -hcl_cnode_t* hcl_makecnodefpdeclit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok) +hcl_cnode_t* hcl_makecnodefpdeclit (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok) { - return make_cnode(hcl, HCL_CNODE_FPDECLIT, loc, tok); + return make_cnode(hcl, HCL_CNODE_FPDECLIT, flags, loc, tok); } -hcl_cnode_t* hcl_makecnodesmptrlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_oow_t v) +hcl_cnode_t* hcl_makecnodesmptrlit (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_oow_t v) { - hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_SMPTRLIT, loc, tok); + hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_SMPTRLIT, flags, loc, tok); if (HCL_UNLIKELY(!c)) return HCL_NULL; c->u.smptrlit.v = v; return c; } -hcl_cnode_t* hcl_makecnodeerrlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_ooi_t v) +hcl_cnode_t* hcl_makecnodeerrlit (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_ooi_t v) { - hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_ERRLIT, loc, tok); + hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_ERRLIT, flags, loc, tok); if (HCL_UNLIKELY(!c)) return HCL_NULL; c->u.errlit.v = v; return c; } -hcl_cnode_t* hcl_makecnodecons (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_cnode_t* car, hcl_cnode_t* cdr) +hcl_cnode_t* hcl_makecnodecons (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_cnode_t* car, hcl_cnode_t* cdr) { - hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_CONS, loc, tok); + hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_CONS, flags, loc, tok); if (HCL_UNLIKELY(!c)) return HCL_NULL; c->u.cons.car = car; c->u.cons.cdr = cdr; return c; } -hcl_cnode_t* hcl_makecnodeelist (hcl_t* hcl, const hcl_loc_t* loc, hcl_concode_t type) +hcl_cnode_t* hcl_makecnodeelist (hcl_t* hcl, int flags, const hcl_loc_t* loc, hcl_concode_t type) { - hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_ELIST, loc, HCL_NULL); + hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_ELIST, flags, loc, HCL_NULL); if (HCL_UNLIKELY(!c)) return HCL_NULL; c->u.elist.concode = type; return c; } -hcl_cnode_t* hcl_makecnodeshell (hcl_t* hcl, const hcl_loc_t* loc, hcl_cnode_t* obj) +hcl_cnode_t* hcl_makecnodeshell (hcl_t* hcl, int flags, const hcl_loc_t* loc, hcl_cnode_t* obj) { - hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_SHELL, loc, HCL_NULL); + hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_SHELL, flags, loc, HCL_NULL); if (HCL_UNLIKELY(!c)) return HCL_NULL; c->u.shell.obj = obj; return c; diff --git a/lib/comp.c b/lib/comp.c index af5a79d..6018c25 100644 --- a/lib/comp.c +++ b/lib/comp.c @@ -1962,14 +1962,17 @@ inside_loop: /* ========================================================================= */ -static int compile_expression_block (hcl_t* hcl, hcl_cnode_t* src, const hcl_bch_t* ctxname, int is_block) +#define CEB_IS_BLOCK (1 << 0) +#define CEB_AUTO_FORGED (1 << 1) + +static int compile_expression_block (hcl_t* hcl, hcl_cnode_t* src, const hcl_bch_t* ctxname, int flags) { hcl_cnode_t* cmd, * obj, * tmp; hcl_oow_t nlvars, tvslen; hcl_fnblk_info_t* fbi; hcl_cframe_t* cf; - if (is_block) + if (flags & CEB_IS_BLOCK) { HCL_ASSERT (hcl, HCL_CNODE_IS_CONS_CONCODED(src, HCL_CONCODE_BLOCK) || HCL_CNODE_IS_ELIST_CONCODED(src, HCL_CONCODE_BLOCK)); cmd = src; /* it's the cons cell itself */ @@ -1991,12 +1994,28 @@ static int compile_expression_block (hcl_t* hcl, hcl_cnode_t* src, const hcl_bch } } - if (is_in_class_init_scope(hcl) && is_followed_by_vlist(hcl, obj)) + if (is_followed_by_vlist(hcl, obj)) { - hcl_setsynerrbfmt ( - hcl, HCL_SYNERR_VARDCLBANNED, HCL_CNODE_GET_LOC(obj), HCL_NULL, - "variable declaration disallowed in class init scope"); - return -1; + if (is_in_class_init_scope(hcl)) + { + hcl_setsynerrbfmt ( + hcl, HCL_SYNERR_VARDCLBANNED, HCL_CNODE_GET_LOC(obj), HCL_NULL, + "variable declaration disallowed in class init scope"); + return -1; + } + + if (!(flags & CEB_IS_BLOCK) && (flags & CEB_AUTO_FORGED)) + { + /* `do` not explicitly enclosed in (). + * e.g. do | x | { set x 20; }; + * ^ + * +-- this is not allowed + */ + hcl_setsynerrbfmt ( + hcl, HCL_SYNERR_VARDCLBANNED, HCL_CNODE_GET_LOC(obj), HCL_NULL, + "variable declaration disallowed in %hs context", ctxname); + return -1; + } } tvslen = hcl->c->tv.s.len; @@ -2033,12 +2052,13 @@ static int compile_expression_block (hcl_t* hcl, hcl_cnode_t* src, const hcl_bch return 0; } -static int compile_do (hcl_t* hcl, hcl_cnode_t* src) +static int compile_do (hcl_t* hcl, hcl_cnode_t* xlist) { hcl_cnode_t* cmd, * obj, * tmp; hcl_oow_t nlvars, tvslen; hcl_fnblk_info_t* fbi; hcl_cframe_t* cf; + int flags = 0; /* (do * (+ 10 20) @@ -2048,13 +2068,14 @@ static int compile_do (hcl_t* hcl, hcl_cnode_t* src) * you can use this to combine multiple expressions to a single expression */ - HCL_ASSERT (hcl, HCL_CNODE_IS_CONS(src)); - HCL_ASSERT (hcl, HCL_CNODE_IS_SYMBOL_SYNCODED(HCL_CNODE_CONS_CAR(src), HCL_SYNCODE_DO)); + HCL_ASSERT (hcl, HCL_CNODE_IS_CONS(xlist)); + HCL_ASSERT (hcl, HCL_CNODE_IS_SYMBOL_SYNCODED(HCL_CNODE_CONS_CAR(xlist), HCL_SYNCODE_DO)); - cmd = HCL_CNODE_CONS_CAR(src); /* do itself */ - obj = HCL_CNODE_CONS_CDR(src); /* expression list after it */ + cmd = HCL_CNODE_CONS_CAR(xlist); /* do itself */ + obj = HCL_CNODE_CONS_CDR(xlist); /* expression list after it */ - return compile_expression_block(hcl, src, "do", 0); + if (HCL_CNODE_GET_FLAGS(xlist) & HCL_CNODE_AUTO_FORGED) flags |= CEB_AUTO_FORGED; + return compile_expression_block(hcl, xlist, "do", flags); } static int compile_do_p1 (hcl_t* hcl) @@ -3927,7 +3948,7 @@ static int compile_cons_mlist_expression (hcl_t* hcl, hcl_cnode_t* obj, int nret static int compile_cons_block_expression (hcl_t* hcl, hcl_cnode_t* obj) { - return compile_expression_block(hcl, obj, "block", 1); + return compile_expression_block(hcl, obj, "block", CEB_IS_BLOCK); } static HCL_INLINE int compile_symbol (hcl_t* hcl, hcl_cnode_t* obj) diff --git a/lib/hcl-prv.h b/lib/hcl-prv.h index 1927217..c574b9c 100644 --- a/lib/hcl-prv.h +++ b/lib/hcl-prv.h @@ -243,7 +243,14 @@ enum hcl_cnode_type_t }; typedef enum hcl_cnode_type_t hcl_cnode_type_t; +enum hcl_cnode_flag_t +{ + HCL_CNODE_AUTO_FORGED = (1 << 0) +}; +typedef enum hcl_cnode_flagt hcl_cnode_flag_t; + #define HCL_CNODE_GET_TYPE(x) ((x)->cn_type) +#define HCL_CNODE_GET_FLAGS(x) ((x)->cn_flags) #define HCL_CNODE_GET_LOC(x) (&(x)->cn_loc) #define HCL_CNODE_GET_TOK(x) (&(x)->cn_tok) #define HCL_CNODE_GET_TOKPTR(x) ((x)->cn_tok.ptr) @@ -274,6 +281,7 @@ typedef enum hcl_cnode_type_t hcl_cnode_type_t; struct hcl_cnode_t { hcl_cnode_type_t cn_type; + int cn_flags; hcl_loc_t cn_loc; hcl_oocs_t cn_tok; @@ -283,12 +291,10 @@ struct hcl_cnode_t { hcl_ooch_t v; } charlit; - struct { hcl_syncode_t syncode; /* special if non-zero */ } symbol; - struct { hcl_oow_t v; @@ -1707,26 +1713,26 @@ int hcl_emitbyteinstruction ( /* ========================================================================= */ /* cnode.c */ /* ========================================================================= */ -hcl_cnode_t* hcl_makecnodenil (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok); -hcl_cnode_t* hcl_makecnodetrue (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok); -hcl_cnode_t* hcl_makecnodefalse (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok); -hcl_cnode_t* hcl_makecnodeself (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok); -hcl_cnode_t* hcl_makecnodesuper (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok); -hcl_cnode_t* hcl_makecnodeellipsis (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok); -hcl_cnode_t* hcl_makecnodetrpcolons (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok); -hcl_cnode_t* hcl_makecnodedcstar (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok); -hcl_cnode_t* hcl_makecnodecharlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok, const hcl_ooch_t v); -hcl_cnode_t* hcl_makecnodesymbol (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok); -hcl_cnode_t* hcl_makecnodedsymbol (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok); -hcl_cnode_t* hcl_makecnodestrlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok); -hcl_cnode_t* hcl_makecnodenumlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok); -hcl_cnode_t* hcl_makecnoderadnumlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok); -hcl_cnode_t* hcl_makecnodefpdeclit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok); -hcl_cnode_t* hcl_makecnodesmptrlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_oow_t v); -hcl_cnode_t* hcl_makecnodeerrlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_ooi_t v); -hcl_cnode_t* hcl_makecnodecons (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_cnode_t* car, hcl_cnode_t* cdr); -hcl_cnode_t* hcl_makecnodeelist (hcl_t* hcl, const hcl_loc_t* loc, hcl_concode_t type); -hcl_cnode_t* hcl_makecnodeshell (hcl_t* hcl, const hcl_loc_t* loc, hcl_cnode_t* obj); +hcl_cnode_t* hcl_makecnodenil (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok); +hcl_cnode_t* hcl_makecnodetrue (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok); +hcl_cnode_t* hcl_makecnodefalse (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok); +hcl_cnode_t* hcl_makecnodeself (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok); +hcl_cnode_t* hcl_makecnodesuper (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok); +hcl_cnode_t* hcl_makecnodeellipsis (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok); +hcl_cnode_t* hcl_makecnodetrpcolons (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok); +hcl_cnode_t* hcl_makecnodedcstar (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok); +hcl_cnode_t* hcl_makecnodecharlit (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok, const hcl_ooch_t v); +hcl_cnode_t* hcl_makecnodesymbol (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok); +hcl_cnode_t* hcl_makecnodedsymbol (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok); +hcl_cnode_t* hcl_makecnodestrlit (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok); +hcl_cnode_t* hcl_makecnodenumlit (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok); +hcl_cnode_t* hcl_makecnoderadnumlit (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok); +hcl_cnode_t* hcl_makecnodefpdeclit (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok); +hcl_cnode_t* hcl_makecnodesmptrlit (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_oow_t v); +hcl_cnode_t* hcl_makecnodeerrlit (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_ooi_t v); +hcl_cnode_t* hcl_makecnodecons (hcl_t* hcl, int flags, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_cnode_t* car, hcl_cnode_t* cdr); +hcl_cnode_t* hcl_makecnodeelist (hcl_t* hcl, int flags, const hcl_loc_t* loc, hcl_concode_t type); +hcl_cnode_t* hcl_makecnodeshell (hcl_t* hcl, int flags, const hcl_loc_t* loc, hcl_cnode_t* obj); void hcl_freesinglecnode (hcl_t* hcl, hcl_cnode_t* c); hcl_oow_t hcl_countcnodecons (hcl_t* hcl, hcl_cnode_t* cons); void hcl_dumpcnode (hcl_t* hcl, hcl_cnode_t* c, int newline); diff --git a/lib/read.c b/lib/read.c index d618087..725e217 100644 --- a/lib/read.c +++ b/lib/read.c @@ -657,7 +657,7 @@ static const hcl_ooch_t* add_sr_name (hcl_t* hcl, const hcl_oocs_t* name) static HCL_INLINE int enter_list (hcl_t* hcl, const hcl_loc_t* loc, int flagv) { hcl_rstl_t* rstl; - rstl = hcl_callocmem(hcl, HCL_SIZEOF(*rstl)); + rstl = (hcl_rstl_t*)hcl_callocmem(hcl, HCL_SIZEOF(*rstl)); if (HCL_UNLIKELY(!rstl)) return -1; rstl->loc = *loc; rstl->flagv = flagv; @@ -716,11 +716,12 @@ static HCL_INLINE hcl_cnode_t* leave_list (hcl_t* hcl, hcl_loc_t* list_loc, int* { HCL_ASSERT (hcl, HCL_CNODE_IS_CONS(head)); HCL_CNODE_CONS_CONCODE(head) = concode; + if (fv & AUTO_FORGED) HCL_CNODE_GET_FLAGS(head) |= HCL_CNODE_AUTO_FORGED; return head; } /* the list is empty */ - return hcl_makecnodeelist(hcl, &loc, concode); + return hcl_makecnodeelist(hcl, ((fv & AUTO_FORGED)? HCL_CNODE_AUTO_FORGED: 0), &loc, concode); } static HCL_INLINE int can_dot_list (hcl_t* hcl) @@ -836,7 +837,7 @@ static int chain_to_list (hcl_t* hcl, hcl_cnode_t* obj, hcl_loc_t* loc) * use a shell node to wrap the actual object list node head * for the compiler. */ - shell = hcl_makecnodeshell(hcl, HCL_CNODE_GET_LOC(obj), obj); + shell = hcl_makecnodeshell(hcl, 0, HCL_CNODE_GET_LOC(obj), obj); if (HCL_UNLIKELY(!shell)) return -1; tail->u.cons.cdr = shell; @@ -880,7 +881,7 @@ static int chain_to_list (hcl_t* hcl, hcl_cnode_t* obj, hcl_loc_t* loc) fake_tok_ptr = &fake_tok; } - cons = hcl_makecnodecons(hcl, (loc? loc: HCL_CNODE_GET_LOC(obj)), fake_tok_ptr, obj, HCL_NULL); + cons = hcl_makecnodecons(hcl, 0, (loc? loc: HCL_CNODE_GET_LOC(obj)), fake_tok_ptr, obj, HCL_NULL); if (HCL_UNLIKELY(!cons)) return -1; if (rstl->count <= 0) @@ -1323,35 +1324,35 @@ static int feed_process_token (hcl_t* hcl) } case HCL_TOK_NIL: - frd->obj = hcl_makecnodenil(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); + frd->obj = hcl_makecnodenil(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; case HCL_TOK_TRUE: - frd->obj = hcl_makecnodetrue(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); + frd->obj = hcl_makecnodetrue(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; case HCL_TOK_FALSE: - frd->obj = hcl_makecnodefalse(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); + frd->obj = hcl_makecnodefalse(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; case HCL_TOK_SELF: - frd->obj = hcl_makecnodeself(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); + frd->obj = hcl_makecnodeself(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; case HCL_TOK_SUPER: - frd->obj = hcl_makecnodesuper(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); + frd->obj = hcl_makecnodesuper(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; case HCL_TOK_ELLIPSIS: - frd->obj = hcl_makecnodeellipsis(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); + frd->obj = hcl_makecnodeellipsis(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; case HCL_TOK_TRPCOLONS: - frd->obj = hcl_makecnodetrpcolons(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); + frd->obj = hcl_makecnodetrpcolons(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; case HCL_TOK_DCSTAR: - frd->obj = hcl_makecnodedcstar(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); + frd->obj = hcl_makecnodedcstar(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; case HCL_TOK_SMPTRLIT: @@ -1372,7 +1373,7 @@ static int feed_process_token (hcl_t* hcl) goto oops; } - frd->obj = hcl_makecnodesmptrlit(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl), v); + frd->obj = hcl_makecnodesmptrlit(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl), v); goto auto_xlist; } @@ -1394,24 +1395,24 @@ static int feed_process_token (hcl_t* hcl) } } - frd->obj = hcl_makecnodeerrlit(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl), v); + frd->obj = hcl_makecnodeerrlit(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl), v); goto auto_xlist; } case HCL_TOK_CHARLIT: - frd->obj = hcl_makecnodecharlit(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl), TOKEN_NAME_CHAR(hcl, 0)); + frd->obj = hcl_makecnodecharlit(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl), TOKEN_NAME_CHAR(hcl, 0)); goto auto_xlist; case HCL_TOK_NUMLIT: - frd->obj = hcl_makecnodenumlit(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); + frd->obj = hcl_makecnodenumlit(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; case HCL_TOK_RADNUMLIT: - frd->obj = hcl_makecnoderadnumlit(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); + frd->obj = hcl_makecnoderadnumlit(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; case HCL_TOK_FPDECLIT: - frd->obj = hcl_makecnodefpdeclit(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); + frd->obj = hcl_makecnodefpdeclit(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; /* @@ -1421,15 +1422,15 @@ static int feed_process_token (hcl_t* hcl) */ case HCL_TOK_STRLIT: - frd->obj = hcl_makecnodestrlit(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); + frd->obj = hcl_makecnodestrlit(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; case HCL_TOK_IDENT: - frd->obj = hcl_makecnodesymbol(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); + frd->obj = hcl_makecnodesymbol(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; case HCL_TOK_IDENT_DOTTED: - frd->obj = hcl_makecnodedsymbol(hcl, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); + frd->obj = hcl_makecnodedsymbol(hcl, 0, TOKEN_LOC(hcl), TOKEN_NAME(hcl)); goto auto_xlist; auto_xlist: diff --git a/t/Makefile.am b/t/Makefile.am index eef9574..67d94c3 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -9,6 +9,7 @@ check_SCRIPTS = \ va-01.hcl check_ERRORS = \ + do-01.err \ feed-01.err \ var-01.err \ var-02.err \ diff --git a/t/Makefile.in b/t/Makefile.in index bbb6eae..d145a27 100644 --- a/t/Makefile.in +++ b/t/Makefile.in @@ -480,6 +480,7 @@ check_SCRIPTS = \ va-01.hcl check_ERRORS = \ + do-01.err \ feed-01.err \ var-01.err \ var-02.err \ diff --git a/t/do-01.err b/t/do-01.err new file mode 100644 index 0000000..5a75315 --- /dev/null +++ b/t/do-01.err @@ -0,0 +1,3 @@ +## if `do` is not enclosed in `( )`, variable declaration is prohibited +(do | k | (set k 10)) +do | k | {set k 10;}; ##ERROR: syntax error - variable declaration disallowed