Skip to content

Instantly share code, notes, and snippets.

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