Changeset 389
- Timestamp:
- 07/21/08 09:00:32 (4 months ago)
- Files:
-
- trunk/tools/tools/base.d (modified) (7 diffs)
- trunk/tools/tools/functional.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/tools/tools/base.d
r385 r389 462 462 LHS lhs; 463 463 typeof(function() { LHS lhs; RHS rhs; "~CODE~" }()) "~OP~"(RHS)(RHS rhs) { "~CODE~" } 464 typeof(function() { LHS lhs; RHS rhs; "~CODE~" }()) opCall(RHS)(RHS rhs) { "~CODE~" }464 alias "~OP~" opCall; 465 465 }" 466 466 )~" … … 469 469 ?" 470 470 typeof(function() { Unstatic!(LHS) lhs; "~CODE~" }()) "~OP~"_r(LHS)(LHS lhs) { "~CODE~" } 471 typeof(function() { Unstatic!(LHS) lhs; "~CODE~" }()) opCall (LHS)(LHS lhs) { "~CODE~" }471 alias "~OP~"_r opCall; 472 472 " 473 473 :" 474 474 __"~NAME~"!(Unstatic!(LHS)) "~OP~"_r(LHS)(LHS lhs) { __"~NAME~"!(Unstatic!(LHS)) res=void; res.lhs = lhs; return res; } 475 __"~NAME~"!(Unstatic!(LHS)) opCall(LHS)(LHS lhs) { __"~NAME~"!(Unstatic!(LHS)) res=void; res.lhs = lhs; return res; }475 alias "~OP~"_r opCall; 476 476 " 477 477 )~"} … … 588 588 template Length(T, U) { const int Length=CallConfig!(T, U).length; } 589 589 590 struct fix_late_holder(LHS, RHS) { 591 LHS lhs; RHS rhs; 592 typeof(lhs(rhs, TupleInit!(X))) opCall(X...)(X par) { 593 return lhs(rhs, par); 594 } 595 } 596 590 597 mixin(Operator!("fix", " 591 598 // pragma(msg, \"fix: \"~LHS.stringof~\" -> \"~RHS.stringof); 592 599 struct holder { 593 600 LHS fix_lhs; Unstatic!(RHS) fix_rhs; 594 static if(!is(Params!(LHS))) 595 static assert(false, \"fix: \"~LHS.stringof~\" is not callable!\"); 596 else mixin(\"Ret!(LHS) call(\"~refToParamList(\"Params!(LHS)[Length!(RHS, LHS)..$]\", isRef!(LHS)[Length!(RHS, LHS)..$])~\") { 601 mixin(\"Ret!(LHS) call(\"~refToParamList(\"Params!(LHS)[Length!(RHS, LHS)..$]\", isRef!(LHS)[Length!(RHS, LHS)..$])~\") { 597 602 return mixin(\\\"fix_lhs(\\\"~Value!(RHS, Ret!(LHS) function(Params!(LHS)[0..Length!(RHS, LHS)]), \\\"fix_rhs\\\")~\\\" 598 603 \"~((Length!(RHS, LHS) && (Params!(LHS).length-Length!(RHS, LHS)))?\", \":\"\")~refToValueList(isRef!(LHS)[Length!(RHS, LHS)..$])~\")\\\"); 599 604 }\"); 600 605 } 601 auto res=new holder; res.fix_lhs=lhs; res.fix_rhs=rhs; 602 return &res.call; 606 static if(!is(Params!(LHS))) { 607 // static assert(false, \"fix: \"~LHS.stringof~\" is not callable!\"); 608 fix_late_holder!(LHS, RHS) res = void; 609 res.lhs = lhs; res.rhs = rhs; return res; 610 } else { 611 auto res=new holder; res.fix_lhs=lhs; res.fix_rhs=rhs; 612 return &res.call; 613 } 603 614 ")); 604 615 … … 626 637 _partial!(C) partial(C)(C callable) { _partial!(C) res; res.callable=callable; return res; } 627 638 628 mixin(Operator!("chain", " 629 struct holder { 630 LHS _lhs; RHS _rhs; 631 Ret!(RHS) call(Params!(LHS) left, Params!(RHS)[Params!(LHS).length..$] right) { 632 return _rhs(_lhs(left), right); 633 } 634 } 635 auto res=new holder; res._lhs=lhs; res._rhs=rhs; 636 return &res.call; 637 ")); 639 struct chain_holder(LHS, RHS) { 640 LHS _lhs; RHS _rhs; 641 Ret!(typeof(function(X foo) { return _rhs(_lhs(foo)); })) opCall(X...)(X par) { 642 return _rhs(_lhs(par)); 643 } 644 } 645 mixin(Operator!("chain", " chain_holder!(LHS, RHS) res = void; res._lhs = lhs; res._rhs = rhs; return res; ")); 638 646 639 647 template Reverse(T...) { … … 646 654 static if(LEN==1) const string ReverseIndices=BASE~"[0]"; else 647 655 const string ReverseIndices=BASE~"["~(LEN-1).stringof~"], "~ReverseIndices!(BASE, LEN-1); 656 } 657 658 struct reverse_holder(LHS) { 659 LHS lhs; 660 typeof(lhs(TupleInit!(Reverse!(X)))) opCall(X...)(X par) { 661 return mixin("lhs("~ReverseIndices!("par", X.length)~")"); 662 } 648 663 } 649 664 … … 655 670 } else { 656 671 // reverse parameter order 657 struct reverted { 658 LHS _lhs; 659 Ret!(LHS) call(Reverse!(Params!(LHS)) rev) { 660 const string value=\"_lhs(\" ~ ReverseIndices!(\"rev\", Params!(LHS).length) ~ \") \"; 661 mixin(\"return \"~value~\";\"); 672 static if (is(Params!(LHS))) { 673 static typeof(mixin(\"lhs(\"~ReverseIndices!(\"TupleInit!(Reverse!(Params!(LHS)))\", Params!(LHS).length)~\")\")) 674 res_fn(typeof(lhs) lhs, Reverse!(Params!(LHS)) rev) { 675 return mixin(\"lhs(\"~ReverseIndices!(\"rev\", Params!(LHS).length)~\")\"); 662 676 } 663 } 664 auto r=new reverted; r._lhs=lhs; 665 return &r.call; 677 return lhs /apply/ &res_fn; 678 } else { 679 reverse_holder!(LHS) res = void; 680 res.lhs = lhs; 681 return res; 682 } 666 683 }", true 667 684 )); … … 945 962 T delegate(T) Loop(T)(int count, T delegate(T) dg) { 946 963 assert(count>0); 947 if (count==1) return dg; else return Loop(count-1, dg) /chain/ dg; 964 if (count==1) return dg; else { 965 auto foo = Loop(count-1, dg) /chain/ dg; 966 return foo /apply/ (typeof(foo) foo, T t) { return foo(t); }; 967 } 948 968 } 949 969 trunk/tools/tools/functional.d
r317 r389 100 100 101 101 mixin(Operator!("sort", "auto res=lhs.dup; res.sort; return res;", true)); 102 103 struct curry_holder(LHS, G...) { 104 LHS lhs; G g; 105 const string CODE = " 106 static if (is(typeof(lhs(g, par)))) return lhs(g, par); 107 else { 108 curry_holder!(LHS, G, X) res = void; 109 res.lhs = lhs; 110 foreach (id, entry; g) res.g[id] = entry; 111 foreach (id, entry; par) res.g[G.length + id] = entry; 112 return res; 113 }"; 114 Ret!(typeof(function { X par; mixin(CODE); })) opCall(X...)(X par) { 115 mixin(CODE); 116 } 117 } 118 102 119 mixin(Operator!("curry", " 103 static if(Params!(LHS).length!>1) return lhs; 104 else return (LHS _lhs, Params!(LHS)[0] first) { return _lhs /fix/ first /curry; } /fix/ lhs; 120 static if (is(Params!(LHS))) { 121 static if (Params!(LHS).length!>1) return lhs; 122 else return (LHS _lhs, Params!(LHS)[0] first) { return _lhs /fix/ first /curry; } /fix/ lhs; 123 } else { 124 curry_holder!(LHS) res = void; 125 res.lhs = lhs; return res; 126 } 105 127 ", true)); 128 129 template CurryRes(S, T...) { 130 static if (T.length == 1) alias typeof(Init!(S) (Init!(T[0]))) CurryRes; 131 else alias typeof((Init!(S) (Init!(T[0])) /uncurry) (Init!(T[1 .. $]))) CurryRes; 132 } 133 106 134 template CollectParameters(T) { 107 135 static if (is(T==function) || is (T==delegate)) { … … 113 141 } 114 142 } 143 115 144 template CurryCalls(int depth) { 116 145 static if (!depth) const string CurryCalls=""; 117 146 else const string CurryCalls=CurryCalls!(depth-1)~"(cp["~depth.stringof~"-1])"; 118 147 } 148 119 149 mixin(Operator!("uncurry", " 120 150 alias CollectParameters!(LHS).tuple CPAR;
