Skip to content

Instantly share code, notes, and snippets.

@restorer
Created July 22, 2015 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save restorer/7bb54f3b3bcc02eadecc to your computer and use it in GitHub Desktop.
Save restorer/7bb54f3b3bcc02eadecc to your computer and use it in GitHub Desktop.
C-style for-loops for haxe
// 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