Will 6 månader sedan
förälder
incheckning
87badbc90d
8 ändrade filer med 550 tillägg och 10 borttagningar
  1. 6 6
      TTExample.csv
  2. 3 0
      example_hardcode.k
  3. BIN
      k
  4. 47 0
      kjr/v2/a.c
  5. 170 0
      kjr/v2/a.h
  6. 35 0
      kjr/v2/a.k
  7. 237 0
      repl.k
  8. 52 4
      transpiler.py

+ 6 - 6
TTExample.csv

@@ -1,11 +1,11 @@
 tempST,State Table,,,,,,
 States/Events,,E1,E2,E3,E4,E5,E6
-Off,S1,"4, S5","1, S2","2, S3","3, S4","5, S6","-, S1"
-FanLow,S2,"4, S5","-, S2","2, S3","3, S4","5, S6","6, S1"
-FanMedium,S3,"4, S5","1, S2","-, S3","3, S4","5, S6","6, S1"
-FanHigh,S4,"4, S5","1, S2","2, S3","-, S4","5, S6","6, S1"
-Heating,S5,"-, S5","1, S2","2, S3","3, S4","5, S6","6, S1"
-Cooling,S6,"4, S5","1, S2","2, S3","3, S4","-, S6","6, S1"
+Off,S1,"4, 5","1, 2","2, 3","3, 4","5, 6","-, 1"
+FanLow,S2,"4, 5","-, 2","2, 3","3, 4","5, 6","6, 1"
+FanMedium,S3,"4, 5","1, 2","-, 3","3, 4","5, 6","6, 1"
+FanHigh,S4,"4, 5","1, 2","2, 3","-, 4","5, 6","6, 1"
+Heating,S5,"-, 5","1, 2","2, 3","3, 4","5, 6","6, 1"
+Cooling,S6,"4, 5","1, 2","2, 3","3, 4","-, 6","6, 1"
 Events,,E1,E2,E3,E4,E5,E6
 T < 18,C1,Y,N,N,N,N,N
 T >= 18 & T < 22,C2,N,Y,N,N,N,N

+ 3 - 0
example_hardcode.k

@@ -0,0 +1,3 @@
+/ instantiate, represent, evaluate
+tempST: {x+1}
+:tempST

BIN
k


+ 47 - 0
kjr/v2/a.c

@@ -0,0 +1,47 @@
+#include"a.h"//k(c)2024 arthur whitney(l)MIT ! #$%&'()*+,-./:;<=>?@[\]^_{|}~ [rrit]n A
+vV(b_,o(pmovmskb)(a))vV(b2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[nx-1-i])))f(qr,Qr(1)0)f(ty,t(3,px?0:tx?:Tx))g(LMS,QZ)
+Z_(h,ff,qr,qr,qr,qr,no,dt,gi,qr,ra,gE,_t,ge,v_,$)Z_(H,ff,rr,_o,o_,og,no,dt,n_,_v,ou,_n,_t,or,v_,$)
+g(a1,P(20>f,Qr(px&&17>f-1)$6(f,x,3==tx?++x:jJ(1,ti(1),x),3==tx?--x:jJ(1,ti(-1),x),jJ(3,r_(x),x),j4(-1,x),(tx?h:H)[f-5](x)))LMS(f-20,x))Z_(G,No,Dt,N_,0,0,0,0,_C,C_,qr)
+G(a2,P(20>f,P(pa||px,Qr(13!=f)a1(a,x))10>f?jJ(f,a,x):4>f-13?(15>f?AA:NN)(1&f-1,a,x):G[f-10](a,x))Qz(29>f)f-=29;!f&&9>a-11?vk(f1(H[a-5],x)):$3(ta-2,c3,i3,p3)(f,a,x))U(v,_r(*_);*_=x,U*_,Ux)
+#define A() (_&&23<j?_+j-24:D+j)
+U(z_,Ua=x;x=xx;Ur;_W(ij=xc%32;Q(r=*c=$5(xc/32,--c;20>j?j:ti(j-20),j?a1(j,r):(--i,j=xc%32,r_(v(A(),r))),a2(j,r,*++c),--c;r_(*A()),--c;a_(j,a))))*c++,U*_,Ux)
+Zc*P_=" L OSDE]  CARBK^@ GIHNM _ QP F J",*_P=":+-*%&|<>=~.!@?#_^,$LMSAVD...'/\\;()[]";f(_p,B3(90+(V){},10>O-20,O+28,96<O,O,a0(*(V*)_P,O%32)))g(_bp,_(bp(f,x)))
+f(p_,B3(O,10>O-48,O-28,26>O-97,O,bl(26>O-65,55+(V){},a0(*(V*)P_,O-B-(57<O&10)-(90<O&26)-(122<O&27))-B)))f(Ba,0)f(a$,0)f(Bp,0)f(p$,0)f(Bd,0)f(d$,0)f(Bb,0)f(b$,0)f(t3,qg(58,x)?2:qg(46,x)||qg(101,x))
+f(Bq,qg(34,x)?_bp(0,bC(34==O)):0)f(Bt,_bp(5,bC(Vv=10>O-48;v|s(-1,v)&s(1,v)&(46==O|58==O|101==O|B==O))))g(qn,_bp(f,bC(1!=O/32&224>O)))f(Bn,qn(5,x))f(Bo,bC(3>O-61))f(Bv,qn(7,x))
+f(t$,P(qg(B,x),x=Ss(vc(B,x));it=0;n(t=m(t,t3(xU)))v0(3+t,fr(_$,t,x)))_$(t3(x),x))f(n$,V_(nz(n-1,tc(77)),x))f(r$,r(rr(x),*sr+=32))f(v$,B==sx[1]?x:r$(x))f(o$,*sx-=1==*sx/32?32:0;r$(x))
+#define c(z) _(cc[256];Ur=(U)c;ns(e(z)-(U)c,c))
+Z_(P,Bq,Bt,Ba,Bp,Bd,Bb,Bn,Bo,Bv)Z_(R,0,t$,a$,p$,d$,b$,n$,o$,v$)ZU rd();g(fd,Ua=P[f](x);P(!a,x)a=n_(f?_bp(10,a):a);P(!na,_r(a);x)rd(f,a,x))f(pz,i(6,x=fd(3+i,x))x)
+G(rd,_ $=R[f];c(ij=0;i(na,ik=au-1;r=dn(r,k-j,x+j);j=++i<na?au:nx;S[n]=a(ns(j-k-2*!$,sx+k+!$),$?$(a):a);*sr++=(3>f?128:7>f?192:224)+n++)dn(r,nx-j,x+j)))
+F(pS,n(IF(192>xc,*sa++=xc)a=pS(a,S[xc%32]))a)Us(ps,Ux=xs(s);n=1;i(3,x=fd(i,x))x=p_(x);x=pz(x);x=c(pS(r,x));K(n,i?S[i]:x))
+Us(zs,P(92==*s,ys(s+1))ii=is(s[1],2,": ");P(qa(*s)&&2>i,v(D+*s%32,(i?ps:zs)(s+2));0)Qx(ps)_(z_(0,x)))Us(os,P(!*s||39==*s,0)Qx(zs)x?wx(x):0)Us(ls,Qx(_0)n(Ua=xU;Q(na?os(sa):_(96)))_(0))
+int main(int n,char**_){A=(U)$>N;M[29]=m_(0,n0<<30,3,A?0x1042:0x4022,0,0);M_(0);W=0;if(*++_)ls(*_);cc[256];W(1)wc(B),c[_w(0,(U)c,256)-1]=0,*c?os(c):0;}
+Zc*pP="+-*sqr %sqrt &flip |flop <asc >desc =group ~ . !index @first ?nub  #count _floor ^order ,$\n+-*    %     &and  |or   <    >     =      ~ . !index @at    ?find #take  _drop  ^cut   ,$";
+g(ts,Q(x)Ua=ut();i(f,Q(_r(z_(0,x))))_(ti(((ut()-a)/(A?2.4e3:2.4e6)-f/2e5))))Us(ys,if(!*s)_k(0);$5(is(*s,4,"ltvw"),ls(s+2),ii=is(B,20,s);si=0;ts(i?zs(s+2):1,ps(s+i+1)),QZ,ti(W),xs(pP)))
+f(_z,px||tx||!Lx?$(x):qf(x)?$(_v(x)):sS(B,f1(_z,x)))f(wx,x=sS(10,px||tx||Tx||qf(x)?v_(_z(x)):f1(_z,x));w_(2,x,nx);_(0))

+ 170 - 0
kjr/v2/a.h

@@ -0,0 +1,170 @@
+#define _s(o)   i(l0-2,a=o##i(a,s(4<<i,a))) //a=o##i(a,s(4,a));a=o##i(a,s(8,a));a=o##i(a,s(16,a));a=o##i(a,s(32,a))
+#define s_(t,o) a(t##0,i(nN(Nx),a=o##i(a,O))_s(o)a)[b(l0-2)]
+#define S_(t,o) e(V##t b=t##0;N(V##t a=O;_s(o)b=a+b[b(l0-2)]))
+#define A() (_&&23<j?_+j-24:D+j)
+#define B3(b,c,d,e,f,g) _N(bl(127<O,b,bl(c,d,bl(e,f,g))))
+#define l0 5
+#define ZU static U//abc.efg.ij.lmno..r.t...x..
+#define ZV static V//..C..FG......NOPQR.TUVW...
+#define AV(n) __attribute((vector_size(1<<n),aligned(1)))
+typedef char V AV(l0);typedef unsigned u,vu AV(l0-1),Vu AV(l0);typedef unsigned long(*_)(),U,U4 AV(4),VU AV(l0),UU AV(l0+1);typedef int Vi AV(l0);typedef float Ve AV(l0);
+ZU w_(U,...),prr(),b_(),T(),_r(),r_(),a2(),ys(),pz(),wx(),_p(),z_(),W,M[30],D[32],S[1<<10],*c=S+(1<<10);static Ve e0;static VU U0;static Vu u0,I2={0,1,2,3,4,5,6,7}; //,8,9,10,11,12,13,14,15};
+ZV I0={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};//,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63};
+#define D(g,z) U(g,z,_ $,Ua,Ux)
+#define o(f) bu(ia32_##f##256) 
+#define n0 (1L<<l0)
+#define nN(z) (n0-1+(z)>>l0)
+#define bI(z)    R(1,nx,i(nN(Nx),rc=b2(e(z))))
+#define bC(z)    R(1,nx,i(nN(Nx),ru=b_(e(z))))
+#define K(n,z)   R(0,n,i(x(r,nx),rU=e(z)))
+#define L(t,n,z) R(t,n,i(x(r,nN(Nx)),rV=e(z)))
+#define _(z) r(e(z),_r(x))
+#define __(z) r(e(z),_r(x);_r(a))
+#define a(z,y) e(typeof(z)a=z;y)
+#define b(z) ((1l<<(z))-1)
+#define e(_) ({_;})
+#define f(g,z) U(g,z,Ux)
+#define g(g,z) U(g,z,uf,Ux)
+#define i(a,z) {unsigned _n=a;ii=-1;W(++i<_n){z;}}
+#define j(a,z) {unsigned _n=a;ij=-1;W(++j<_n){z;}}
+#define l(a,z) e(typeof(z)e_=z;(a)<e_?(a):e_)
+#define m(a,z) e(typeof(z)e_=z;(a)>e_?(a):e_)
+#define n(z)   i(nx,z)
+#define r(z,y) e(typeof(z)r=z;y;r)
+#define t(t,x) ((U)(t)<<61|(x))
+#define x(z,y) e(typeof(z)x=z;y)
+#define C(t,x) bu(convertvector)(x,t)
+#define F(g,z) U(g,z,Ua,Ux)
+#define G(g,z) U(g,z,uf,Ua,Ux)
+#define N(z) L(Tx,nx,z)
+#define O ((V*)x)[i]
+#define P(b,z) if(e(b))return e(z);
+#define Q(z...) P(96==(z),96)
+#define R(t,n,z)  r(T(t,n),z)
+#define U(g,z,x...) ZU g(x){return e(z);}
+#define V(g,z,x...) ZV g(x){return e(z);}
+#define W(z) while(e(z))
+#define _N(z)     _(N(z))
+#define _K(n,z)   _(K(n,z))
+#define _R(t,n,z) _(R(t,n,z))
+#define _T(t,z)   _(L(t,nx,z))
+#define q3(o) Qd(1!=f&&1<f-5)f-=5;P(Bx,b##o(f,x))Qz(2!=Lx)
+#define i0(a,b) o(pshufb)(a,b)
+#define i2(a,b) o(pshufd)(a,b)
+#define VF(g,z) V(g,z,Va,Vb)
+#define ai(a,b) (a)+(b)
+#define v1($,z) f($,tx?e(Q(x=$(  v_(x)))_v(x)):Tx?e(z):f1($,  x))
+#define V1($,z) F($,tx?e(Q(x=$(a,v_(x)))_v(x)):Tx?e(z):fr($,a,x))
+#define bu(f) __builtin_##f
+#define AN(f,s,x...) __attribute((naked))U f(x){asm(s);}
+#define _W(z) ii=nx;W(i--){z;}
+#define Ui(g,z) U(g,z,ii)
+#define Us(g,z) U(g,z,ss)
+#define Zs(g,a,x...) static char*g(x){return e(a);}
+#define nb(z) bu(popcountl)(z)
+#define ib(z) bu(ctzl)(z)
+#define lb(z) bu(clzl)(z)
+#define a3(t) $3(f-1,t##a+t##x,t##a-t##x,t##a*t##x)
+#define b3(a,x) $3(f-7,(a)<x,(a)>x,(a)==x)
+#define N6(z) i(63+nx>>6,z)
+#define nS(g,z) U(g,z,in,ss)
+#define ve(g,z) U(g,z,ee)
+#define vV(g,z) U(g,z,Va)
+#define IU(a) r(ib(a),a&=a-1)
+#define Z_(f,x...) static _ f[]={x};
+#define Qx(f) Ux=f(s);Q(x)
+#define Qs(e,s) P(e,err((U)__func__,(U)s))
+#define QZ e(Qz(1)0)
+#define Nx tn(Tx,nx)
+#define Uu     C(VU,((vu*)x)[i])
+#define _u (Ve)C(Vu,((UU*)x)[i])
+#define u_ (Ve)C(Vu,((UU*)x)[i]>>32)
+#define Ix (Vu)Cx
+#define Ex (Ve)Cx
+#define Ux U x
+#define Vv V v
+#define Va V a
+#define Vb V b
+#define ax a(x,xx)
+#define xx *UX
+#define mx sx[-6]
+#define R_ UR[-1]
+#define si s[i]
+#define xb sx[i/32]
+#define Ca x(a,tx?cx:Cx)
+#define Ia x(a,tx?ux:Ix)
+#define Ea x(a,tx?ex:Ex)
+#define ca x(a,cx)
+#define AU r_(aU)
+#define XU r_(xU)
+#define UR ((U*)r)
+#define UA ((U*)a)
+#define UX ((U*)x)
+#define aU ((U*)a)[i]
+#define rU ((U*)r)[i]
+#define xU ((U*)x)[i]
+#define na ((u*)a)[-1]
+#define nx ((u*)x)[-1]
+#define xc sx[i]
+#define rc (sr)[i]
+#define ru ((u*)r)[i]
+#define xu ((u*)x)[i]
+#define rV ((V*)r)[i]
+#define aV ((V*)a)[i]
+#define sr (char*)r
+#define sa (char*)a
+#define sx ((char*)x)
+#define rx ((u*)x)[-2]
+#define Tx sx[-5]
+#define Bx (1==Tx)
+#define tx (x>>61)
+#define Cx O
+#define ss char*s
+#define cc char c
+#define ee float e
+#define uf unsigned f
+#define ii unsigned i
+#define ij unsigned j
+#define ik unsigned k
+#define im unsigned m
+#define in unsigned n
+#define it unsigned t
+#define Ur U r
+#define Ua U a
+#define Un U n
+#define oo ws("oo\n")
+#define x_ sx[nx-1]
+#define Zc static char
+#define Zu static u
+#define S(f,i) static AN(f,"mov %rcx,%r10;mov $"#i",%rax;syscall;ret",Ux,...)
+AN(_start,"lea 8(%rsp),%rsi;call main")AN(ut,"rdtsc;shl $32,%rdx;or%rdx,%rax;ret")S(_w,0)S(w_,1)S(f_,2)S(_f,3)S(_l,4)S(m_,9)S(_k,60)
+Zc*_P,B=32;Zu n,A,M1=65535,N=1<<31,L[]={3,0,0,2,2,3},g=95<<23;static float E[]={1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9};Ui(tb,t(1,i))Ui(tc,t(2,i))Ui(ti,t(3,i))f(l2,64-lb(x-1))
+ve(ue,*(u*)&e)ve(te,t(4,ue(e)))ve(eq,1-1e-6<e&&1+1e-6>e)f(qe,2==Tx&&32>x_%128)f(qf,!Tx&&qe(xx))g(tn,1==f?7+x>>3:x<<L[f])f(sb,ii=nx/64;if(nx%64)xU&=b(nx%64);r(0,N6(r+=nb(xU))))
+Ui(g_,ij=N&i;j>>2|b(29)&m(g,i)-g)f(G_,g_(x)|g_(x>>32)<<30)Ui(_g,ij=i&b(29);j?N&i<<2|j+g:0)f(_G,_g(x)|_g(x>>30)<<32)f(z4,64/4-lb(x)/4)f(q0,10>x-48)f(qa,26>x-97)
+Us(sl,ii=0;W(si)++i;i)Zs(ws,w_(2,(U)s,sl(s));s,ss)f(wc,w_(2,(U)&x,1))F(err,if(a)ws(sa);wc(58);ws(sx);wc(10);96)U(OO,ws("OO\n");_k(0))
+U(dn,i(nN(n),rV=O)r+n,Ur,Un,Ux)U(is,i(nN(n),Ua=b_(c==O);P(a,l(n,n0*i+ib(a))))n,cc,in,V*x)g(ig,is(f,nx,sx))g(qg,nx>ig(f,x))nS(ns,R(2,n,dn(r,n,(U)s)))Us(xs,ns(sl(s),s))
+#define ix ux
+#define ia ua
+#define cx (char)x
+#define px (128>x)
+#define lx L[tx]
+#define Lx L[Tx]
+#define ux (unsigned)x
+#define ex a(ux,*(float*)&a)
+#define au x(a,xu)
+#define pa x(a,px)
+#define ta x(a,tx)
+#define ua x(a,ux)
+#define ea x(a,ex)
+#define Ta x(a,Tx)
+#define Qz(e) Qs(e,"nyi")
+#define Qr(e) Qs(e,"rank")
+#define Qt(e) Qs(e,"type")
+#define Qn(e) Qs(e,"count")
+#define Qd(e) Qs(e,"domain")
+#define $3(z,a,b,c)       e(unsigned y=z;!y?e(a):1==y?e(b):e(c))
+#define $4(z,a,b,c,d)     e(unsigned y=z;!y?e(a):1==y?e(b):2==y?e(c):e(d))
+#define $5(z,a,b,c,d,x)   e(unsigned y=z;!y?e(a):1==y?e(b):2==y?e(c):3==y?e(d):e(x))
+#define $6(z,a,b,c,d,x,f) e(unsigned y=z;!y?e(a):1==y?e(b):2==y?e(c):3==y?e(d):4==y?e(x):e(f))
+#define C6(z,a,b,c,d,e,f) switch(z){case 0:{a;break;}case 1:{b;break;}case 2:{c;break;}case 3:{d;break;}case 4:{e;break;}default:{f;}}
+#define IF(z,a) if(z){a;}else  //f(wi,cc[21];ss=c+20;*s=10;do*--s=48+x%10;W(x/=10);w_(2,s,c+21-s);x)f(Y,wx(r_(x));x)

+ 35 - 0
kjr/v2/a.k

@@ -0,0 +1,35 @@
+m:1000000
+\t:m 2
+\t:m ----------2
+\t:m 2-2-2-2-2-2
+\t:m @,@,@,@,@,2
+v:!4000
+m:_1e9%#v
+\t:m 2=v
+\t:m +/v
+\t:m |/v
+\t:m 2+v
+\t:10?m 2%v
+\t:10?m %v
+\t:10?m +\v
+
+todo
+Ia2E3  !(;;)
+Waa:-a (:a)/..
+(:a)/(:a:a-1)
++
++/
+x+*
++/'!'2 3
+A1>_#9
+A2>_(x+*)/x:#9
+
+a'b while(a)b
+A'B if(a)b if(c)d else e 
+2b|~2b
+?n
+^?<>= 
+x.y
+sql
+^x [a/deepmind]
+

+ 237 - 0
repl.k

@@ -0,0 +1,237 @@
+#!k
+\d repl
+banner:"ngn/k, (c) 2019-2024 ngn, GNU AGPLv3. type \\ for more info\n"
+prompt:," " /use 0x0720 for emacs integration
+(opn;sem;cls):"(;)"
+(opn;sem;cls):("( ";" ; ";" )") /uncomment this line for extra spacing
+lines:cols:999
+upd:{(lines;cols)::`I$"\n"\`x(("/usr/bin/env";"tput";"-S");"lines\ncols\n")}
+lim:{(x<#y)(x#)/y}
+dd:{(x<#y)(,[;".."](x-2)#)/y}
+fmt:{upd[];$[(::)~x;"";(1<#x)&|/`m`M`A=@x;mMA x;(dd[cols]`k@lim[cols]x),"\n"]}
+fmtx:{$[(::)~x;"";`k[x],"\n"]}
+mMA:{(P;f;q):((,"!/+(";dct;,")");(("+![";" +(");tbl;")]");(,,"(";lst;,")"))`m`M`A?t:@x
+ w:cols-#*P;u:w-#q;h:lines-2
+ x:$[h<(`M=t)+#x;,[;,".."](h-1)#f[w;u;h#x];f[w;u;x]]
+ ,[;"\n"]@"\n"/@[;-1+#x;,;q]P[!#x],'x}
+lst:{[w;u;x](((-1+#x)#w),u)dd'`k'lim[cols]'x}
+dct:{[w;u;x]k:(|/#'k)$k:`k'!x;par'(((-1+#x)#w-3),u-3)dd'sem/'+(k;`k'.x)}
+tbl:{[w;u;x]h:`k'!x;d:`k''.x;W:(#'h)|/'#''d
+ r:,$[`S~@!x;dd[w](""opn),(""sem)/;par@dd[w-2]@sem/]W$'h
+ r,par'dd[w-2]'sem/'+@[W;&~^`i`d?_@'.x;-:]$'d}
+cell:{$[|/`i`d=@y;-x;x]$z}
+par:{opn,x,cls}
+cmds:(,"a")!{`1:1:joinpath[dirname`argv 0]"LICENSE";}
+basename:{*|"/"\x}
+dirname:{$[#x:"/"/-1_"/"\x;x;,"."]}
+joinpath:{$[x~,".";y;"/"~*|x;x,y;x,"/",y]}
+line0:{c:{0x07~*-2#*x}{(l;r):x;(1:1;r,,(-2_l))}/(x;());"\n"/(*|c),,*c}
+line1:{$[#x;;:0];x:-1_x;$[(3>#x)&("\\"=*x)&~^(!cmds)?x 1;cmds[x 1]x 1;.[`1:(fmt;fmtx)[" "~*x]@.:;,x;{`0:`err[]}]];`1:prompt;1}
+line:line1@line0@
+$["repl.k"~basename`argv 1;{cmds::@[cmds;x[1]1;:;{y;`0:x}2_x]}'{(&x~\:80#"-")_x:(1+*&x~\:,"/")_-1_x}@0:`argv 1;];
+run:{`1:banner,prompt;{line@1:`}::/`;}
+\d .
+
+$[(~"kw"~`argv 0)&~"--non-interactive"~*x;repl.run[];]
+
+/
+--------------------------------------------------------------------------------
+
+\   help               \\         exit
+\a  license(AGPLv3)    \l file.k  load
+\0  types              \d foo     set namespace; restore with  \d .
+\+  verbs              \t:n expr  time(elapsed milliseconds after n runs)
+\:  I/O verbs          \v         variables
+\'  adverbs            \f         functions
+\`  symbols            \cd path   change directory
+\h  summary            \other     command(through /bin/sh)
+--------------------------------------------------------------------------------
+\0
+Types:
+list atom
+ `A        generic list   ()   ,"ab"   (0;`1;"2";{3};%)
+ `I   `i   int            0N -9223372036854775807 01b
+ `F   `f   float          -0w -0.0 0.0 0w 1.2e308 0n
+ `C   `c   char           "a"   0x6263   "d\0\"\n\r\t"
+ `S   `s   symbol         `   `a   `"file.txt"   `b`cd`"ef"
+ `M   `m   table&dict     +`a`b!(0 1;"23")   (0#`)!()
+      `o   lambda         {1+x*y#z}  {[a;b]+/*/2#,a,b}
+      `p   projection     1+   {z}[0;1]   @[;i;;]
+      `q   composition    *|:   1+{x*y}@
+      `r   derived verb   +/   2\   {y,x}':
+      `u   monadic verb   +:   0::
+      `v   dyadic  verb   +   0:
+      `w   adverb         '   /:
+      `x   external func
+--------------------------------------------------------------------------------
+\:
+I/O verbs
+  0:x read  lines
+x 0:y write lines
+  1:x read  bytes
+x 1:y write bytes
+   <s open          fd:<`"file.txt"
+   >i close         >fd
+
+x can be a file descriptor (int) or symbol or string such as
+ "file.txt"
+ "/path/to/file"
+ "host:port"
+ ":port"         /host defaults to 127.0.0.1
+--------------------------------------------------------------------------------
+\+
+Verbs:    : + - * % ! & | < > = ~ , ^ # _ $ ? @ . 0: 1:
+notation: [c]har [i]nt [n]umber(int|float|char) [s]ymbol [a]tom [d]ict
+          [f]unc(monad) [F]unc(dyad) [xyz]any
+special:  var:y     set    a:1;a -> 1
+          var::y    global a:1;{a::2}[];a -> 2
+          (v;..):y  unpack (b;(c;d)):(2 3;4 5);c -> 4
+          :x        return {:x+1;2}[3] -> 4
+          :[x;y;..] cond   :[0;`a;"\0";`b;`;`c;();`d;`e] -> `e
+          o[..]     recur  {:[x<2;x;+/o'x-1 2]}9 -> 34
+          [..]      progn  [0;1;2;3] -> 3
+
+::  self      f:(::);f 12 -> 12
+ :  right     f:(:);f[1;2] -> 2   "abc":'"d" -> "ddd"
+ +x flip      +("ab";"cd") -> ("ac";"bd")
+N+N add       1 2+3 -> 4 5
+ -N negate    - 1 2 -> -1 -2
+N-N subtract  1-2 3 -> -1 -2
+ *x first     *`a`b -> `a   *(0 1;"cd") -> 0 1
+N*N multiply  1 2*3 4 -> 3 8
+ %N sqrt      %25 -> 5.0   %-1 -> 0n
+N%N divide    2 3%4 -> 0.5 0.75
+ !i enum      !3 -> 0 1 2   !-3 -> -3 -2 -1
+ !I odometer  !2 3 -> (0 0 0 1 1 1;0 1 2 0 1 2)
+ !d keys      !`a`b!0 1 -> `a`b
+ !S ns keys   a.b.c:1;a.b.d:2;!`a`b -> `c`d
+x!y dict      `a`b!1 2 -> `a`b!1 2
+i!I div       -10!1234 567 -> 123 56
+i!I mod       10!1234 567 -> 4 7
+ &I where     &3 -> 0 0 0   &1 0 1 4 2 -> 0 2 3 3 3 3 4 4
+ &x deepwhere &(0 1 0;1 0 0;1 1 1) -> (0 1 2 2 2;1 0 0 1 2)
+N&N min/and   2&-1 3 -> -1 2   0 0 1 1&0 1 0 1 -> 0 0 0 1
+ |x reverse   |"abc" -> "cba"   |12 -> 12
+N|N max/or    2|-1 3 -> 2 3   0 0 1 1|0 1 0 1 -> 0 1 1 1
+ <X ascend    <"abacus" -> 0 2 1 3 5 4
+ >X descend   >"abacus" -> 4 5 3 1 0 2
+ <s open      fd:<`"/path/to/file.txt"
+ >i close     >fd
+N<N less      0 2<1 -> 1 0
+N>N more      0 1>0 2 -> 0 0
+ =X group     ="abracadabra" -> "abrcd"!(0 3 5 7 10;1 8;2 9;,4;,6)
+ =i unitmat   =3 -> (1 0 0;0 1 0;0 0 1)
+N=N equal     0 1 2=0 1 3 -> 1 1 0
+ ~x not       ~(0 2;``a;"a \0";::;{}) -> (1 0;1 0;0 0 1;1;0)
+x~y match     2 3~2 3 -> 1   "4"~4 -> 0   0~0.0 -> 0
+ ,x enlist    ,0 -> ,0   ,0 1 -> ,0 1   ,`a!1 -> +(,`a)!,,1
+x,y concat    0,1 2 -> 0 1 2  "a",1 -> ("a";1)
+d,d merge     (`a`b!0 1),`b`c!2 3 -> `a`b`c!0 2 3
+ ^x null      ^(" a";0 1 0N;``a;0.0 0n) -> (1 0;0 0 1;1 0;0 1)
+a^y fill      1^0 0N 2 3 0N -> 0 1 2 3 1   "b"^" " -> "b"
+X^y without   "abracadabra"^"bc" -> "araadara"
+ #x length    #"abc" -> 3   #4 -> 1   #`a`b`c!0 1 0 -> 3
+i#y take      5#"abc" -> "abcab"   -5#`a`b`c -> `b`c`a`b`c
+X#d take keys `c`d`f#`a`b`c`d!1 2 3 4 -> `c`d`f!3 4 0N
+I#y reshape   2 3#` -> (```;```)
+f#y replicate (3>#:')#(0;2 1 3;5 4) -> (0;5 4)   {2}#"ab" -> "aabb"
+ _n floor     _12.34 -12.34 -> 12 -13
+ _c lowercase _"Ab" -> "ab"
+i_Y drop      2_"abcde" -> "cde"   -2_`a`b`c -> ,`a
+X_d drop keys `a`c_`a`b`c!0 1 2 -> (,`b)!,1
+I_Y cut       2 4 4_"abcde" -> ("cd";"";,"e")
+f_Y weed out  (3>#:')_(0;2 1 3;5 4) -> ,2 1 3
+X_i delete    "abcde"_2 -> "abde"
+ $x string    $(12;"ab";`cd;+) -> ("12";(,"a";,"b");"cd";,"+")
+i$C pad       5$"abc" -> "abc  "   -3$"a" -> "  a"
+s$y cast      `c$97 -> "a"   `i$-1.2 -> -1   `$"a" -> `a
+s$y int       `I$"-12" -> -12
+ ?X distinct  ?"abacus" -> "abcus"
+ ?i uniform   ?2 -> 0.6438163747387873 0.8852656305774402 /random
+X?y find      "abcde"?"bfe" -> 1 0N 4
+i?x roll      3?1000 -> 11 398 293   1?0 -> ,-8164324247243690787
+i?x deal      -3?1000 -> 11 398 293 /guaranteed distinct
+ @x type      @1 -> `i   @"ab" -> `C   @() -> `A   @(@) -> `v
+x@y apply(1)  {x+1}@2 -> 3   "abc"@1 -> "b"   (`a`b!0 1)@`b -> 1
+ .S get       a:1;.`a -> 1   b.c:2;.`b`c -> 2
+ .C eval      ."1+2" -> 3
+ .d values    .`a`b!0 1 -> 0 1
+x.y apply(n)  {x*y+1}. 2 3 -> 8   (`a`b`c;`d`e`f). 1 0 -> `d
+
+@[x;y;f]   amend  @["ABC";1;_:] -> "AbC"   @[2 3;1;{-x}] -> 2 -3
+@[x;y;F;z] amend  @["abc";1;:;"x"] -> "axc"   @[2 3;0;+;4] -> 6 3
+.[x;y;f]   drill  .[("AB";"CD");1 0;_:] -> ("AB";"cD")
+.[x;y;F;z] drill  .[("ab";"cd");1 0;:;"x"] -> ("ab";"xd")
+.[f;y;f]   try    .[+;1 2;"E:",] -> 3   .[+;1,`;"E:",] -> "E:'type\n"
+?[x;y;z]   splice ?["abcd";1 3;"xyz"] -> "axyzd"
+--------------------------------------------------------------------------------
+\`
+Special symbols:
+   `j?C parse json   `j?"{\"a\":1,\"b\":[true,\"c\"]}" -> `a`b!(1.0;(1;,"c"))
+   `k@x pretty-print `k("ab";2 3) -> "(\"ab\";2 3)"
+   `p@C parse k
+ `hex@C hexadecimal  `hex"ab" -> "6162"
+ `pri@i primes       `pri 20  ->  2 3 5 7 11 13 17 19
+   `x@x fork-exec    `x(("/bin/wc";"-l");"a\nbc\nd\n") -> "3\n"
+   `t[] current time (microseconds)
+`argv[] list of cmd line args (also in global variable x)
+ `env[] dict of env variables
+`prng[] `prng@I get/set pseudo-random number generator internal state
+                     s:`prng[];r:9?0;`prng s;r~9?0 -> 1
+        `prng@0 use current time to set state
+ `err@C throw error
+ `sin@N trigonometry `sin 12.34 -> -0.22444212919135995
+ `exp@N exponential  `exp 1 -> 2.7182818284590455
+  `ln@N logarithm    `ln 2 -> 0.6931471805599453
+`exit@i exit
+--------------------------------------------------------------------------------
+\'
+Adverbs:   ' / \ ': /: \:
+   f' each1     #'("abc";3 4 5 6) -> 3 4
+ x F' each2     2 3#'"ab" -> ("aa";"bbb")
+   X' binsearch 1 3 5 7 9'8 9 0 -> 3 4 -1
+   F/ fold      +/1 2 3 -> 6
+   F\ scan      +\1 2 3 -> 1 3 6
+ x F/ seeded /  10+/1 2 3 -> 16
+ x F\ seeded \  10+\1 2 3 -> 11 13 16
+ i f/ n-do      5(2*)/1 -> 32
+ i f\ n-dos     5(2*)\1 -> 1 2 4 8 16 32
+ f f/ while     (1<){:[2!x;1+3*x;-2!x]}/3 -> 1
+ f f\ whiles    (1<){:[2!x;1+3*x;-2!x]}\3 -> 3 10 5 16 8 4 2 1
+   f/ converge  {1+1.0%x}/1 -> 1.618033988749895
+   f\ converges (-2!)\100 -> 100 50 25 12 6 3 1 0
+   C/ join      "ra"/("ab";"cadab";"") -> "abracadabra"
+   C\ split     "ra"\"abracadabra" -> ("ab";"cadab";"")
+   I/ decode    24 60 60/1 2 3 -> 3723   2/1 1 0 1 -> 13
+   I\ encode    24 60 60\3723 -> 1 2 3   2\13 -> 1 1 0 1
+  i': window    3':"abcdef" -> ("abc";"bcd";"cde";"def")
+i f': stencil   3{x,"."}':"abcde" -> ("abc.";"bcd.";"cde.")
+  F': eachprior -':12 13 11 17 14 -> 12 1 -2 6 -3
+x F': seeded ': 10-':12 13 11 17 14 -> 2 1 -2 6 -3
+x F/: eachright 1 2*/:3 4 -> (3 6;4 8)
+x F\: eachleft  1 2*\:3 4 -> (3 4;6 8)
+--------------------------------------------------------------------------------
+\h
+: SET      RETURN    :[c;t;f]     COND
++ add      flip
+- subtract negate    '  each|slide|bin
+* multiply first     /  fold|join |dec|comment
+% divide   sqrt      \  scan|split|enc|trace
+! mod|dict enum|key  ': eachprior
+& min|and  where     /: eachright
+| max|or   reverse   \: eachleft
+< less     ascend
+> more     descend   /
+= equal    group     multiline comment
+~ match    not       \
+, concat   enlist
+^ without  null      0: lines i/o
+# reshape  length    1: bytes i/o
+_ drop|cut floor
+$ cast     string
+? find|rnd uniq      ?[a;i;b]     splice
+@ apply(1) type      @[x;i;[f;]y] amend
+. apply(n) eval      .[x;i;[f;]y] drill
+grammar:  E:E;e|e e:nve|te| t:n|v v:tA|V n:t[E]|(E)|{E}|N
+limits:   8args 16locals 256globals 256bytecode 2048stack
+\

+ 52 - 4
transpiler.py

@@ -1,7 +1,55 @@
 import csv
 
-with open('TTExample.csv') as csvfile:
-    input = csv.reader(csvfile)
+# read state table csv
+def parse_st_csv(file_path):
+    with open(file_path, newline='') as csvfile:
+        reader = csv.reader(csvfile)
+        data = [row for row in reader]
 
-    for row in input:
-        print(row)
+    state_table = []
+    events = []
+    actions = []
+
+    section = None
+    for row in data:
+        if row[0] == 'States/Events':
+            section = 'state_table'
+            continue
+        elif row[0] == 'Events':
+            section = 'events'
+            continue
+        elif row[0] == 'Actions':
+            section = 'actions'
+            continue
+
+        if section == 'state_table':
+            state_table.append(row)
+        elif section == 'events':
+            events.append(row)
+        elif section == 'actions':
+            actions.append(row)
+
+    return state_table, events, actions
+
+# state table transpiler
+def transpile_st(state_table, events, actions):
+    # actions 0-n (starting from 0)
+    actions = [a[0] for a in actions]
+
+    # conditions for events
+    conditions = [e[0] for e in events]
+
+    # events on conditions
+    events = [e[2:] for e in events]
+
+    # state names
+    states = [s[0] for s in state_table]
+
+    # columns of state table - event_states[0] is E1
+    # this is used because action/nextstate is choosed based on event
+    event_states = [[erows[i] for erows in state_table] for i in range(2, len(state_table[2]))]
+
+    print(event_states)
+
+state_table, events, actions = parse_st_csv('TTExample.csv')
+transpile_st(state_table, events, actions)