From 57629478a15249b4fc4bbffe534c2540e61607d3 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 28 Nov 2023 20:47:23 +0900 Subject: [PATCH] some 'do' experiment --- README.md | 7 +++++++ lib/comp.c | 20 +++++++++++++++++++- t/Makefile.am | 1 + t/Makefile.in | 1 + 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3cd878f..7238cd2 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,13 @@ A HCL program is composed of expressions. - until - while +### do + +``` +do; +do 10; +do { | k | set k 20; printf "k=%d\n" k }; +``` ## Literals - integer diff --git a/lib/comp.c b/lib/comp.c index 6018c25..fb9de33 100644 --- a/lib/comp.c +++ b/lib/comp.c @@ -24,6 +24,10 @@ #include "hcl-prv.h" +/* limit the `do` expression to have not more than 1 expression and + * no variable declaration if not enclosed in parentheses */ +#define LANG_LIMIT_DO + enum { VAR_NAMED, @@ -1707,7 +1711,6 @@ static HCL_INLINE int compile_or_p2 (hcl_t* hcl) /* ========================================================================= */ - /* EXPERIMENT WITH BINOP */ static int compile_plus (hcl_t* hcl, hcl_cnode_t* src) { @@ -2004,6 +2007,7 @@ static int compile_expression_block (hcl_t* hcl, hcl_cnode_t* src, const hcl_bch return -1; } +#if defined(LANG_LIMIT_DO) /* this limitation doesn't seem really useful? or make it #pragma based? */ if (!(flags & CEB_IS_BLOCK) && (flags & CEB_AUTO_FORGED)) { /* `do` not explicitly enclosed in (). @@ -2016,6 +2020,7 @@ static int compile_expression_block (hcl_t* hcl, hcl_cnode_t* src, const hcl_bch "variable declaration disallowed in %hs context", ctxname); return -1; } +#endif } tvslen = hcl->c->tv.s.len; @@ -2033,6 +2038,19 @@ static int compile_expression_block (hcl_t* hcl, hcl_cnode_t* src, const hcl_bch } } +#if defined(LANG_LIMIT_DO) + if (!(flags & CEB_IS_BLOCK) && (flags & CEB_AUTO_FORGED)) + { + if (obj && HCL_CNODE_IS_CONS(obj) && hcl_countcnodecons(hcl, obj) != 1) + { + hcl_setsynerrbfmt ( + hcl, HCL_SYNERR_VARDCLBANNED, HCL_CNODE_GET_LOC(obj), HCL_NULL, + "more than one expression after %hs", ctxname); + return -1; + } + } +#endif + fbi = &hcl->c->fnblk.info[hcl->c->fnblk.depth]; fbi->tmprlen = hcl->c->tv.s.len; fbi->tmprcnt = hcl->c->tv.wcount; diff --git a/t/Makefile.am b/t/Makefile.am index 67d94c3..929c1e5 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -10,6 +10,7 @@ check_SCRIPTS = \ check_ERRORS = \ do-01.err \ + do-02.err \ feed-01.err \ var-01.err \ var-02.err \ diff --git a/t/Makefile.in b/t/Makefile.in index d145a27..150455c 100644 --- a/t/Makefile.in +++ b/t/Makefile.in @@ -481,6 +481,7 @@ check_SCRIPTS = \ check_ERRORS = \ do-01.err \ + do-02.err \ feed-01.err \ var-01.err \ var-02.err \