All pastes #929695 Raw Edit

KigForth, early attempt

public c v1 · immutable
·published 2008-03-05 22:47 UTC
rendered paste body
#define V(x) void x()#define TR(x) rsp--;*rsp=x#define FR(x) x=*rsp;rsp++#define T sp[0]#define S sp[1]#define D sp++typedef void(*fp)();typedef union CELL{int i;fp f;CELL*p;char*s}C;// word: name, prev, code, data// w.p sometimes points to current word (i.e. its name)// ip points to data: an array of cells that point to wordsC *sp,*rsp,*ip,w;V(tl){for(;;)(w.p=*ip)[2]();}V(co){TR(ip);ip=w.p[3].p;}#define W(n,p,N,c) V(f_##n){c;ip.p++;} C[]n{N,p,f_##n};#define B(n,p,N,o) W(n,p,N,S.i=T.i o S.i;D)W(re,0 ,0       ,FR(ip))W(du,re,"dup"   ,sp++;sp[0]=sp[1])W(sw,du,"swap"  ,w=S;S=T;T=w)W(ro,sw,"rot"   ,w=sp[2];sp[2]=S;S=T;T=w)W(dr,ro,"drop"  ,D)W(fe,dr,"@"     ,T=*T.p)W(st,fe,"!"     ,*T.p=T)W(li,st,0       ,--sp=(++ip.p))W(pr,li,","     ,putchar(T);D)W(hw,pr,0       ,printf("hello, world\n"))B(ad,hw,"+"     ,+)B(su,ad,"-"     ,-)B(mu,su,"*"     ,*)B(di,mu,"/"     ,/)B(an,di,"and"   ,&)B(or,ad,"or"    ,|)B(xo,or,"xor"   ,^)W(ha,xo,"halt"  ,exit(0))V(main){C m[]={hw,ha,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};sp=m+5;rsp=m+10;ip=m;w=0;tl();}