Skip to content

Instantly share code, notes, and snippets.

@kl0tl
Last active August 29, 2015 14:02
Show Gist options
  • Save kl0tl/b638e3fd1505526687ed to your computer and use it in GitHub Desktop.
Save kl0tl/b638e3fd1505526687ed to your computer and use it in GitHub Desktop.
Parens free `unless` statement, the opposite of `if`
/*
`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