From 47e4883a12f36914d419e9e5fd1a0ff97227c093 Mon Sep 17 00:00:00 2001 From: xchaos Date: Fri, 15 Aug 2008 21:22:55 +0000 Subject: [PATCH] #3 FINALY !!! objects-wiki.c compiles (with -Wall) and runs ! git-svn-id: https://dev.arachne.cz/repos/cll1h/trunk@76 4bb87942-c103-4e5a-b51c-0ebff58f8515 --- cll1.h | 20 +++++++++++--------- demos/objects/objects-wiki.c | 32 +++++++++++++++++--------------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/cll1.h b/cll1.h index 0b404ed..9f1ca5f 100644 --- a/cll1.h +++ b/cll1.h @@ -176,21 +176,22 @@ extern unsigned RANDOM_SEED; #define def_type(TYPE) typedef struct _##TYPE##_t * TYPE; #define def_obj(TYPE) struct _##TYPE##_t #define method(NAME) (*NAME) -#define construct(TYPE) _init_##TYPE(TYPE o,void *i) { o->__interface=i; return o; } TYPE __init__##TYPE +#define construct(TYPE) TYPE _init_##TYPE(TYPE o,void *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(ID,MEM,INTERFACE) MEM ID=get_obj(MEM,INTERFACE) #define get_obj_as(COMMUNITY,MEM,INTERFACE) (COMMUNITY)get_obj(MEM,INTERFACE) #define Get_obj_as(ID,COMMUNITY,MEM,INTERFACE) MEM ID=get_obj_as(COMMUNITY,MEM,INTERFACE) -#define _(METHOD,...) ( _ASSERT(ARG1(__VA_ARGS__,)), (*( ARG1(__VA_ARGS__,)->__interface->METHOD )) ( __VA_ARGS__ ) ) -#define __(METHOD,...) ( _ASSERT(ARG1(__VA_ARGS__,)), (*( __cll1_seekmethod(ARG1(__VA_ARGS__,)->__interface,METHOD) )) ( __VA_ARGS__ ) ) -#define call(OBJTYPE,METHOD,...) { int _N=0; OBJTYPE _OBJ; /* <-' */ while( (_OBJ=(OBJTYPE)_cll1_for_ptrs(_N++,__VA_ARGS__,NIL))!=(struct OBJTYPE *)NIL ) _(_OBJ,METHOD); } +//#define _(METHOD,...) /*_ASSERT(ARG1(__VA_ARGS__,)), */(*( ARG1(__VA_ARGS__,)->__interface->METHOD )) ( __VA_ARGS__ ) +#define _(METHOD,OBJ) ( OBJ?(*( OBJ->__interface->METHOD )) ( OBJ ):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_mem(TYPE) { INTERFACE __interface; }; #define anonymous void * #define I_am(MEM) MEM self=(MEM)community /* .----------------------------------------------------------------------. / 9. implementation of C<<1 library functions, updated 2008-01-26 xCh. '----------------------------------------------------------------------- */ -#define _MALLOC void *_cll1_malloc(size_t size) { /*temporary solution*/ return malloc(size); } +//#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); } #define _DECLARE_PRINT void _cll1_print(char *s,...) #define _PRINT _DECLARE_PRINT _CLL1_FPRINT(stdout) @@ -227,8 +228,9 @@ extern _DECLARE_STRSTR; #define _F (_Fatom->F) #define _CTX (_CTXatom->CTX) #define _ARG1(ARG1,...) ARG1 -#define _ARG2(ARG1,ARG2,...) ARG2 -#define _GET_MEM(TYPE) ((TYPE)_cll1_malloc(sizeof(struct _##TYPE##_t))) +//#define _ARG2(ARG1,ARG2,...) ARG2 +#define _ASSERT(EXPR) assert(EXPR) //later: _cll1_assert(EXPR,handler) +#define _GET_MEM(TYPE) ((TYPE)malloc(sizeof(struct _##TYPE##_t))) //((TYPE)_cll1_malloc(sizeof(struct _##TYPE##_t))) #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) @@ -342,13 +344,13 @@ obsolete //bind_exception, unbind_exception //#define _CLL1_THROW(ERR) //#define _CLL1_TRY _NEW_E for(bind_exception(0,NULL);;) if () {.... if(_Eatom->SYS) { if(_Eatom->RET) return; else break;} else { } -//#define _ASSERT(EXPR) assert(EXPR) + /* .----------------------------------------------------------------------. / 98. We have have done our homework - now let's have some fun: '----------------------------------------------------------------------- */ #define _INIT_VARIABLES _EXPORT_GLOBALS _EXPORT_YES _EXPORT_STACKS _SQL_GLOBALS _CGI_GLOBALS -#define _IMPL_FUNCTIONS _MALLOC _EXPORT_OUTS _EXPORT_ARGS _EXPORT_STRS +#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 #define _PROGRAM _cll1_program diff --git a/demos/objects/objects-wiki.c b/demos/objects/objects-wiki.c index 127f959..50e1288 100644 --- a/demos/objects/objects-wiki.c +++ b/demos/objects/objects-wiki.c @@ -1,48 +1,50 @@ #include "cll1.h" /* This is C<<1 rewrite of object polymorphism from - http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming */ - -def_type(Animal); + http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming + + Of course there are easier ways to do the same thing... :-) + (G)2008 xChas */ def_mem(Actions) { - void method(init) (Animal self, str name); - str method(talk) (Animal self); + str method(talk) (anonymous self); }; -def_obj(Animal) +def_mem(Animal) { interface(Actions); char *name; }; -Animal construct(Animal) (Animal self, str name) +construct(Animal) (Animal self, str name) { self->name = name; return self; } -str cat_talk(Animal self) +str cat_talk(anonymous self) { return "Meow!"; } -str dog_talk(Animal self) +str dog_talk(anonymous self) { return "Arf! Arf!"; } Actions cat_actions(void) { - Actions this=get_mem(Actions); + Get_mem(this,Actions); + this->talk = cat_talk; return this; } Actions dog_actions(void) { - Actions this=get_mem(Actions); + Get_mem(this,Actions); + this->talk = dog_talk; return this; } @@ -54,12 +56,12 @@ program Actions cat = cat_actions(); Actions dog = dog_actions(); - animal[0] = get_obj(Animal, cat, "Missy"); - animal[1] = get_obj(Animal, cat, "Mr. Bojangles"); - animal[2] = get_obj(Animal, dog, "Lassie"); + animal[0] = get_obj(Animal,cat,"Missy"); + animal[1] = get_obj(Animal,cat,"Mr. Bojangles"); + animal[2] = get_obj(Animal,dog,"Lassie"); for_range(i, 0, 2) { - printf("%s: %s\n", animal[i]->name, _(talk, animal[i])); + print(animal[i]->name,":",_(talk,animal[i])); } } -- 2.30.2