Created
October 6, 2013 23:15
-
-
Save natefaubion/6860380 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let let = macro { | |
case { $mac syntax $id:ident $punc = $rhs:expr } => { | |
var mac = #{ $mac }; | |
var id = #{ $id }; | |
var val = #{ $val }; | |
var arg = #{ $($rhs) }; | |
var punc = #{ $punc }; | |
if (punc[0].token.type !== parser.Token.Punctuator || | |
punc[0].token.value !== '...') { | |
throw new SyntaxError('Unexpected token: ' + punc[0].token.value + | |
' (expected ...)'); | |
} | |
if (id[0].token.value[0] !== '$') { | |
throw new SyntaxError('Syntax identifiers must start with $: ' + | |
id[0].token.value); | |
} | |
return res = [ | |
makeIdent('match', mac), | |
makePunc('.'), | |
makeIdent('patternEnv'), | |
makeDelim('[]', [makeValue(id[0].token.value)]), | |
makePunc('='), | |
makeDelim('{}', [ | |
makeIdent('level'), makePunc(':'), makeValue(1), makePunc(','), | |
makeIdent('match'), makePunc(':'), arg[0], makePunc('.'), | |
makeIdent('map'), makeDelim('()', #{ | |
function(t) { | |
return { | |
level: 0, | |
match: [t] | |
}; | |
} | |
}) | |
]) | |
]; | |
} | |
case { $mac syntax $id:ident = $rhs:expr } => { | |
var mac = #{ $mac }; | |
var id = #{ $id }; | |
var val = #{ $val }; | |
var arg = #{ $($rhs) }; | |
if (id[0].token.value[0] !== '$') { | |
throw new SyntaxError('Syntax identifiers must start with $: ' + | |
id[0].token.value); | |
} | |
return res = [ | |
makeIdent('match', mac), | |
makePunc('.'), | |
makeIdent('patternEnv'), | |
makeDelim('[]', [makeValue(id[0].token.value)]), | |
makePunc('='), | |
makeDelim('{}', [ | |
makeIdent('level'), makePunc(':'), makeValue(0), makePunc(','), | |
makeIdent('match'), makePunc(':'), arg[0] | |
]) | |
]; | |
} | |
case { _ } => { | |
return #{ let }; | |
} | |
} | |
macro $test { | |
case { $mac () } => { | |
let syntax $foo ... = [makeValue(12), makePunc('+'), makeValue(42)]; | |
return #{ $foo ... } | |
} | |
} | |
$test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment