/ 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
/* .----------------------------------------------------------------------.
/ 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)
#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)
def_mem(Actions)
{
- Animal method(init) (Animal self, str name);
+ void method(init) (Animal self, str name);
str method(talk) (Animal self);
};
char *name;
};
+Animal construct(Animal) (Animal self, str name)
+{
+ self->name = name;
+ return self;
+}
+
str cat_talk(Animal self)
{
return "Meow!";
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
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]));
}
}