-
-
Save YellowAfterlife/7494363 to your computer and use it in GitHub Desktop.
This file contains 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
-x Main |
This file contains 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.Expr; | |
import haxe.macro.Context; | |
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) { | |
var outer:Array<Expr> = [], inner:Array<Expr> = []; | |
// adds expression(s) into given list (block): | |
function unroll(o:Expr, to:Array<Expr>) { | |
switch (o.expr) { | |
case EBlock(m): for (v in m) to.push(v); | |
default: to.push(o); | |
} | |
} | |
// inner (inside the for-loop) block: | |
inner.push(macro var _ = true); | |
inner.push({ | |
expr: ExprDef.EWhile(macro _ = false, block, false), | |
pos: block.pos | |
}); | |
inner.push(macro if (_) break); | |
unroll(incr, inner); | |
// outer (outside the for-loop) block: | |
unroll(init, outer); | |
outer.push({ | |
expr: ExprDef.EWhile(cond, { | |
expr: ExprDef.EBlock(inner), | |
pos: block.pos | |
}, true), | |
pos: block.pos | |
}); | |
// | |
return { | |
expr: ExprDef.EBlock(outer), | |
pos: init.pos | |
}; | |
} | |
} |
This file contains 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
@:build(FTW.build()) | |
class Main { | |
static function main() { | |
@for( { var i = 0, j = -5; } , i < 5 && j < 0, { i++; j++; } ) { | |
trace('Step $i, $j'); | |
} | |
} | |
} |
None really, its just Nicolas don't liking C-fors ( there is support for while but not for ) which is a shame because Haxe borrows a lot of AS3 syntax except one of the most used AS3 idiom constructs making porting AS3 games more of a hassle that it could be.
One question in the line: macro @for($init , $cond , $incr) $block:
is there some way to tell Haxe to accept: @for($init EConst(';') $cond EConst(';') $incr) $block:
The best you can do to approach the semicolons is around
@for({ var i = 0; i < 5; i++; }) trace(i);
... which is still pretty strange.
The general conclusion here is, that whatever you do about for-loops, they will never feel right.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there actually a compelling argument/discussion around the lack of c-style for loops in Haxe?