Permalink
Browse files

Add optimized over adverb for +*|& - #137 closes #86

  • Loading branch information...
1 parent 06f5527 commit 9322284ac29c56fdc6c0e2f1112ea2bb12105438 @kevinlawler kevinlawler committed Sep 7, 2011
Showing with 195 additions and 133 deletions.
  1. +184 −120 k.c
  2. +1 −1 km.c
  3. +8 −11 kx.c
  4. +2 −1 ts.h
View
304 k.c
@@ -271,129 +271,193 @@ K show(K a)
R a;
}
+K plus_over(K x,K y)
+{
+ I accI=0; F accF=0;
+ I yt=y->t,yn=y->n;
+ if((!yt&&yn) || ABS(yt) > 2 || (x && xt!=1 && xt!=2)) R 0;
+ K z,r;
+ SW(ABS(yt)) //May want to consider casting y->t==1 to F's when x && x->t is F
+ {
+ CS(2,if(yn)accF=*kF(y);DO(y->n-1, accF=accF+kF(y)[i+1]) z=Kf(accF)) //CS order matters for empty list ()
+ CD: if(yn)accI=*kI(y);DO(y->n-1, accI=accI+kI(y)[i+1]) z=Ki(accI); //CS 1
+ }
+ U(z)
+ if(x){r=plus(x,z); cd(z); z=r;}
+ R z;
+}
+
+K times_over(K x,K y)
+{
+ I accI=1; F accF=1;
+ I yt=y->t,yn=y->n;
+ if((!yt&&yn) || ABS(yt) > 2 || (x && xt!=1 && xt!=2)) R 0;
+ K z,r;
+ SW(ABS(yt)) //May want to consider casting y->t==1 to F's when x && x->t is F
+ {
+ CS(2,if(yn)accF=*kF(y);DO(y->n-1, accF=accF*kF(y)[i+1]) z=Kf(accF)) //CS order matters for empty list ()
+ CD: if(yn)accI=*kI(y);DO(y->n-1, accI=accI*kI(y)[i+1]) z=Ki(accI); //CS 1
+ }
+ U(z)
+ if(x){r=times(x,z); cd(z); z=r;}
+ R z;
+}
+
+K max_or_over(K x,K y)
+{
+ I accI=0; F accF=-FI;
+ I yt=y->t, yn=y->n;
+ if((!yt&&yn) || ABS(yt) > 2 || (x && xt!=1 && xt!=2)) R 0;
+ K z,r;
+ SW(ABS(yt)) //May want to consider casting y->t==1 to F's when x && x->t is F
+ {
+ CS(2,if(yn)accF=*kF(y);DO(y->n-1, accF=MAX(accF,kF(y)[i+1])) z=Kf(accF)) //CS order matters for empty list ()
+ CD: if(yn)accI=*kI(y);DO(y->n-1, accI=MAX(accI,kI(y)[i+1])) z=Ki(accI); //CS 1
+ }
+ U(z)
+ if(x){r=max_or(x,z); cd(z); z=r;}
+ R z;
+}
+
+K min_and_over(K x,K y)
+{
+ I accI=1; F accF=FI;
+ I yt=y->t, yn=y->n;
+ if((!yt&&yn) || ABS(yt) > 2 || (x && xt!=1 && xt!=2)) R 0;
+ K z,r;
+ SW(ABS(yt)) //May want to consider casting y->t==1 to F's when x && x->t is F
+ {
+ CS(2,if(yn)accF=*kF(y);DO(y->n-1, accF=MIN(accF,kF(y)[i+1])) z=Kf(accF)) //CS order matters for empty list ()
+ CD: if(yn)accI=*kI(y);DO(y->n-1, accI=MIN(accI,kI(y)[i+1])) z=Ki(accI); //CS 1
+ }
+ U(z)
+ if(x){r=min_and(x,z); cd(z); z=r;}
+ R z;
+}
+
TR DT[] = //Dispatch table is append-only. Reorder/delete/insert breaks backward compatibility with IO & inet
{
- {0, 0, 0,0}, //So no row index is confused with null pointer
- {0, 0, end,0}, // ; and such. convenience. (for ex(). not to be confused with last element of table)
- {0, 0, 0, 0},
- {1, 0, over,"/"},
- {1, 0, scan,"\\"},
- {1, 0, each,"'"},
- {2, 0, eachright,"/:"},
- {2, 0, eachleft,"\\:"},
- {2, 0, eachpair,"':"},
- {0, 0, 0,0},
- {0, 0, 0,0},
- {0, 0, 0,0},
- {0, 0, 0,0},
- {0, 0, 0,0},
- {0, 0, 0,0},
- {0, 0, 0,0},
- {0, 0, 0,0},
- {0, 1, flip,"+"},
- {0, 2, plus,"+"},
- {0, 1, negate,"-"},
- {0, 2, minus,"-"},
- {0, 1, first,"*"},
- {0, 2, times,"*"},
- {0, 1, reciprocal,"%%"},
- {0, 2, divide,"%%"},
- {0, 1, reverse,"|"},
- {0, 2, max_or,"|"},
- {0, 1, where,"&"},
- {0, 2, min_and,"&"},
- {0, 1, shape,"^"},
- {0, 2, power,"^"},
- {0, 1, enumerate,"!"},
- {0, 2, rotate_mod,"|"},
- {0, 1, grade_up,"<"},
- {0, 2, less,"<"},
- {0, 1, grade_down,">"},
- {0, 2, more,">"},
- {0, 1, group,"="},
- {0, 2, equals,"="},
- {0, 1, not_attribute,"~"},
- {0, 2, match,"~"},
- {0, 1, atom,"@"},
- {0, 2, at,"@"},
- {0, 1, range,"?"},
- {0, 2, what,"?"},
- {0, 1, floor_verb,"_"},
- {0, 2, drop_cut,"_"},
- {0, 1, enlist,","},
- {0, 2, join,","},
- {0, 1, count,"#"},
- {0, 2, take_reshape,"#"},
- {0, 1, format,"$"},
- {0, 2, dollar,"$"},
- {0, 1, dot_monadic,"."},
- {0, 2, dot,"."},
- {0, 1, colon_monadic,":"},
- {0, 2, colon_dyadic,":"},
- {0, 1, _0m,"0:"},
- {0, 2, _0d,"0:"},
- {0, 1, _1m,"1:"},
- {0, 2, _1d,"1:"},
- {0, 1, _2m,"2:"},
- {0, 2, _2d,"2:"},
- {0, 1, _3m,"3:"},
- {0, 2, _3d,"3:"},
- {0, 1, _4m,"4:"},
- {0, 2, _4d,"4:"},
- {0, 1, _5m,"5:"},
- {0, 2, _5d,"5:"},
- {0, 1, _6m,"6:"},
- {0, 2, _6d,"6:"}, //do not add 7+ here. go to bottom. keep paired as before
- {0, 1, _acos,"_acos"},
- {0, 1, _asin,"_asin"},
- {0, 1, _atan,"_atan"},
- {0, 1, _ceil,"_ceil"},
- {0, 1, _cos,"_cos"},
- {0, 1, _cosh,"_cosh"},
- {0, 1, _exp,"_exp"},
- {0, 1, _floor,"_floor"},
- {0, 1, _log,"_log"},
- {0, 1, _sin,"_sin"},
- {0, 1, _sinh,"_sinh"},
- {0, 1, _sqr,"_sqr"},
- {0, 1, _sqrt,"_sqrt"},
- {0, 1, _tan,"_tan"},
- {0, 1, _tanh,"_tanh"},
- {0, 1, _abs,"_abs"},
- {0, 1, _bd,"_bd"},
- {0, 1, _ceiling,"_ceiling"},
- {0, 1, _ci,"_ci"},
- {0, 1, _db,"_db"},
- {0, 1, _dj,"_dj"},
- {0, 1, _kona_exit,"_exit"},
- {0, 1, _getenv,"_getenv"},
- {0, 1, _gtime,"_gtime"},
- {0, 1, _host,"_host"},
- {0, 1, _ic,"_ic"},
- {0, 1, _inv,"_inv"},
- {0, 1, _jd,"_jd"},
- {0, 1, _lt,"_lt"},
- {0, 1, _ltime,"_ltime"},
- {0, 1, _size,"_size"},
- {0, 2, _bin,"_bin"},
- {0, 2, _binl,"_binl"},
- {0, 2, _di,"_di"},
- {0, 2, _dot,"_dot"},
- {0, 2, _draw,"_draw"},
- {0, 2, _dv,"_dv"},
- {0, 2, _dvl,"_dvl"},
- {0, 2, _in,"_in"},
- {0, 2, _lin,"_lin"},
- {0, 2, _lsq,"_lsq"},
- {0, 2, _mul,"_mul"},
- {0, 2, _setenv,"_setenv"},
- {0, 2, _sm,"_sm"},
- {0, 2, _ss,"_ss"},
- {0, 2, _sv,"_sv"},
- {0, 2, _vs,"_vs"},
- {0, 3, _ssr,"_ssr"},
+ {0, 0, 0,0,0}, //So no row index is confused with null pointer
+ {0, 0, end,0,0}, // ; and such. convenience. (for ex(). not to be confused with last element of table)
+ {0, 0, 0, 0,0},
+ {1, 0, over,"/",0},
+ {1, 0, scan,"\\",0},
+ {1, 0, each,"'",0},
+ {2, 0, eachright,"/:",0},
+ {2, 0, eachleft,"\\:",0},
+ {2, 0, eachpair,"':",0},
+ {0, 0, 0,0,0},
+ {0, 0, 0,0,0},
+ {0, 0, 0,0,0},
+ {0, 0, 0,0,0},
+ {0, 0, 0,0,0},
+ {0, 0, 0,0,0},
+ {0, 0, 0,0,0},
+ {0, 0, 0,0,0},
+ {0, 1, flip,"+",0},
+ {0, 2, plus,"+",{plus_over}},
+ {0, 1, negate,"-",0},
+ {0, 2, minus,"-",0},
+ {0, 1, first,"*",0},
+ {0, 2, times,"*",{times_over}},
+ {0, 1, reciprocal,"%%",0},
+ {0, 2, divide,"%%",0},
+ {0, 1, reverse,"|",0},
+ {0, 2, max_or,"|",{max_or_over}},
+ {0, 1, where,"&",0},
+ {0, 2, min_and,"&",{min_and_over}},
+ {0, 1, shape,"^",0},
+ {0, 2, power,"^",0},
+ {0, 1, enumerate,"!",0},
+ {0, 2, rotate_mod,"|",0},
+ {0, 1, grade_up,"<",0},
+ {0, 2, less,"<",0},
+ {0, 1, grade_down,">",0},
+ {0, 2, more,">",0},
+ {0, 1, group,"=",0},
+ {0, 2, equals,"=",0},
+ {0, 1, not_attribute,"~",0},
+ {0, 2, match,"~",0},
+ {0, 1, atom,"@",0},
+ {0, 2, at,"@",0},
+ {0, 1, range,"?",0},
+ {0, 2, what,"?",0},
+ {0, 1, floor_verb,"_",0},
+ {0, 2, drop_cut,"_",0},
+ {0, 1, enlist,",",0},
+ {0, 2, join,",",0},
+ {0, 1, count,"#",0},
+ {0, 2, take_reshape,"#",0},
+ {0, 1, format,"$",0},
+ {0, 2, dollar,"$",0},
+ {0, 1, dot_monadic,".",0},
+ {0, 2, dot,".",0},
+ {0, 1, colon_monadic,":",0},
+ {0, 2, colon_dyadic,":",0},
+ {0, 1, _0m,"0:",0},
+ {0, 2, _0d,"0:",0},
+ {0, 1, _1m,"1:",0},
+ {0, 2, _1d,"1:",0},
+ {0, 1, _2m,"2:",0},
+ {0, 2, _2d,"2:",0},
+ {0, 1, _3m,"3:",0},
+ {0, 2, _3d,"3:",0},
+ {0, 1, _4m,"4:",0},
+ {0, 2, _4d,"4:",0},
+ {0, 1, _5m,"5:",0},
+ {0, 2, _5d,"5:",0},
+ {0, 1, _6m,"6:",0},
+ {0, 2, _6d,"6:",0}, //do not add 7+ here. go to bottom. keep paired as before
+ {0, 1, _acos,"_acos",0},
+ {0, 1, _asin,"_asin",0},
+ {0, 1, _atan,"_atan",0},
+ {0, 1, _ceil,"_ceil",0},
+ {0, 1, _cos,"_cos",0},
+ {0, 1, _cosh,"_cosh",0},
+ {0, 1, _exp,"_exp",0},
+ {0, 1, _floor,"_floor",0},
+ {0, 1, _log,"_log",0},
+ {0, 1, _sin,"_sin",0},
+ {0, 1, _sinh,"_sinh",0},
+ {0, 1, _sqr,"_sqr",0},
+ {0, 1, _sqrt,"_sqrt",0},
+ {0, 1, _tan,"_tan",0},
+ {0, 1, _tanh,"_tanh",0},
+ {0, 1, _abs,"_abs",0},
+ {0, 1, _bd,"_bd",0},
+ {0, 1, _ceiling,"_ceiling",0},
+ {0, 1, _ci,"_ci",0},
+ {0, 1, _db,"_db",0},
+ {0, 1, _dj,"_dj",0},
+ {0, 1, _kona_exit,"_exit",0},
+ {0, 1, _getenv,"_getenv",0},
+ {0, 1, _gtime,"_gtime",0},
+ {0, 1, _host,"_host",0},
+ {0, 1, _ic,"_ic",0},
+ {0, 1, _inv,"_inv",0},
+ {0, 1, _jd,"_jd",0},
+ {0, 1, _lt,"_lt",0},
+ {0, 1, _ltime,"_ltime",0},
+ {0, 1, _size,"_size",0},
+ {0, 2, _bin,"_bin",0},
+ {0, 2, _binl,"_binl",0},
+ {0, 2, _di,"_di",0},
+ {0, 2, _dot,"_dot",0},
+ {0, 2, _draw,"_draw",0},
+ {0, 2, _dv,"_dv",0},
+ {0, 2, _dvl,"_dvl",0},
+ {0, 2, _in,"_in",0},
+ {0, 2, _lin,"_lin",0},
+ {0, 2, _lsq,"_lsq",0},
+ {0, 2, _mul,"_mul",0},
+ {0, 2, _setenv,"_setenv",0},
+ {0, 2, _sm,"_sm",0},
+ {0, 2, _ss,"_ss",0},
+ {0, 2, _sv,"_sv",0},
+ {0, 2, _vs,"_vs",0},
+ {0, 3, _ssr,"_ssr",0},
//^^Add new rows here^^
- {-1,-1,TABLE_END,0} //sentinel
+ {-1,-1,TABLE_END,0,0} //sentinel
};
K TABLE_END(){R 0;}
View
2 km.c
@@ -62,7 +62,7 @@ K cd(K a)
}
K ci(K a){if(a)a->c++; R a;}
-I bp(I t) {SW(ABS(t)){CSR(1, R sizeof(I)) CSR(2, R sizeof(F)) CSR(3, R sizeof(C)) default: R sizeof(V); } } //Default 0/+-4/5/6/7 (assumes sizeof(K)==sizeof(S)==...)
+I bp(I t) {SW(ABS(t)){CSR(1, R sizeof(I)) CSR(2, R sizeof(F)) CSR(3, R sizeof(C)) CD: R sizeof(V); } } //Default 0/+-4/5/6/7 (assumes sizeof(K)==sizeof(S)==...)
I sz(I t,I n){R 3*sizeof(I)+(7==t?TYPE_SEVEN_SIZE:n)*bp(t)+(3==ABS(t));} //not recursive. assert sz() > 0: Everything gets valid block for simplified munmap/(free)
Z I nearPG(I i){ I k=((size_t)i)&(PG-1);R k?i+PG-k:i;}//up 0,8,...,8,16,16,...
View
19 kx.c
@@ -19,22 +19,19 @@ Z V ex_(V a,I r);
//TODO: for derived verbs like +/ you can add the sub-pieces in parallel
Z K over2(K a, V *p, K b)
{
+
+ V *o=p-1; K(*f)(K,K);
+
+ K k=0;
+ if(VA(*o) && (f=DT[(I)*o].alt_funcs.verb_over))k=f(a,b); //k==0 just means not handled. Errors are not set to come from alt_funcs
+ P(k,k)
+
K u=0,v=0;
K y=a?v=join(u=enlist(a),b):b; //oom u (TODO: need to unroll to 'x f/y' and 'f/y' to optimize?)
I yt=y->t, yn=y->n;
K z=0,g=0;
if(yt > 0){z=ci(y); GC;}
- if(yn == 0)
- {
- V **q=(V**)p-1; I s=-2==y->t;
- if(VA(*q))
- if (DT[(I)*q].func==plus) z= s?Kf( 0):Ki(0);
- else if(DT[(I)*q].func==max_or) z= s?Kf(-FI):Ki(0);
- else if(DT[(I)*q].func==times) z= s?Kf( 1):Ki(1);
- else if(DT[(I)*q].func==min_and) z= s?Kf( FI):Ki(1);
- else z=LE;
- GC;
- }
+ if(yn == 0){if(VA(*o))z=LE; GC; } //Some verbs will handle this in alt_funcs
K c=first(y),d;//mm/o
//TODO: this reuse of g should be implemented in other adverbs
if(0 >yt) DO(yn-1, d=c; if(!g)g=newK(ABS(yt),1); memcpy(g->k,((V)y->k)+(i+1)*bp(yt),bp(yt)); c=dv_ex(d,p-1,g); if(2==g->c){cd(g);g=0;} cd(d); if(!c) GC;) //TODO: oom err/mmo unwind above - oom-g
View
3 ts.h
@@ -17,7 +17,8 @@ enum TYPE_SEVEN_MEMBERS {CONTEXT,DEPTH,CODE,LOCALS,PARAMS,CONJ,CACHE_WD,CACHE_TR
//Executable types: t-n is 7-n for n in {0,1,2,3,4,5,6,7}: 0: list of unexecuted types, 1: [derived] verb, 2: dynamically loaded function, 3: brace function{}, 4: ":[]", 5: if[], 6: while[], 7: do[]
typedef struct node{V k,v;I b;struct node *c[2];}Node;typedef Node*N;//Knuth's AVL tree
typedef struct pda{I i,s,n;S c;}Pda;typedef Pda*PDA; //holds parse state. pos in input, state, stacklength, stack
-typedef struct tr{ I adverbClass; I arity; V func; S text; } TR; //Table Row for Dispatch Table
+typedef struct af{ V verb_over; } AF; //Alternative/Adverb Functions
+typedef struct tr{ I adverbClass; I arity; V func; S text; AF alt_funcs; } TR; //Table Row for Dispatch Table
#define ke(x) (((K)x)->k)
#define kK(x) ke(x)
#define kI(x) ((I*)ke(x))

0 comments on commit 9322284

Please sign in to comment.