Skip to content

Instantly share code, notes, and snippets.

@profelis
Created May 30, 2013 19:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save profelis/5680556 to your computer and use it in GitHub Desktop.
Save profelis/5680556 to your computer and use it in GitHub Desktop.
while () {} else {} for () {} else {}
package ;
import haxe.macro.Expr;
/**
* ...
* @author deep <system.grand@gmail.com>
*/
class ElseMacros
{
/**
* ElseMacros.build( for (i in 0...10) trace(i), trace("empty loop") ); // no trace
*
* ElseMacros.build( for (i in 0...0) trace(i), trace("empty loop 2") ); // empty loop 2
*/
macro static public function build(eLoop:Expr, eElse:Expr):Expr {
var pre = macro var __flag__ = true;
var body = macro __flag__ = false;
var pos = macro if (__flag__) $eElse;
return switch (eLoop.expr) {
case EFor(it, expr):
macro { $pre;
for ($it) {
$body;
$expr;
}
$pos;
}
case EWhile(econd, expr, true):
macro { $pre;
while ($econd) {
$body;
$expr;
}
$pos;
}
case _: throw "unsupported loop";
}
return eLoop;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment