From 7e4d757514e0d9904d6b31b5a08760eff306a9f2 Mon Sep 17 00:00:00 2001 From: xchaos Date: Tue, 30 Sep 2008 19:07:44 +0000 Subject: [PATCH 1/1] feature #6 - preliminary implementation, cannot nest yet git-svn-id: https://dev.arachne.cz/repos/cll1h/trunk@95 4bb87942-c103-4e5a-b51c-0ebff58f8515 --- cll1.h | 12 +++++++- demos/exceptions/exceptions.c | 58 +++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 demos/exceptions/exceptions.c diff --git a/cll1.h b/cll1.h index 6f49f72..48aa0a1 100644 --- a/cll1.h +++ b/cll1.h @@ -197,7 +197,17 @@ extern unsigned RANDOM_SEED; #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. implementation of C<<1 library functions, updated 2008-01-26 xCh. + / 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; +/* .----------------------------------------------------------------------. + / 10. implementation of C<<1 library functions, updated 2008-01-26 xCh. '----------------------------------------------------------------------- */ /* later... #define _MALLOC void *_cll1_malloc(size_t size) { return malloc(size); } */ #define _CLL1_FPRINT(IOSTREAM) { int ofs=0; va_list ap; /* <-' */ for(va_start(ap,s);s!=NIL;s=va_arg(ap,char *)) { if(ofs)fputs(OFS,IOSTREAM);else ofs=1;/**/fputs(coalesce(s,NIL),IOSTREAM); } va_end(ap); fputs(EOL,IOSTREAM); } diff --git a/demos/exceptions/exceptions.c b/demos/exceptions/exceptions.c new file mode 100644 index 0000000..de07215 --- /dev/null +++ b/demos/exceptions/exceptions.c @@ -0,0 +1,58 @@ +#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."); + } +} -- 2.30.2