Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Last active August 29, 2015 14:22
Show Gist options
  • Save francescoagati/e7b7f153b37fa3f9122d to your computer and use it in GitHub Desktop.
Save francescoagati/e7b7f153b37fa3f9122d to your computer and use it in GitHub Desktop.
replace ugly experiment
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');
});
}
}
@francescoagati
Copy link
Author

result

Test.hx:17: TPath({ name => 3, pack => [], params => [] })
Test.hx:29: {
sum(3, '2');
}
Test.hx:38: characters 10-11 : Unknown identifier : 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment