From: xchaos Date: Fri, 15 Aug 2008 17:07:49 +0000 (+0000) Subject: some unfinished work X-Git-Url: https://git.harvie.cz/?a=commitdiff_plain;h=1a8d78d9e9f1a4a2e8b7c3b740c5516195fbc04b;p=svn%2FCll1h%2F.git some unfinished work git-svn-id: https://dev.arachne.cz/repos/cll1h/trunk@75 4bb87942-c103-4e5a-b51c-0ebff58f8515 --- diff --git a/cll1.h b/cll1.h index 1f4ddbf..0b404ed 100644 --- a/cll1.h +++ b/cll1.h @@ -92,8 +92,8 @@ extern unsigned RANDOM_SEED; / 4. C<<1 data declaration and allocation, updated 2008-01-31 xCh. '----------------------------------------------------------------------- */ #define def_mem(TYPE) typedef struct _##TYPE##_t * TYPE; struct _##TYPE##_t -#define get_mem(...) _GET_MEM(__VA_ARGS__,NULL) -#define Get_mem(ID,...) _ARG1(__VA_ARGS__,) ID=_GET_MEM(__VA_ARGS__,NULL) +#define get_mem(TYPE) _GET_MEM(TYPE) +#define Get_mem(ID,TYPE) TYPE ID=_GET_MEM(TYPE) #define mem_len(TYPE) sizeof(struct _##TYPE##_t) //#define mem_copy #define list(TYPE) TYPE __next @@ -173,24 +173,24 @@ extern unsigned RANDOM_SEED; /* .----------------------------------------------------------------------. / 8. C<<1 object oriented infrastructure, updated 2008-02-04 '----------------------------------------------------------------------- */ -#define def_type(TYPE) typedef struct _##TYPE##_t * TYPE; -#define def_obj(TYPE) struct _##TYPE##_t -#define construct_obj(MEM,INTERFACE) MEM _init_##MEM##_##INTERFACE(INTERFACE i) { Get_mem(o,MEM); o->__interface=i; return o; } -#define anonymous void * -#define I_am(MEM) MEM self=(MEM)community -#define def_community(TYPE,INTERFACE) def_mem(TYPE) { INTERFACE __interface; }; +#define def_type(TYPE) typedef struct _##TYPE##_t * TYPE; +#define def_obj(TYPE) struct _##TYPE##_t #define method(NAME) (*NAME) -#define get_obj(MEM,INTERFACE,INIT) _init_##MEM##_##INTERFACE(INIT) +#define construct(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)((_PTR=get_mem(MEM),(_PTR?((MEM)_PTR)->__interface=INTERFACE:0),_PTR)) -#define Get_obj_as(ID,COMMUNITY,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 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) { /*temporary solution*/ 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) @@ -228,7 +228,7 @@ extern _DECLARE_STRSTR; #define _CTX (_CTXatom->CTX) #define _ARG1(ARG1,...) ARG1 #define _ARG2(ARG1,ARG2,...) ARG2 -#define _GET_MEM(TYPE,CTX) ((TYPE)_cll1_malloc(sizeof(struct _##TYPE##_t),CTX)) +#define _GET_MEM(TYPE) ((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) diff --git a/demos/objects/objects-wiki.c b/demos/objects/objects-wiki.c index e55cb74..127f959 100644 --- a/demos/objects/objects-wiki.c +++ b/demos/objects/objects-wiki.c @@ -7,7 +7,7 @@ def_type(Animal); def_mem(Actions) { - Animal method(init) (Animal self, str name); + void method(init) (Animal self, str name); str method(talk) (Animal self); }; @@ -17,6 +17,12 @@ def_obj(Animal) char *name; }; +Animal construct(Animal) (Animal self, str name) +{ + self->name = name; + return self; +} + str cat_talk(Animal self) { return "Meow!"; @@ -27,22 +33,18 @@ str dog_talk(Animal self) return "Arf! Arf!"; } -Animal animal_init(Animal self, str name) -{ - self->name = name; - return self; -} - -Actions cat_actions(Actions this) +Actions cat_actions(void) { - this->init = animal_init; + Actions this=get_mem(Actions); this->talk = cat_talk; + return this; } -Actions dog_actions(Actions this) +Actions dog_actions(void) { - this->init = animal_init; + Actions this=get_mem(Actions); this->talk = dog_talk; + return this; } program @@ -52,13 +54,12 @@ program Actions cat = cat_actions(); Actions dog = dog_actions(); - animal[0] = init_obj(Animal, Actions, cat) - "Missy"); - animal[1] = init_obj(Animal, cat, "Mr. Bojangles"); - animal[2] = init_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])); + printf("%s: %s\n", animal[i]->name, _(talk, animal[i])); } }