Skip to content

Instantly share code, notes, and snippets.

@aaronpowell
Last active August 29, 2015 13:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aaronpowell/9967406 to your computer and use it in GitHub Desktop.
Save aaronpowell/9967406 to your computer and use it in GitHub Desktop.
Trying out the C# vNext `?.` operator in Sweet.js
macro null_helper {
rule { ($processed ...) ($rest) } => {
$processed (.) ... . $rest
}
rule { ($processed ...) ($rest_head $rest ...) } => {
$processed (.) ... . $rest_head
&& null_helper ($processed ... $rest_head) ($rest ...)
}
}
macro (?.) {
rule { ($x) } => {
$x
}
rule infix { $head | $rest (.) ... } => {
$head && null_helper ($head) ($rest ...)
}
}
var x = {
y: { z: { a: 42 } }
};
var foo = x?.y.z.a
console.log(foo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment