Created
June 7, 2015 21:58
-
-
Save francescoagati/54257f21e9d945c7005a to your computer and use it in GitHub Desktop.
Let.hx
This file contains hidden or 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; | |
using haxe.macro.ExprTools; | |
using haxe.macro.MacroStringTools; | |
class Let { | |
static function processDestructuring(e:Expr) { | |
inline function processArrays(lefts:Array<Expr>,rights) { | |
var cnt = 0; | |
var blocks = [ for (left in lefts) { | |
var valueLeft = left.toString(); | |
var right = rights[cnt]; | |
cnt++; | |
macro var $valueLeft = $e{right}; | |
}]; | |
return macro $b{blocks}; | |
} | |
inline function processObject(lefts:Array<Expr>,right) { | |
var cnt = 0; | |
var blocks = [ for (left in lefts) { | |
var valueLeft = left.toString(); | |
cnt++; | |
macro var $valueLeft = $right.$valueLeft; | |
}]; | |
return macro $b{blocks}; | |
} | |
return switch e { | |
case macro [$a{lefts}] = [$a{rights}]: processArrays(lefts,rights); | |
case macro [$a{lefts}] << ${right}: processObject(lefts,right); | |
case _:e.map(processDestructuring); | |
} | |
} | |
public static macro function let(e:Expr) { | |
return e.map(processDestructuring); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment