Created
December 29, 2014 21:40
-
-
Save utaal/de0d9905e92a7badc4df to your computer and use it in GitHub Desktop.
sweet.js if expression macro
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
macro iff { | |
rule { | |
($x:expr) { | |
$truebody | |
... | |
$truelast:expr | |
} els { | |
$falsebody | |
... | |
$falselast:expr | |
} | |
} => { | |
(function() { | |
var res = null; | |
if ($x) { | |
$truebody | |
... | |
res = $truelast; | |
} else { | |
$falsebody | |
... | |
res = $falselast | |
} | |
return res; | |
}()) | |
} | |
} | |
var fortytwo = 42; | |
console.log(iff(fortytwo == 42) { | |
var temp = fortytwo + 2; | |
"" + temp | |
} els { | |
null; | |
"not!" | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prints