From eac5d6c70c3babc9056f059edd88a2c1a82bc7e9 Mon Sep 17 00:00:00 2001 From: xchaos Date: Fri, 3 Oct 2008 11:15:48 +0000 Subject: [PATCH] #6 feature fuly implemented git-svn-id: https://dev.arachne.cz/repos/cll1h/trunk@96 4bb87942-c103-4e5a-b51c-0ebff58f8515 --- cll1.h | 80 ++++++++++++++++++---------- demos/exceptions/advanced-failures.c | 66 +++++++++++++++++++++++ demos/exceptions/exceptions.c | 58 -------------------- demos/exceptions/failures.c | 75 ++++++++++++++++++++++++++ demos/repeat.c | 4 +- 5 files changed, 195 insertions(+), 88 deletions(-) create mode 100644 demos/exceptions/advanced-failures.c delete mode 100644 demos/exceptions/exceptions.c create mode 100644 demos/exceptions/failures.c diff --git a/cll1.h b/cll1.h index 48aa0a1..a3a6a31 100644 --- a/cll1.h +++ b/cll1.h @@ -42,6 +42,7 @@ #include #include #include +#include #include #ifdef USE_LIBRARY_FASTCGI #include @@ -103,7 +104,7 @@ extern unsigned RANDOM_SEED; #define tree(TYPE) TYPE __next; TYPE __seek #define dict(TYPE) tree(TYPE); str __key #define array(TYPE) tree(TYPE); int __key -/* variable declarators */ +/* ---- variable declarators -------------------------------------------- */ #define Get_mem(ID,TYPE) TYPE ID=get_mem(TYPE) #define Get_buf(ID,SIZE) buf ID=get_buf(SIZE) /* .----------------------------------------------------------------------. @@ -127,7 +128,7 @@ extern unsigned RANDOM_SEED; #define suffix(A,B,C) (((A=strrchr(B,C))&&!(*(A++)=0))||(A=B)) #define prefix(A,B,C) ((A=B),((B=strchr(B,C))&&!(*(B++)=0)||(B=A))) #define tr(STR,B,C) _NEW(S,STR) while(S && *_S) { if(*_S==B)*_S=C; /* <-' */ _S++; } -/* variable declarators */ +/* ---- variable declarators -------------------------------------------- */ #define Get_str(ID,...) str ID=get_str(__VA_ARGS__) /* .----------------------------------------------------------------------. / 6. C<<1 data container manipulation, updated 2008-01-26 xCh. @@ -152,8 +153,8 @@ extern unsigned RANDOM_SEED; / 7. C<<1 builtin iterators and conditionals, updated 2008-01-26 xCh. '----------------------------------------------------------------------- */ #define loop while(TRUE) -#define repeat(N) _NEW(I,0) _NEW(J,(N)) while(_I++<_J) /* C99 version would be: for(int _i=0,_j=(N);_i++<_j;) */ -#define for_range(VAR,FROM,TO) _NEW(I,(FROM)) _NEW(J,(TO)) for( VAR=_I ; _I<=_J?VAR<=_J:VAR>=_J ; _I<=_J?VAR++:VAR--) +#define repeat(N) _NEW(I,(_J=(N),0)) while(_I++<_J) /* C99 version would be: for(int _i=0,_j=(N);_i++<_j;) */ +#define for_range(VAR,FROM,TO) _NEW(I,((_J=(TO)),FROM)) for( VAR=_I ; _I<=_J?VAR<=_J:VAR>=_J ; _I<=_J?VAR++:VAR--) #define for_each(NODE,HEAD) for(NODE=HEAD; NODE; NODE=NODE->__next) #define for_search(NODE,HEAD,EXPR) for_each(NODE,HEAD) if(EXPR) #define for_file_lines(STR,FNAME) /* _NEW(CTX,get_context()) file_read(FNAME) for(use_context(_CTX),STR=file_line(_F);release_context(_M),(*S && !feof(_F));STR=file_line(_F),end_context()) _CATCH */ @@ -166,9 +167,9 @@ extern unsigned RANDOM_SEED; #define continue_args _I++ #define if_exists(NODE,HEAD,EXPR) for_search(NODE,HEAD,EXPR) break; if(NODE) #define if_args if(argc>1) -#define switch_for_args for_args switch_str(this_arg) -#define switch_str(STR) _NEW(S,STR) -#define if_case(VAL) if(eq(argv[_I],VAL)) +#define switch_for_args for_args with_str(this_arg) +#define with_str(STR) _NEW(S,STR) +#define if_case(VAL) if(eq(_S,VAL)) #define for_fields(FIELD,STR) for_valid_split(FIELD,STR,*OFS) #define for_split_str(A,B,C) for(prefix(A,B,C);A;(A!=B)&&prefix(A,B,C)||(A=NULL)) #define for_valid_split(A,B,C) for_split_str(A,B,C) if(*A) @@ -188,24 +189,27 @@ extern unsigned RANDOM_SEED; #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,...) ( _ASSERT(ARG1(__VA_ARGS__,)), (*( __cll1_seekmethod(ARG1(__VA_ARGS__,),METHOD) )) ( __VA_ARGS__ ) ) +//#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; }; #define anonymous void * -/* variable declarators */ +/* ---- variable declarators -------------------------------------------- */ #define Get_obj(ID,MEM,...) MEM ID=get_obj(MEM,__VA_ARGS__) #define Get_obj_as(ID,COMMUNITY,MEM,...) MEM ID=get_obj_as(COMMUNITY,MEM,INTERFACE,__VA_ARGS__) #define I_am(SELF,MEM) MEM SELF=(MEM)community /* .----------------------------------------------------------------------. / 9. C<<1 setjmp()/longjmp() based exceptions, updated 2008-09-30 '----------------------------------------------------------------------- */ -#include -#define try if(!(_exception_type=setjmp(_exception_env))) -#define fail(E) longjmp(_exception_env, (E)) -#define else_switch_exceptions else switch(_exception_type) -#define else_catch_exception(E) else if (_exception_type==(E)) -static jmp_buf _exception_env; -static int _exception_type; +#define FAILED_TO_FAIL -273 +#define SYSTEM_FAILURE -1 +#define LOCAL_FAILURE 13 +#define LIBRARY_FAILURE 23 +#define EVIL_FAILURE 666 +#define try _NEW(E,0) if(!(_E=setjmp(_EENV))) +#define fail(E) longjmp(_EENV,((E)>0?(E):FAILED_TO_FAIL)) +#define pass_failure (_Eatom->__next?longjmp((pop(_Eatom,_Estack),_EENV),_E):_SYSFAIL(FAILED_TO_FAIL)) +#define else_switch_failures else switch(_E) +#define else_catch(E) else if (_E==(E)) /* .----------------------------------------------------------------------. / 10. implementation of C<<1 library functions, updated 2008-01-26 xCh. '----------------------------------------------------------------------- */ @@ -242,24 +246,27 @@ extern _DECLARE_STRSTR; / 10. C<<1 private infrastructure, updated 2008-02-03 by xCh. '----------------------------------------------------------------------- */ #define _I (_Iatom->I) -#define _J (_Jatom->J) +#define _J (_Iatom->J) #define _S (_Satom->S) #define _F (_Fatom->F) #define _CTX (_CTXatom->CTX) +#define _E (_Eatom->E) +#define _EENV (_Eatom->ENV) #define _ARG1(ARG1,...) ARG1 -#define _ASSERT(EXPR) assert(EXPR) /* later... _cll1_assert(EXPR,handler) */ +//#define _ASSERT(EXPR) (expr?expr:fail(CONSISTENCY_FAILURE)) +#define _SYSFAIL(E) longjmp(_EENV,(E)) #define _GET_MEM(TYPE) ((TYPE)malloc(sizeof(struct _##TYPE##_struct))) /* later... ((TYPE)_cll1_malloc(sizeof(struct _##TYPE##_struct))) */ -#define _ANYATOM(TYPE,NAME) def_mem(_##NAME##Atom ) { TYPE NAME; int c; list( _##NAME##Atom ); } -#define _FOR_COLUMNS(A,B,C) _NEW(I,0) for( prefix(A,B,C) ; A ; ((A!=B)&&prefix(A,B,C)||(A=NULL) ),_I++ ) -#define _FOR_VALID_COLUMNS(A,B,C) _NEW(I,0) for( prefix(A,B,C) ; A ; ((A!=B)&&prefix(A,B,C))||(A=NULL) ) if(*A&&++_I) +#define _ANYATOM(NAME,DECL) def_mem(_##NAME##Atom ) { DECL; int c; list( _##NAME##Atom ); } +//#define _FOR_COLUMNS(A,B,C) _NEW(I,0) for( prefix(A,B,C) ; A ; ((A!=B)&&prefix(A,B,C)||(A=NULL) ),_I++ ) +//#define _FOR_VALID_COLUMNS(A,B,C) _NEW(I,0) for( prefix(A,B,C) ; A ; ((A!=B)&&prefix(A,B,C))||(A=NULL) ) if(*A&&++_I) #define _NEW(NAME,VALUE) for(_##NAME##atom=get_mem(_##NAME##Atom),_##NAME##atom->NAME=(VALUE),_##NAME##atom->c=1,push(_##NAME##atom,_##NAME##stack);_##NAME##atom->c||(pop(_##NAME##atom,_##NAME##stack),0);_##NAME##atom->c--) #define _ANYSTACK(NAME) _##NAME##Atom _##NAME##atom,_##NAME##stack=NULL; -#define _EXPORT_STACKS _ANYSTACK(I) _ANYSTACK(J) _ANYSTACK(S) _ANYSTACK(F) _ANYSTACK(CTX) -_ANYATOM(int,I); -_ANYATOM(int,J); -_ANYATOM(str,S); -_ANYATOM(FILE *,F); -_ANYATOM(void *,CTX); +#define _EXPORT_STACKS _ANYSTACK(I) _ANYSTACK(S) _ANYSTACK(F) _ANYSTACK(CTX) _ANYSTACK(E) +_ANYATOM(I, int I;int J); +_ANYATOM(S, str S); +_ANYATOM(F, file F); +_ANYATOM(CTX, void *CTX); +_ANYATOM(E, int E;jmp_buf ENV); /* .----------------------------------------------------------------------. / 11. C<<1 memory contexts - alternative to garbage collection '----------------------------------------------------------------------- */ @@ -372,9 +379,26 @@ obsolete #define _IMPL_FUNCTIONS /*_MALLOC*/ _EXPORT_OUTS _EXPORT_ARGS _EXPORT_STRS #define _IMPL_INTERFACE //_SQL_IFC _CAKE_IFC _STD_IFC _X_IFC #define _IMPLEMENT_CLL1_LIBRARY _INIT_VARIABLES _IMPL_FUNCTIONS _IMPL_INTERFACE +//this will be probably accomplished some other way... +//#ifndef DISABLE_CLL1_LIBRARY_IMPLEMENTATION +_IMPLEMENT_CLL1_LIBRARY +//#endif #define _PROGRAM _cll1_program #define _DECLARE_PROGRAM void _PROGRAM(int argc, char **argv) -#define program _IMPLEMENT_CLL1_LIBRARY _DECLARE_PROGRAM; int main(int argc, char **argv) { LANG=setlocale(LC_ALL, ""); _PROGRAM(argc,argv); return 0; } _DECLARE_PROGRAM +#define program \ +_DECLARE_PROGRAM; \ +int main(int argc, char **argv) \ +{ \ + LANG=setlocale(LC_ALL, ""); \ + try \ + { \ + _PROGRAM(argc,argv); \ + return 0; \ + } \ + else \ + return -1; \ +} \ +_DECLARE_PROGRAM #ifdef USE_PREPROCESSOR #include "_include.c" #endif diff --git a/demos/exceptions/advanced-failures.c b/demos/exceptions/advanced-failures.c new file mode 100644 index 0000000..1f1f674 --- /dev/null +++ b/demos/exceptions/advanced-failures.c @@ -0,0 +1,66 @@ +#include "cll1.h" + +void private_method(int i) +{ + if(i==2) + { + fail(LOCAL_FAILURE); + } + else if(i==6) + { + fail(EVIL_FAILURE); + } + else + { + printf("nuda #%d\n",i); + } +} + +void some_job(int i) +{ + try + { + private_method(i); + private_method(i+1); + private_method(i+2); + } + else_switch_failures + { + case EVIL_FAILURE: puts("[serious error]"); pass_failure; + case LOCAL_FAILURE: puts("[non-fatal error detected]"); break; + default : puts("[sorry, vole, error]"); + } +} + +program +{ + + try + { + some_job(1); + some_job(3); + some_job(6); + some_job(2); + some_job(7); + } + else + { + puts("sorry, vole, error!"); + } + + try + { + some_job(1); + some_job(2); + some_job(4); + some_job(7); + } + else_catch(EVIL_FAILURE) + { + puts("totaly screwed."); + } + else + { + puts("ahem... something went wrong."); + } +} diff --git a/demos/exceptions/exceptions.c b/demos/exceptions/exceptions.c deleted file mode 100644 index de07215..0000000 --- a/demos/exceptions/exceptions.c +++ /dev/null @@ -1,58 +0,0 @@ -#include "cll1.h" - -void pokus(int i) -{ - if(i==2) - { - fail(666); - } - if(i==7) - { - pokus(2); - } - if(i==6) - { - fail(0); - } - printf("nuda #%d\n",i); -} - -program -{ - try - { - pokus(1); - pokus(2); - pokus(6); - pokus(7); - } - else_switch_exceptions - { - case 666: puts("chyba 666"); break; - default : puts("sorry, vole, error!"); - } - - try - { - pokus(1); - pokus(3); - pokus(6); - pokus(2); - pokus(7); - } - else_switch_exceptions - { - case 666: puts("chyba 666"); break; - default : puts("sorry, vole, error!"); - } - - try - { - pokus(1); - pokus(7); - } - else_catch_exception(666) - { - puts("tak, a basta."); - } -} diff --git a/demos/exceptions/failures.c b/demos/exceptions/failures.c new file mode 100644 index 0000000..2338f7f --- /dev/null +++ b/demos/exceptions/failures.c @@ -0,0 +1,75 @@ +#include "cll1.h" + +void some_function(int i) +{ + if(i==2) + { + fail(EVIL_FAILURE); + } + if(i==7) + { + some_function(2); + } + if(i==6) + { + fail(LOCAL_FAILURE); + } + printf("nuda #%d\n",i); +} + +program +{ + try + { + some_function(1); + some_function(2); + some_function(6); + } + else_switch_failures + { + case EVIL_FAILURE: puts("serious failure detected!"); break; + default: puts("sorry, vole, error!"); + } + + try + { + some_function(1); + some_function(3); + some_function(6); + some_function(2); + } + else + { + puts("sorry, vole, error!"); + } + + try + { + some_function(1); + some_function(6); + } + else_catch(EVIL_FAILURE) + { + puts("Totally screwed."); + exit(-1); + } + else + { + puts("We survived this failure."); + } + + try + { + some_function(3); + some_function(7); + } + else_catch(EVIL_FAILURE) + { + puts("totally screwed."); + exit(-1); + } + else + { + puts("we survived this failure."); + } +} diff --git a/demos/repeat.c b/demos/repeat.c index db3726a..491b74d 100644 --- a/demos/repeat.c +++ b/demos/repeat.c @@ -9,7 +9,7 @@ program print("This is demonstration, that nesting of C<<1 repeat(N) { ... } flow-control macro is completely safe:"); repeat(i++) { - echo("Outer loop is supposed to be repeated twice: "); + echo("This loop is supposed to be repeated twice: "); repeat(i) { printf("[doing this %d times] ",i); @@ -31,6 +31,6 @@ program } repeat(--i) { - print("Last loop is not nested and it is supposed to be repeated again just twice."); + print("This loop is not nested and it is supposed to be repeated again just twice."); } } -- 2.30.2