Skip to content

Instantly share code, notes, and snippets.

@ex
Forked from dpeek/FTW.hx
Created March 10, 2014 16:00
Show Gist options
  • Save ex/9467836 to your computer and use it in GitHub Desktop.
Save ex/9467836 to your computer and use it in GitHub Desktop.
import haxe.macro.Expr;
class FTW
{
public static function build()
{
return haxe.macro.Context.getBuildFields().map(transformField);
}
static function transformField(field:Field)
{
switch (field.kind)
{
case FFun(f): transformExpr(f.expr);
default:
}
return field;
}
static function transformExpr(expr:Expr) switch (expr)
{
case macro @for($init, $cond, $incr) $block:
transformExpr(block);
expr.expr = makeLoop(init, cond, incr, block).expr;
default:
haxe.macro.ExprTools.iter(expr, transformExpr);
}
static function makeLoop(init:Expr, cond:Expr, incr:Expr, block:Expr) return macro
{
$init;
while ($cond)
{
$block;
$incr;
}
}
}
@:build(FTW.build())
class Main
{
static function main()
{
@for(var i = 0, i < 10, i++)
{
trace('ftw: $i');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment