- /* .----------------------------------------------------------------------.
+ /* .---------------------------------------------------------------------.
/ C<<1 header file - C language style sheet - GNU C/ANSI C99 compliant
/ Cumbersome Extraction and Report Language - http://cll1h.arachne.cz/
'---------------------------------------------------------------------- */
typedef int bool;
extern str NIL,EOL,OFS,IFS;
extern unsigned RANDOM_SEED;
-#define _EXPORT_GLOBALS str NIL="(null)", EOL="\n", OFS=" ", IFS=" \t\n"; unsigned RANDOM_SEED=0;
+#define _EXPORT_GLOBALS str NIL="(null)", EOL="\n", OFS=" ", IFS=" \t\n"; unsigned RANDOM_SEED=0; void *_PTR;
/* .----------------------------------------------------------------------.
/ 1. C<<1 then buddhist section, updated 2008-02-04 xCh.
'----------------------------------------------------------------------- */
//#define atob(EXPR) (word_in_stri(YES,(EXPR))?1:0)
//#define _EXPORT_YES str YES="TRUE\nYES\nJA\nOUI\nDA\nANO\nY\n1";
//#else
-#define atob(EXPR) (eqi((EXPR),YES)?1:0)
+#define atob(EXPR) (eqi((EXPR),YES)?TRUE:FALSE)
#define _EXPORT_YES str YES="TRUE";
#endif
/* .----------------------------------------------------------------------.
/ 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 construct_mem(TYPE) TYPE _init_##TYPE(void) { create_mem(this,TYPE); /* <-' */ _init_##TYPE(this); return this; } _init_##TYPE(TYPE this)
#define get_mem(...) _GET_MEM(__VA_ARGS__,NULL)
-#define init_mem(...) _init_##_ARG1(__VA_ARGS__,)( _ARG2(__VA_ARGS__,,) - 0 )
-#define create_mem(ID,...) _ARG1(__VA_ARGS__,) ID=_GET_MEM(__VA_ARGS__,NULL)
-#define new_mem(ID,...) _ARG1(__VA_ARGS__,) ID=init_mem(__VA_ARGS__)
+#define Get_mem(ID,...) _ARG1(__VA_ARGS__,) ID=_GET_MEM(__VA_ARGS__,NULL)
#define mem_len(TYPE) sizeof(struct _##TYPE##_t)
-//#define mem_copy
+//#define mem_copy
#define list(TYPE) TYPE __next
#define tree(TYPE) TYPE __next; TYPE __seek
#define dict(TYPE) tree(TYPE); str __key
'----------------------------------------------------------------------- */
#define push(NODE,HEAD) ( NODE ? ((NODE->__next=HEAD),(HEAD=NODE)) : NULL )
#define pop(NODE,HEAD) ( HEAD ? ((NODE=HEAD->__next),free(HEAD),(HEAD=NODE)) : (NODE=NULL) )
-#define append(NODE,HEAD) { void *N=NODE; /* <-' */ NODE->__next=NULL; for_search(NODE,HEAD,!NODE->__next) { NODE->__next=N; break; } }
+#define append(NODE,HEAD) { _PTR=NODE; NODE->__next=NULL; for_search(NODE,HEAD,!NODE->__next) { NODE->__next=_PTR; break; } }
#define remove(NODE,HEAD,EXPR) { void **_D=NULL; /* <-' */ for_search(NODE,HEAD,EXPR) { if(_D)*_D=NODE->__next; else HEAD=NODE->__next; free(NODE); } else _D=(void *)&(NODE->__next); }
#define drop(NODE,HEAD) for( NODE=HEAD; NODE || (HEAD=NULL); HEAD=NODE, NODE=NODE->__next, free(HEAD) )
//to do: seek
/* .----------------------------------------------------------------------.
/ 8. C<<1 object oriented infrastructure, updated 2008-02-04
'----------------------------------------------------------------------- */
-/* too non-C: #define I_must_be(TYPE) TYPE self, void *_method * */
-#define member_of(TYPE) TYPE _we
-#define anonymous void *_we
-#define I_am(TYPE) TYPE self=(TYPE)_we
-#define def_community(METATYPE,ITYPE) def_mem(METATYPE) { ITYPE __interface; };
-#define def_method(RET,NAME,...) RET (*NAME)(__VA_ARGS__)
-#define construct_interface(MTYPE,ITYPE) ITYPE _init,_##MTYPE##ITYPE(void *dummy, ...) { new_mem(_FACE,ITYPE); va_list(ap); _##MTYPE##ITYPE##_init(_FACE); return _FACE; } void _##MTYPE##ITYPE##_init(IFACE _FACE)
-#define bind_method(METHOD,FUNCTION) _FACE->METHOD=FUNCTION
-#define new_interface(ID,MTYPE,...) _ARG1(__VA_ARGS__,) ID=_init_##MTYPE##_ARG1(__VA_ARGS__,) ((void *)init_##MTYPE##__VA_ARGS__,)
-#define init_interface(TYPE,...)
-#define construct_object(MTYPE,...) (void *)_init_##TYPE##_()
-#define init_object(TYPE,...) (TYPE)_init_##TYPE##_(__VA_ARGS__)
-#define init_polymorph(TYPE,COMMUNITY,...) (COMMUNITY)_init_##TYPE##_(__VA_ARGS__)
+#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 method(NAME) (*NAME)
+#define get_obj(MEM,INTERFACE,INIT) _init_##MEM##_##INTERFACE(INIT)
+#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 _(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); }
#ifdef USE_MODULE_CGI
#define cgi(MIME) { char *PTR; /* <-' */ recycle_context(_CGI); _CGIdict=NULL; for_split_str(PTR,getenv("QUERY_STRING"),'&') /* _init_cgi */ if(MIME) printf("Content-type: %s\r\n\r\n",MIME); }
#define _GETCGI char *getcgi(VAR) { }
-#define _CGI_GLOBALS def_context(_CGI); struct _CGIVariable { char *V; dictionary(_CGIVariable); } *_CGIvariable,*_CGIdict;
+#define _CGI_GLOBALS def_context(_CGI); struct _CGIVariable { char *V; dict(_CGIVariable); } *_CGIvariable,*_CGIdict;
#else
#define _GETCGI
#define _CGI_GLOBALS
program
{
- Record record,records=NULL;
+ Record record, records=NULL;
- print("4x insert(record,records,order_by,i);");
+ print("4x insert(record,records,order_by_int,i);");
record=get_mem(Record);
record->i=1;
record->s="aaaa";
insert(record,records,order_by_int,i);
- for_each(record,records) printf("i=%d, s=%s\n",record->i,record->s);
+ for_each(record,records)
+ {
+ printf("i=%d, s=%s\n",record->i,record->s);
+ }
print("1x append(record,records);");
record=get_mem(Record);
record->i=0;
record->s="ccc";
append(record,records);
- for_each(record,records) printf("i=%d, s=%s\n",record->i,record->s);
+
+ for_each(record,records)
+ {
+ printf("i=%d, s=%s\n",record->i,record->s);
+ }
print("sort(record,records,order_by_int_desc,i);");
sort(record,records,order_by_int_desc,i);
- for_each(record,records) printf("i=%d, s=%s\n",record->i,record->s);
+
+ for_each(record,records)
+ {
+ printf("i=%d, s=%s\n",record->i,record->s);
+ }
print("sort(record,records,order_by_str,s);");
sort(record,records,order_by_str,s);
- for_each(record,records) printf("i=%d, s=%s\n",record->i,record->s);
+
+ for_each(record,records)
+ {
+ printf("i=%d, s=%s\n",record->i,record->s);
+ }
print("sort(record,records,order_by_ascii_desc,s);");
sort(record,records,order_by_ascii_desc,s);
- for_each(record,records) printf("i=%d, s=%s\n",record->i,record->s);
+
+ for_each(record,records)
+ {
+ printf("i=%d, s=%s\n",record->i,record->s);
+ }
print("remove(record,records,record->i==1);");
- remove(record,records,record->i==1);
- for_each(record,records) printf("i=%d, s=%s\n",record->i,record->s);
+ remove(record,records,record->i==1);
+
+ for_each(record,records)
+ {
+ printf("i=%d, s=%s\n",record->i,record->s);
+ }
print("drop(record,records);");
drop(record,records);
- for_each(record,records) printf("i=%d, s=%s\n",record->i,record->s);
+
+ for_each(record,records)
+ {
+ printf("i=%d, s=%s\n",record->i,record->s);
+ }
}
--- /dev/null
+#include <cll1h>
+
+def_community(Drawable_object,Actions);
+
+def_mem(Actions)
+{
+ void method(init) (Drawable_object self, ...);
+ void method(draw) (Drawable_object self);
+ int method(desc) (Drawable_object self,int something);
+};
+
+def_mem(Tri)
+{
+ interface(Actions);
+ int x1, y1, x2, y2, x3, y3;
+};
+
+def_mem(Rect)
+{
+ interface(Actions);
+ int x1, y1, x2, y2;
+};
+
+construct_obj(Tri,Actions);
+
+construct_obj(Rect,Actions);
+
+
+def_mem(Object_list)
+{
+ Drawable_object object;
+ list(Object_list);
+};
+
+Actions init_tri_interface(Actions this)
+{
+ this->init = init_tri;
+ this->draw = draw_tri;
+ this->desc = desc_tri;
+
+ return this;
+}
+
+Actions init_rect_interface(Actions this)
+{
+ this->init = init_tri;
+ this->draw = draw_rect;
+ this->desc = desc_rect;
+
+ return this;
+}
+
+void draw_tri(Drawable_object community)
+{
+ I_am(Tri);
+
+ printf("imagine I am drawing %d,%d - %d,%d - %d,%d\n",
+ self->x1,self->y1,self->x2,self->y2,self->x3,self->y3);
+};
+
+void draw_rect(Drawable_object community)
+{
+ I_am(Rect);
+
+ printf("imagine I am drawing %d,%d - %d,%d\n",
+ self->x1,self->y1,self->x2,self->y2);
+};
+
+int desc_tri(Drawable_object community,int something)
+{
+ I_am(Tri);
+
+ printf("I am triangle %d,%d - %d,%d - %d,%d, method draw called with argument: %d\n",
+ self->x1,self->y1,self->x2,self->y2,self->x3,self->y3,something);
+
+ return something;
+};
+
+int desc_rect(Drawable_object community,int something)
+{
+ I_am(Rect);
+
+ printf("I am rectangle %d,%d - %d,%d, method draw called with argument: %d\n",
+ self->x1,self->y1,self->x2,self->y2,something);
+
+ return something;
+};
+
+Tri init_tri(Drawable_object self, int x1, int y1, int x2, int y2, int x3, int y3)
+{
+ I_am(Tri);
+
+ self->x1 = x1;
+ self->y1 = y1;
+ self->x2 = x2;
+ self->y2 = y2;
+ self->x3 = x3;
+ self->y3 = y3;
+
+ return self;
+}
+
+Rect init_rect(Drawable_object self, int x1, int y1, int x2, int y2)
+{
+ I_am(Rect);
+
+ self->x1 = x1;
+ self->y1 = y1;
+ self->x2 = x2;
+ self->y2 = y2;
+
+ return self;
+}
+
+program
+{
+ Init_mem(tri_interface,Actions,init_tri_interface);
+ Init_mem(rect_interface,Actions,init_rect_interface);
+ Object_list all = NULL, one = get_mem(Object_list);
+
+ one->object = init_obj_as(Drawable_data, Rect, rect_interface, 0, 10, 1, 11);
+ append(one,all);
+ one->object = init_obj_as(Drawable_data, Tri, tri_interface, 0, 0, 0, 4, 3, 0);
+ append(one,all);
+ one->object = init_obj_as(Drawable_data, Rect, rect_interface, 10, 0, 11, 1);
+ append(one,all);
+
+ for_each(one,all)
+ {
+ printf("(return value %d)\n", _(one->object,desc));
+ _(one->object,draw);
+ }
+}
-#include <cll1.h>
+#include "cll1.h"
/* This is C<<1 rewrite of object polymorphism from
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming */
-struct Animal
+def_type(Animal);
+
+def_mem(Actions)
{
- use_interface(AnimalActions);
- char *name;
+ Animal method(init) (Animal self, str name);
+ str method(talk) (Animal self);
};
-struct AnimalActions
+def_obj(Animal)
{
- const char *(method(talk));
-}
+ interface(Actions);
+ char *name;
+};
-struct *Animal get_animal(char *name, struct AnimalActions *interface)
+str cat_talk(Animal self)
{
- struct *Animal animal=get_object(Animal,interface);
- animal->name=name;
- return animal;
+ return "Meow!";
}
-const char *cat_talk(void *_self)
+str dog_talk(Animal self)
{
- return "Meow!";
+ return "Arf! Arf!";
}
-const char *dog_talk(void *_self)
+Animal animal_init(Animal self, str name)
{
- return "Arf! Arf!";
+ self->name = name;
+ return self;
}
-interface_implementation(CatActions,AnimalActions)
+Actions cat_actions(Actions this)
{
- bind_method(talk,cat_talk);
+ this->init = animal_init;
+ this->talk = cat_talk;
}
-interface_implementation(DogActions,AnimalActions)
+Actions dog_actions(Actions this)
{
- bind_method(talk,dog_talk);
+ this->init = animal_init;
+ this->talk = dog_talk;
}
program
{
int i;
- struct Animals *animal[3];
- struct AnimalActions *cat = get_interface(CatActions);
- struct AnimalActions *dog = get_interface(DogActions);
+ Animal animal[3];
+ Actions cat = cat_actions();
+ Actions dog = dog_actions();
- animal[0]=get_animal("Missy",cat);
- animal[0]=get_animal("Mr. Bojangles",cat);
- animal[0]=get_animal("Lassie",dog);
-
- for_range(i,0,2)
- printf("%s: %s\n",animal[i].name,(_(animal[i],talk)));
+ animal[0] = init_obj(Animal, Actions, cat)
+ "Missy");
+ animal[1] = init_obj(Animal, cat, "Mr. Bojangles");
+ animal[2] = init_obj(Animal, dog, "Lassie");
+ for_range(i, 0, 2)
+ {
+ printf("%s: %s\n", animal[i].name, _(talk, animal[i]));
+ }
}