Skip to content

Instantly share code, notes, and snippets.

@bakso
Created January 12, 2017 09:15
Show Gist options
  • Save bakso/616cba9ccb1864474ebc865203570b90 to your computer and use it in GitHub Desktop.
Save bakso/616cba9ccb1864474ebc865203570b90 to your computer and use it in GitHub Desktop.
{
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