Playing with SymPy design in C
From SymPy
This is playground for SymPy like design in C.
#include "stdlib.h" #include "stdio.h" //----------------------- basic --------------------------------- typedef struct _basic { void (*print)(); struct _basic *args; int nargs; } basic; void basic_print(basic *b) { if (b->nargs != 0) { printf("basic_print nargs\n"); } else printf("basic_print\n"); } void basic_init(basic *b) { b->print = basic_print; b->nargs = 0; } basic *basic_new() { basic *b; b = malloc(sizeof(basic)); basic_init(b); return b; } //------------------------ add ------------------------------------------- typedef struct { basic super; int i,j; } add; void add_print(add *p) { printf("add_print %d %d\n", p->i, p->j); } basic *add_new(int a, int b) { add *p; p = malloc(sizeof(add)); basic_init((basic *)p); p->super.print = add_print; p->super.nargs = 1; p->i = a; p->j = b; return (basic *)p; } // ------------------------------------------------------------ int main() { basic *b; b = basic_new(); b->print(b); b = add_new(1, 2); b->print(b); printf("OK"); return 0; }
output:
$ ./basic basic_print add_print 1 2
