Skip to content

Instantly share code, notes, and snippets.

@kl0tl
Last active August 29, 2015 14:02
Show Gist options
  • Save kl0tl/3c3c880c7be79e0ceedc to your computer and use it in GitHub Desktop.
Save kl0tl/3c3c880c7be79e0ceedc to your computer and use it in GitHub Desktop.
Parens free `for`, `for in` and `for own` statements
/*
```
for var i = 0; i < length; i += 1 {
...
}
for var key in object {
...
}
for own var key in object {
...
}
```
Brackets are mandatory
A `for own` loop iterates only over own properties
*/
macro assignment {
rule { $symbol:ident = $value:expr }
rule { $symbol:ident }
}
macro statement {
rule { ($expression:expr (,) ...) }
rule { $expression:expr (,) ... }
}
macro for_assignment {
rule { var $assignment:assignment (,) ... }
rule { $statement:statement }
}
macro for_statements {
rule { $assignments:for_assignment ; $cond:statement ; $final:statement }
}
macro for_in_assignment {
rule { var $assignment:assignment }
rule { $symbol:ident }
}
let for = macro {
rule { ($statements:for_statements) } => {
for ($statements)
}
rule { $statements:for_statements ; } => {
for ($statements);
}
rule { $statements:for_statements { $body ... } } => {
for ($statements) { $body ... }
}
rule { ($assignment:for_in_assignment in $object:expr) } => {
for ($assignment in $object)
}
rule { $assignment:for_in_assignment in $object:expr ; } => {
for ($assignment in $object);
}
rule { $assignment:for_in_assignment in $object:expr { $body ... } } => {
for ($assignment in $object) { $body ... }
}
rule { own $assignment:for_in_assignment in $object:expr ; } => {
var ref = $object, proto = ref.__proto__;
ref.__proto__ = null;
for ($assignment in ref);
ref.__proto__ = proto;
}
rule { own $assignment:for_in_assignment in $object:expr { $body ... } } => {
var ref = $object, proto = ref.__proto__;
ref.__proto__ = null;
for ($assignment in ref) { $body ... }
ref.__proto__ = proto;
}
}
export for;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment