Last active
August 29, 2015 14:02
-
-
Save kl0tl/b66f2c70fee61c010ead to your computer and use it in GitHub Desktop.
Parens free `switch` expression
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
/* | |
Multiple expressions per case are allowed | |
The result of the matching case, or the optional default, is automatically returned | |
``` | |
var x = switch os { | |
case 'osx', 'ios': 'apple' | |
default: os | |
}; | |
``` | |
*/ | |
let switch = macro { | |
rule { $value:expr { $body ... } } => { | |
(function () { | |
switch ($value) { | |
$body ... | |
} | |
})() | |
} | |
} | |
let case = macro { | |
rule { $($pattern:expr (,) ...): $return:expr } => { | |
$(case $pattern:) ... | |
return $return | |
} | |
} | |
let default = macro { | |
rule { : $return:expr } => { | |
default: | |
return $return | |
} | |
} | |
export switch; | |
export case; | |
export default; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment