Skip to content

Instantly share code, notes, and snippets.

@natefaubion
Created December 7, 2013 23:57
Show Gist options
  • Save natefaubion/7851541 to your computer and use it in GitHub Desktop.
Save natefaubion/7851541 to your computer and use it in GitHub Desktop.
withSyntax with repeaters
macro withSyntax_unzip {
case { _ $name ($ps ...) ($es ...) ($p = $e:expr , $rest ...) $body } => {
return #{
withSyntax_unzip $name ($ps ... ($p)) ($es ... ($e)) ($rest ...) $body
}
}
case { _ $name ($ps ...) ($es ...) ($p = $e:expr) $body } => {
return #{
withSyntax_unzip $name ($ps ... ($p)) ($es ... ($e)) () $body
}
}
case { _ $name ($ps ...) ($es ...) ($p $punc = $e:expr , $rest ...) $body } => {
var punc = #{$punc};
if (punc[0].token.type !== parser.Token.Punctuator ||
punc[0].token.value !== '...') {
throwSyntaxError('withSyntax', 'Unexpected token', punc);
}
return #{
withSyntax_unzip $name ($ps ... ($p $punc)) ($es ... ($e)) ($rest ...) $body
}
}
case { $name ($ps ...) ($es ...) ($p $punc = $e:expr) $body } => {
var punc = #{$punc};
if (punc[0].token.type !== parser.Token.Punctuator ||
punc[0].token.value !== '...') {
throwSyntaxError('withSyntax', 'Unexpected token', punc);
}
return #{
withSyntax_unip $name ($ps ... ($p $punc)) ($es ... ($e)) () $body
}
}
case { _ $name ($ps ...) ($es ...) () $body } => {
var name = #{$name};
var here = #{here};
var res = [makeIdent('syntaxCase', name)];
var args = #{[$(makeDelim('()', $es)) (,) ...],};
args = args.concat(makeIdent('context', name));
res = res.concat(makeDelim('()', args, here));
res = res.concat(#{
{ case { $ps ... } => $body }
});
return [makeDelim('()', res, here),
makePunc('.', here),
makeIdent('result', here)];
}
}
let withSyntax = macro {
case { $name ($vars ...) {$body ...} } => {
return #{
withSyntax_unzip $name () () ($vars ...) {$body ...}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment