Created
May 30, 2013 19:44
-
-
Save profelis/5680556 to your computer and use it in GitHub Desktop.
while () {} else {}
for () {} else {}
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
package ; | |
import haxe.macro.Expr; | |
/** | |
* ... | |
* @author deep <[email protected]> | |
*/ | |
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