From eeff2724ab61172d037fbba78785a25d691b6ad1 Mon Sep 17 00:00:00 2001 From: xchaos Date: Thu, 21 Aug 2008 11:44:42 +0000 Subject: [PATCH] added check for lvalue to coalesce() macro (to prevent double evaluation of expression or function) git-svn-id: https://dev.arachne.cz/repos/cll1h/trunk@82 4bb87942-c103-4e5a-b51c-0ebff58f8515 --- cll1.h | 5 +++-- demos/advanced-hello-world.c | 13 +++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cll1.h b/cll1.h index 2e27140..c49fa3c 100644 --- a/cll1.h +++ b/cll1.h @@ -88,7 +88,8 @@ extern unsigned RANDOM_SEED; #define PI 3.141592654 #define WEIRDINT (1<<(sizeof(int)*8-1)) #define btoa(EXPR) ((EXPR)?"TRUE":"FALSE") -#define coalesce(VAR,EXPR) (VAR?VAR:(EXPR)) +#define coalesce(VAR,EXPR) (VAR?VAR:(VAR=0,(EXPR))) +#define coalesce_str(VAR,EXPR) (VAR && *VAR?VAR:((VAR=NULL),(EXPR))) /* .----------------------------------------------------------------------. / 4. C<<1 data declaration and allocation, updated 2008-01-31 xCh. '----------------------------------------------------------------------- */ @@ -185,7 +186,7 @@ extern unsigned RANDOM_SEED; #define construct(TYPE,INTERFACE) TYPE _init_##TYPE(TYPE o,INTERFACE i) { o->__interface=i; return o; } TYPE __init__##TYPE #define get_obj(TYPE,INTERFACE,...) __init__##TYPE(_init_##TYPE(get_mem(TYPE),INTERFACE),__VA_ARGS__) #define get_obj_as(COMMUNITY,MEM,INTERFACE,...) (COMMUNITY)get_obj(MEM,INTERFACE,__VA_ARGS__) -#define _(METHOD,...) ((((_ARG1(__VA_ARGS__,)) || /* will not compile if object is not Lvalue: */(_ARG1(__VA_ARGS__,)=NULL)) && (_ARG1(__VA_ARGS__,))->__interface->METHOD)?(*( (_ARG1(__VA_ARGS__,))->__interface->METHOD )) ((void *)__VA_ARGS__):0) +#define _(METHOD,...) ((((_ARG1(__VA_ARGS__,)) || /* will not compile if object is not lvalue: */(_ARG1(__VA_ARGS__,)=NULL)) && (_ARG1(__VA_ARGS__,))->__interface->METHOD)?(*( (_ARG1(__VA_ARGS__,))->__interface->METHOD )) ((void *)__VA_ARGS__):0) #define __(METHOD,...) ( _ASSERT(ARG1(__VA_ARGS__,)), (*( __cll1_seekmethod(ARG1(__VA_ARGS__,),METHOD) )) ( __VA_ARGS__ ) ) #define call(OBJTYPE,METHOD,...) { int _N=0; OBJTYPE _OBJ; /* <-' */ while( (_OBJ=(OBJTYPE)_cll1_for_ptrs(_N++,__VA_ARGS__,NIL))!=(OBJTYPE)NIL ) _(_OBJ,METHOD); } #define def_community(TYPE,INTERFACE) def_obj(TYPE) { INTERFACE __interface; }; diff --git a/demos/advanced-hello-world.c b/demos/advanced-hello-world.c index a5475ba..124788a 100644 --- a/demos/advanced-hello-world.c +++ b/demos/advanced-hello-world.c @@ -7,11 +7,16 @@ program print("* print()"); print("Hello","world","!"); print("* NIL"); - print("We can print",NULL,"safely."); + print("We can print", NULL, "safely."); NIL="(hic sunt crocodiles)"; - print("We can print",NULL,"safely."); - print("* coalesce():"); - print("We can",coalesce("print","everything"),"almost",coalesce(NULL,"anything"),"safely."); + print("We can print", NULL, "safely."); + + { + str p = "print", a = NULL; + print("* coalesce():"); + print("We can", coalesce(p, "everything"), "almost", coalesce(a, "anything"), "safely."); + } + echo("Surprisingly ->",NIL," (you can't see this)"); print("<- nothing special here."); print("* OFS"); -- 2.30.2