Created
January 12, 2017 09:15
-
-
Save bakso/616cba9ccb1864474ebc865203570b90 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
function buildBinaryExpression(op, left, right) { | |
return { | |
type: 'BinaryExpression', | |
operator: op, | |
left: left, | |
right: right | |
} | |
} | |
} | |
Expression | |
= head:Integer tails:(ExpressionRest)* { | |
return tails.reduce(function(prev, cur){ | |
return buildBinaryExpression(cur.operator, prev, cur.right); | |
}, head); | |
} | |
ExpressionRest | |
= _ f:Expression _ op:Operator { | |
return { | |
operator: op, | |
right: f | |
} | |
} | |
Operator | |
= "+" | |
/ "-" | |
/ "*" | |
/ "/" | |
Integer "integer" | |
= [0-9]+ { | |
return parseInt(text(), 10); | |
} | |
_ "whitespace" | |
= " "* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment