Last active
August 29, 2015 14:22
-
-
Save francescoagati/e7b7f153b37fa3f9122d to your computer and use it in GitHub Desktop.
replace ugly experiment
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
import haxe.macro.Context; | |
import haxe.macro.Expr; | |
using haxe.macro.ExprTools; | |
using haxe.macro.MacroStringTools; | |
using StringTools; | |
class Test { | |
static function replace(e:Expr,symbol:String) { | |
var fn = replace.bind(_,symbol); | |
return switch(e.expr) { | |
case EConst(CIdent(placeholder)): | |
if (placeholder == '_') { | |
{expr:EConst(CIdent(symbol)),pos:e.pos}; | |
} | |
else | |
e; | |
case _: ExprTools.map(e, fn); | |
} | |
} | |
macro public static function dp(expr:Expr) { | |
var new_expr = replace(expr,'3'); | |
trace(new_expr.toString()); | |
return macro { $new_expr; }; | |
} | |
public static function main() { | |
var sum = function(a:String,b:String) {return a+b;}; | |
var fn = dp({ | |
sum(_,'2'); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
result
Test.hx:17: TPath({ name => 3, pack => [], params => [] })
Test.hx:29: {
sum(3, '2');
}
Test.hx:38: characters 10-11 : Unknown identifier : 3