diff --git a/lib/read.c b/lib/read.c index 93236d5..4a11257 100644 --- a/lib/read.c +++ b/lib/read.c @@ -25,6 +25,7 @@ #include "hcl-prv.h" #define HCL_LANG_ENABLE_WIDE_DELIM +#define HCL_LANG_AUTO_FORGE_XLIST_ALWAYS #define BUFFER_ALIGN 128 #define BALIT_BUFFER_ALIGN 128 @@ -1307,7 +1308,7 @@ static int feed_process_token (hcl_t* hcl) goto start_list; case HCL_TOK_LBRACE: /* { */ - /* this is a block opener itself. auto xlist forge at the block beginning */ + /* this is a block opener itself. auto xlist forge at the block beginning only */ frd->flagv = 0; LIST_FLAG_SET_CONCODE (frd->flagv, HCL_CONCODE_BLOCK); goto start_list; @@ -1332,6 +1333,25 @@ static int feed_process_token (hcl_t* hcl) #endif case HCL_TOK_LPAREN: /* ( */ +#if defined(HCL_LANG_AUTO_FORGE_XLIST_ALWAYS) + /* with this feature on, you must not enclose an expression with () + * at the top-level or a the block level. If you do, it is interpreted as + * double exuection, meaning the return value of the expression is + * called again. + * + * (+ 10 20) as a leading expression is like ((+ 10 20)). + * ------------------------------------------------------------- + * However, it can be a bit confusing: + * defun x(a) { return (fun(b) { return (+ a b) }) } + * printf "%d\n" ((x 10) 20) ## excplicit outer () is required here + * (x 10) 30 ## explicit outer () must not be used here + * j := ((x 10) 40) ## explicit outer () is required here + * k := { + * (x 10) 50 ## explicit outer () must not be used here + * } + */ + if (auto_forge_xlist_if_at_block_beginning(hcl, frd) <= -1) goto oops; +#endif frd->flagv = 0; LIST_FLAG_SET_CONCODE (frd->flagv, HCL_CONCODE_XLIST); start_list: diff --git a/t/fun-01.hcl b/t/fun-01.hcl index 5b39764..c9d936b 100644 --- a/t/fun-01.hcl +++ b/t/fun-01.hcl @@ -20,23 +20,41 @@ defun mkfun(t) { }); }; -set f (mkfun 20); +f := (mkfun 20); set k (f 50); -if (= k 70) { +if (k = 70) { printf "OK - %d\n" k; } else { printf "ERROR - %d\n" k; }; +k := { + (mkfun 20) 30 +} +if (k = 50) { + printf "OK - %d\n" k +} else { + printf "ERROR - %d\n" k +}; + +k := { + (mkfun 20) 30 + (mkfun 20) 40 +} +if (k = 60) { + printf "OK - %d\n" k +} else { + printf "ERROR - %d\n" k +}; ## -------------------------------------- defclass A | a b c | { defun :* newInstance(x y z) { - (set a x) - (set b y) - (set c z) - (return self) + set a x + set b y + set c z + return self }; defun get-a() { return a; };