Created
July 22, 2015 11:10
-
-
Save restorer/7bb54f3b3bcc02eadecc to your computer and use it in GitHub Desktop.
C-style for-loops for haxe
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
// http://yal.cc/haxe-some-cleaner-c-style-for-loops/ | |
// macro | |
static macro function cfor(init, cond, post, expr) { | |
#if !display | |
var func = null; | |
func = function(expr:haxe.macro.Expr) { | |
return switch (expr.expr) { | |
case EContinue: macro { $post; $expr; } | |
case EWhile(_, _, _): expr; | |
case ECall(macro cfor, _): expr; | |
case EFor(_): expr; | |
case EIn(_): expr; | |
default: haxe.macro.ExprTools.map(expr, func); | |
} | |
} | |
expr = func(expr); | |
#end | |
return macro { | |
$init; | |
while ($cond) { | |
$expr; | |
$post; | |
} | |
}; | |
} | |
// usage: | |
cfor(var i = 0, i < 10, i++, { | |
if (i == 3) i++; | |
if (i == 5) continue; | |
trace(i); | |
}); | |
// will compile into | |
var i = 0; | |
while (i < 10) { | |
if (i == 3) i++; | |
if (i == 5) { | |
i++; | |
continue; | |
} | |
trace(i); | |
i++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment