Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 9322284ac29c56fdc6c0e2f1112ea2bb12105438 1 parent 06f5527
Kevin Lawler authored September 07, 2011
304  k.c
@@ -271,129 +271,193 @@ K show(K a)
271 271
   R a;
272 272
 }
273 273
 
  274
+K plus_over(K x,K y)
  275
+{
  276
+  I accI=0; F accF=0;
  277
+  I yt=y->t,yn=y->n;
  278
+  if((!yt&&yn) || ABS(yt) > 2 || (x && xt!=1 && xt!=2)) R 0;
  279
+  K z,r;
  280
+  SW(ABS(yt)) //May want to consider casting y->t==1 to F's when x && x->t is F
  281
+  {
  282
+    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 ()
  283
+    CD:  if(yn)accI=*kI(y);DO(y->n-1, accI=accI+kI(y)[i+1]) z=Ki(accI); //CS 1
  284
+  }
  285
+  U(z)
  286
+  if(x){r=plus(x,z); cd(z); z=r;}
  287
+  R z;
  288
+}
  289
+
  290
+K times_over(K x,K y)
  291
+{
  292
+  I accI=1; F accF=1;
  293
+  I yt=y->t,yn=y->n;
  294
+  if((!yt&&yn) || ABS(yt) > 2 || (x && xt!=1 && xt!=2)) R 0;
  295
+  K z,r;
  296
+  SW(ABS(yt)) //May want to consider casting y->t==1 to F's when x && x->t is F
  297
+  {
  298
+    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 () 
  299
+    CD:  if(yn)accI=*kI(y);DO(y->n-1, accI=accI*kI(y)[i+1]) z=Ki(accI); //CS 1
  300
+  }
  301
+  U(z)
  302
+  if(x){r=times(x,z); cd(z); z=r;}
  303
+  R z;
  304
+}
  305
+
  306
+K max_or_over(K x,K y)
  307
+{
  308
+  I accI=0; F accF=-FI;
  309
+  I yt=y->t, yn=y->n;
  310
+  if((!yt&&yn) || ABS(yt) > 2 || (x && xt!=1 && xt!=2)) R 0;
  311
+  K z,r;
  312
+  SW(ABS(yt)) //May want to consider casting y->t==1 to F's when x && x->t is F
  313
+  {
  314
+    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 () 
  315
+    CD:  if(yn)accI=*kI(y);DO(y->n-1, accI=MAX(accI,kI(y)[i+1])) z=Ki(accI); //CS 1
  316
+  }
  317
+  U(z)
  318
+  if(x){r=max_or(x,z); cd(z); z=r;}
  319
+  R z;
  320
+}
  321
+
  322
+K min_and_over(K x,K y)
  323
+{
  324
+  I accI=1; F accF=FI;
  325
+  I yt=y->t, yn=y->n;
  326
+  if((!yt&&yn) || ABS(yt) > 2 || (x && xt!=1 && xt!=2)) R 0;
  327
+  K z,r;
  328
+  SW(ABS(yt)) //May want to consider casting y->t==1 to F's when x && x->t is F
  329
+  {
  330
+    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 () 
  331
+    CD:  if(yn)accI=*kI(y);DO(y->n-1, accI=MIN(accI,kI(y)[i+1])) z=Ki(accI); //CS 1
  332
+  }
  333
+  U(z)
  334
+  if(x){r=min_and(x,z); cd(z); z=r;}
  335
+  R z;
  336
+}
  337
+
274 338
 TR DT[] =  //Dispatch table is append-only. Reorder/delete/insert breaks backward compatibility with IO & inet
275 339
 {
276  
-  {0, 0, 0,0}, //So no row index is confused with null pointer
277  
-  {0, 0, end,0}, // ; and such. convenience. (for ex(). not to be confused with last element of table)
278  
-  {0, 0, 0, 0},
279  
-  {1, 0, over,"/"},
280  
-  {1, 0, scan,"\\"},
281  
-  {1, 0, each,"'"},
282  
-  {2, 0, eachright,"/:"},
283  
-  {2, 0, eachleft,"\\:"},
284  
-  {2, 0, eachpair,"':"},
285  
-  {0, 0, 0,0},
286  
-  {0, 0, 0,0},
287  
-  {0, 0, 0,0},
288  
-  {0, 0, 0,0},
289  
-  {0, 0, 0,0},
290  
-  {0, 0, 0,0},
291  
-  {0, 0, 0,0},
292  
-  {0, 0, 0,0},
293  
-  {0, 1, flip,"+"},
294  
-  {0, 2, plus,"+"},
295  
-  {0, 1, negate,"-"},
296  
-  {0, 2, minus,"-"},
297  
-  {0, 1, first,"*"},
298  
-  {0, 2, times,"*"},
299  
-  {0, 1, reciprocal,"%%"},
300  
-  {0, 2, divide,"%%"},
301  
-  {0, 1, reverse,"|"},
302  
-  {0, 2, max_or,"|"},
303  
-  {0, 1, where,"&"},
304  
-  {0, 2, min_and,"&"},
305  
-  {0, 1, shape,"^"},
306  
-  {0, 2, power,"^"},
307  
-  {0, 1, enumerate,"!"},
308  
-  {0, 2, rotate_mod,"|"},
309  
-  {0, 1, grade_up,"<"},
310  
-  {0, 2, less,"<"},
311  
-  {0, 1, grade_down,">"},
312  
-  {0, 2, more,">"},
313  
-  {0, 1, group,"="},
314  
-  {0, 2, equals,"="},
315  
-  {0, 1, not_attribute,"~"},
316  
-  {0, 2, match,"~"},
317  
-  {0, 1, atom,"@"},
318  
-  {0, 2, at,"@"},
319  
-  {0, 1, range,"?"},
320  
-  {0, 2, what,"?"},
321  
-  {0, 1, floor_verb,"_"},
322  
-  {0, 2, drop_cut,"_"},
323  
-  {0, 1, enlist,","},
324  
-  {0, 2, join,","},
325  
-  {0, 1, count,"#"},
326  
-  {0, 2, take_reshape,"#"},
327  
-  {0, 1, format,"$"},
328  
-  {0, 2, dollar,"$"},
329  
-  {0, 1, dot_monadic,"."},
330  
-  {0, 2, dot,"."},
331  
-  {0, 1, colon_monadic,":"},
332  
-  {0, 2, colon_dyadic,":"},
333  
-  {0, 1, _0m,"0:"}, 
334  
-  {0, 2, _0d,"0:"}, 
335  
-  {0, 1, _1m,"1:"}, 
336  
-  {0, 2, _1d,"1:"}, 
337  
-  {0, 1, _2m,"2:"}, 
338  
-  {0, 2, _2d,"2:"}, 
339  
-  {0, 1, _3m,"3:"}, 
340  
-  {0, 2, _3d,"3:"}, 
341  
-  {0, 1, _4m,"4:"}, 
342  
-  {0, 2, _4d,"4:"}, 
343  
-  {0, 1, _5m,"5:"}, 
344  
-  {0, 2, _5d,"5:"}, 
345  
-  {0, 1, _6m,"6:"}, 
346  
-  {0, 2, _6d,"6:"},  //do not add 7+ here. go to bottom. keep paired as before
347  
-  {0, 1, _acos,"_acos"},
348  
-  {0, 1, _asin,"_asin"},
349  
-  {0, 1, _atan,"_atan"},
350  
-  {0, 1, _ceil,"_ceil"},
351  
-  {0, 1, _cos,"_cos"},
352  
-  {0, 1, _cosh,"_cosh"},
353  
-  {0, 1, _exp,"_exp"},
354  
-  {0, 1, _floor,"_floor"},
355  
-  {0, 1, _log,"_log"},
356  
-  {0, 1, _sin,"_sin"},
357  
-  {0, 1, _sinh,"_sinh"},
358  
-  {0, 1, _sqr,"_sqr"},
359  
-  {0, 1, _sqrt,"_sqrt"},
360  
-  {0, 1, _tan,"_tan"},
361  
-  {0, 1, _tanh,"_tanh"},
362  
-  {0, 1, _abs,"_abs"},
363  
-  {0, 1, _bd,"_bd"},
364  
-  {0, 1, _ceiling,"_ceiling"},
365  
-  {0, 1, _ci,"_ci"},
366  
-  {0, 1, _db,"_db"},
367  
-  {0, 1, _dj,"_dj"},
368  
-  {0, 1, _kona_exit,"_exit"},
369  
-  {0, 1, _getenv,"_getenv"},
370  
-  {0, 1, _gtime,"_gtime"},
371  
-  {0, 1, _host,"_host"},
372  
-  {0, 1, _ic,"_ic"},
373  
-  {0, 1, _inv,"_inv"},
374  
-  {0, 1, _jd,"_jd"},
375  
-  {0, 1, _lt,"_lt"},
376  
-  {0, 1, _ltime,"_ltime"},
377  
-  {0, 1, _size,"_size"},
378  
-  {0, 2, _bin,"_bin"},
379  
-  {0, 2, _binl,"_binl"},
380  
-  {0, 2, _di,"_di"},
381  
-  {0, 2, _dot,"_dot"},
382  
-  {0, 2, _draw,"_draw"},
383  
-  {0, 2, _dv,"_dv"},
384  
-  {0, 2, _dvl,"_dvl"},
385  
-  {0, 2, _in,"_in"},
386  
-  {0, 2, _lin,"_lin"},
387  
-  {0, 2, _lsq,"_lsq"},
388  
-  {0, 2, _mul,"_mul"},
389  
-  {0, 2, _setenv,"_setenv"},
390  
-  {0, 2, _sm,"_sm"},
391  
-  {0, 2, _ss,"_ss"},
392  
-  {0, 2, _sv,"_sv"},
393  
-  {0, 2, _vs,"_vs"},
394  
-  {0, 3, _ssr,"_ssr"},
  340
+  {0, 0, 0,0,0}, //So no row index is confused with null pointer
  341
+  {0, 0, end,0,0}, // ; and such. convenience. (for ex(). not to be confused with last element of table)
  342
+  {0, 0, 0, 0,0},
  343
+  {1, 0, over,"/",0},
  344
+  {1, 0, scan,"\\",0},
  345
+  {1, 0, each,"'",0},
  346
+  {2, 0, eachright,"/:",0},
  347
+  {2, 0, eachleft,"\\:",0},
  348
+  {2, 0, eachpair,"':",0},
  349
+  {0, 0, 0,0,0},
  350
+  {0, 0, 0,0,0},
  351
+  {0, 0, 0,0,0},
  352
+  {0, 0, 0,0,0},
  353
+  {0, 0, 0,0,0},
  354
+  {0, 0, 0,0,0},
  355
+  {0, 0, 0,0,0},
  356
+  {0, 0, 0,0,0},
  357
+  {0, 1, flip,"+",0},
  358
+  {0, 2, plus,"+",{plus_over}},
  359
+  {0, 1, negate,"-",0},
  360
+  {0, 2, minus,"-",0},
  361
+  {0, 1, first,"*",0},
  362
+  {0, 2, times,"*",{times_over}},
  363
+  {0, 1, reciprocal,"%%",0},
  364
+  {0, 2, divide,"%%",0},
  365
+  {0, 1, reverse,"|",0},
  366
+  {0, 2, max_or,"|",{max_or_over}},
  367
+  {0, 1, where,"&",0},
  368
+  {0, 2, min_and,"&",{min_and_over}},
  369
+  {0, 1, shape,"^",0},
  370
+  {0, 2, power,"^",0},
  371
+  {0, 1, enumerate,"!",0},
  372
+  {0, 2, rotate_mod,"|",0},
  373
+  {0, 1, grade_up,"<",0},
  374
+  {0, 2, less,"<",0},
  375
+  {0, 1, grade_down,">",0},
  376
+  {0, 2, more,">",0},
  377
+  {0, 1, group,"=",0},
  378
+  {0, 2, equals,"=",0},
  379
+  {0, 1, not_attribute,"~",0},
  380
+  {0, 2, match,"~",0},
  381
+  {0, 1, atom,"@",0},
  382
+  {0, 2, at,"@",0},
  383
+  {0, 1, range,"?",0},
  384
+  {0, 2, what,"?",0},
  385
+  {0, 1, floor_verb,"_",0},
  386
+  {0, 2, drop_cut,"_",0},
  387
+  {0, 1, enlist,",",0},
  388
+  {0, 2, join,",",0},
  389
+  {0, 1, count,"#",0},
  390
+  {0, 2, take_reshape,"#",0},
  391
+  {0, 1, format,"$",0},
  392
+  {0, 2, dollar,"$",0},
  393
+  {0, 1, dot_monadic,".",0},
  394
+  {0, 2, dot,".",0},
  395
+  {0, 1, colon_monadic,":",0},
  396
+  {0, 2, colon_dyadic,":",0},
  397
+  {0, 1, _0m,"0:",0}, 
  398
+  {0, 2, _0d,"0:",0}, 
  399
+  {0, 1, _1m,"1:",0}, 
  400
+  {0, 2, _1d,"1:",0}, 
  401
+  {0, 1, _2m,"2:",0}, 
  402
+  {0, 2, _2d,"2:",0}, 
  403
+  {0, 1, _3m,"3:",0}, 
  404
+  {0, 2, _3d,"3:",0}, 
  405
+  {0, 1, _4m,"4:",0}, 
  406
+  {0, 2, _4d,"4:",0}, 
  407
+  {0, 1, _5m,"5:",0}, 
  408
+  {0, 2, _5d,"5:",0}, 
  409
+  {0, 1, _6m,"6:",0}, 
  410
+  {0, 2, _6d,"6:",0},  //do not add 7+ here. go to bottom. keep paired as before
  411
+  {0, 1, _acos,"_acos",0},
  412
+  {0, 1, _asin,"_asin",0},
  413
+  {0, 1, _atan,"_atan",0},
  414
+  {0, 1, _ceil,"_ceil",0},
  415
+  {0, 1, _cos,"_cos",0},
  416
+  {0, 1, _cosh,"_cosh",0},
  417
+  {0, 1, _exp,"_exp",0},
  418
+  {0, 1, _floor,"_floor",0},
  419
+  {0, 1, _log,"_log",0},
  420
+  {0, 1, _sin,"_sin",0},
  421
+  {0, 1, _sinh,"_sinh",0},
  422
+  {0, 1, _sqr,"_sqr",0},
  423
+  {0, 1, _sqrt,"_sqrt",0},
  424
+  {0, 1, _tan,"_tan",0},
  425
+  {0, 1, _tanh,"_tanh",0},
  426
+  {0, 1, _abs,"_abs",0},
  427
+  {0, 1, _bd,"_bd",0},
  428
+  {0, 1, _ceiling,"_ceiling",0},
  429
+  {0, 1, _ci,"_ci",0},
  430
+  {0, 1, _db,"_db",0},
  431
+  {0, 1, _dj,"_dj",0},
  432
+  {0, 1, _kona_exit,"_exit",0},
  433
+  {0, 1, _getenv,"_getenv",0},
  434
+  {0, 1, _gtime,"_gtime",0},
  435
+  {0, 1, _host,"_host",0},
  436
+  {0, 1, _ic,"_ic",0},
  437
+  {0, 1, _inv,"_inv",0},
  438
+  {0, 1, _jd,"_jd",0},
  439
+  {0, 1, _lt,"_lt",0},
  440
+  {0, 1, _ltime,"_ltime",0},
  441
+  {0, 1, _size,"_size",0},
  442
+  {0, 2, _bin,"_bin",0},
  443
+  {0, 2, _binl,"_binl",0},
  444
+  {0, 2, _di,"_di",0},
  445
+  {0, 2, _dot,"_dot",0},
  446
+  {0, 2, _draw,"_draw",0},
  447
+  {0, 2, _dv,"_dv",0},
  448
+  {0, 2, _dvl,"_dvl",0},
  449
+  {0, 2, _in,"_in",0},
  450
+  {0, 2, _lin,"_lin",0},
  451
+  {0, 2, _lsq,"_lsq",0},
  452
+  {0, 2, _mul,"_mul",0},
  453
+  {0, 2, _setenv,"_setenv",0},
  454
+  {0, 2, _sm,"_sm",0},
  455
+  {0, 2, _ss,"_ss",0},
  456
+  {0, 2, _sv,"_sv",0},
  457
+  {0, 2, _vs,"_vs",0},
  458
+  {0, 3, _ssr,"_ssr",0},
395 459
   //^^Add new rows here^^
396  
-  {-1,-1,TABLE_END,0} //sentinel
  460
+  {-1,-1,TABLE_END,0,0} //sentinel
397 461
 };
398 462
 
399 463
 K TABLE_END(){R 0;}
2  km.c
@@ -62,7 +62,7 @@ K cd(K a)
62 62
 }
63 63
 K ci(K a){if(a)a->c++; R a;}
64 64
 
65  
-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)==...)
  65
+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)==...)
66 66
 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)
67 67
 
68 68
 Z I nearPG(I i){ I k=((size_t)i)&(PG-1);R k?i+PG-k:i;}//up 0,8,...,8,16,16,...
19  kx.c
@@ -19,22 +19,19 @@ Z V ex_(V a,I r);
19 19
 //TODO: for derived verbs like +/ you can add the sub-pieces in parallel
20 20
 Z K over2(K a, V *p, K b)
21 21
 {
  22
+
  23
+  V *o=p-1; K(*f)(K,K); 
  24
+
  25
+  K k=0;
  26
+  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
  27
+  P(k,k)
  28
+
22 29
   K u=0,v=0;
23 30
   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?)
24 31
   I yt=y->t, yn=y->n;
25 32
   K z=0,g=0;
26 33
   if(yt  > 0){z=ci(y); GC;}
27  
-  if(yn == 0)
28  
-  {
29  
-    V **q=(V**)p-1; I s=-2==y->t;
30  
-    if(VA(*q))
31  
-      if     (DT[(I)*q].func==plus)    z= s?Kf(  0):Ki(0);
32  
-      else if(DT[(I)*q].func==max_or)  z= s?Kf(-FI):Ki(0);
33  
-      else if(DT[(I)*q].func==times)   z= s?Kf(  1):Ki(1);
34  
-      else if(DT[(I)*q].func==min_and) z= s?Kf( FI):Ki(1);
35  
-      else  z=LE;
36  
-      GC;
37  
-  }
  34
+  if(yn == 0){if(VA(*o))z=LE; GC; } //Some verbs will handle this in alt_funcs
38 35
   K c=first(y),d;//mm/o
39 36
   //TODO: this reuse of g should be implemented in other adverbs
40 37
   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
3  ts.h
@@ -17,7 +17,8 @@ enum TYPE_SEVEN_MEMBERS {CONTEXT,DEPTH,CODE,LOCALS,PARAMS,CONJ,CACHE_WD,CACHE_TR
17 17
 //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[]
18 18
 typedef struct node{V k,v;I b;struct node *c[2];}Node;typedef Node*N;//Knuth's AVL tree
19 19
 typedef struct pda{I i,s,n;S c;}Pda;typedef Pda*PDA; //holds parse state. pos in input, state, stacklength, stack
20  
-typedef struct tr{ I adverbClass; I arity; V func; S text; } TR; //Table Row for Dispatch Table
  20
+typedef struct af{ V verb_over; } AF; //Alternative/Adverb Functions
  21
+typedef struct tr{ I adverbClass; I arity; V func; S text; AF alt_funcs; } TR; //Table Row for Dispatch Table
21 22
 #define ke(x) (((K)x)->k)
22 23
 #define kK(x) ke(x)
23 24
 #define kI(x) ((I*)ke(x))

0 notes on commit 9322284

Please sign in to comment.
Something went wrong with that request. Please try again.