Last active
August 29, 2015 14:02
-
-
Save kl0tl/b638e3fd1505526687ed to your computer and use it in GitHub Desktop.
Parens free `unless` statement, the opposite of `if`
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
/* | |
`unless cond {}` is equivalent to `if !(cond) {}` | |
Brackets are mandatory, except when `unless` is followed by specials keywords (return, break and continue) | |
``` | |
unless cond return | |
unless cond break [label] | |
unless cond continue [label] | |
``` | |
*/ | |
macro unless { | |
rule { $cond:expr { $body ... } } => { | |
if (!$cond) { $body ... } | |
} | |
rule { $cond ; } => { | |
if (!$cond); | |
} | |
rule { $cond:expr return $return:expr } => { | |
if (!$cond) return $return | |
} | |
rule { $cond:expr return } => { | |
if (!$cond) return | |
} | |
rule { $cond:expr continue $label:ident } => { | |
if (!$cond) continue $label | |
} | |
rule { $cond:expr continue } => { | |
if (!$cond) continue | |
} | |
rule { $cond:expr break $label:ident } => { | |
if (!$cond) break $label | |
} | |
rule { $cond:expr break } => { | |
if (!$cond) break | |
} | |
rule { $cond:expr do $body } => { | |
if (!$cond) do $body | |
} | |
} | |
export unless; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment