Created
September 22, 2013 05:39
-
-
Save MondayPM/6657031 to your computer and use it in GitHub Desktop.
Bind workaround for neko
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package macro; | |
| #if macro | |
| import haxe.macro.Type; | |
| import haxe.macro.Expr; | |
| import haxe.macro.Context; | |
| import tink.macro.tools.ExprTools; | |
| import tink.macro.tools.TypeTools; | |
| #end | |
| //Macros_statics | |
| class Macros_s { | |
| macro public static function bind(func:Expr,?args:Array<Expr>):Expr { | |
| var ret:ComplexType=null; | |
| var name:String=null; | |
| var r_args_s:String=""; | |
| var r_call:String=""; | |
| switch(func.expr) { | |
| case EConst(c): | |
| switch(c) { | |
| case CIdent(s): | |
| name=s; | |
| default: | |
| } | |
| default: | |
| } | |
| var t:Type=Context.typeof(func); | |
| switch(t) { | |
| case TFun(f_args,f_ret): | |
| ret=TypeTools.toComplex(f_ret,true); | |
| var args_c:Int=0; | |
| var r_call_first:Bool=true; | |
| var r_args:Array<ComplexType>=new Array<ComplexType>(); | |
| if(args!=null) { | |
| for(arg in args) { | |
| var arg_s:String=ExprTools.toString(arg); | |
| if(r_call_first) { | |
| r_call_first=false; | |
| } else { | |
| r_call+=","; | |
| } | |
| var f_arg:Dynamic=f_args.shift(); | |
| if(arg_s=="_") { | |
| r_call+="v"+r_args.length; | |
| r_args.push(TypeTools.toComplex(f_arg.t,true)); | |
| } else { | |
| r_call+=arg_s; | |
| } | |
| } | |
| } | |
| var first:Bool=true; | |
| var r_args_c:Int=0; | |
| for(r_arg in r_args) { | |
| if(first) { | |
| first=false; | |
| } else { | |
| r_args_s+=","; | |
| } | |
| r_args_s+="v"+r_args_c+":"+TypeTools.toString(r_arg); | |
| ++r_args_c; | |
| } | |
| for(f_arg in f_args) { | |
| if(first) { | |
| first=false; | |
| } else { | |
| r_args_s+=","; | |
| } | |
| if(f_arg.opt) { | |
| r_args_s+="?"; | |
| } | |
| r_args_s+="v"+r_args_c+":"+TypeTools.toString(TypeTools.toComplex(f_arg.t,true)); | |
| if(r_call_first) { | |
| r_call_first=false; | |
| } else { | |
| r_call+=","; | |
| } | |
| r_call+="v"+r_args_c; | |
| ++r_args_c; | |
| } | |
| default: | |
| } | |
| var r:String=null; | |
| r="{ | |
| var f=function("+r_args_s+"):"+TypeTools.toString(ret)+" { | |
| return "+name+"("+r_call+"); | |
| } | |
| f; | |
| }"; | |
| return Context.parse(r,Context.currentPos()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment