Skip to content

Instantly share code, notes, and snippets.

@toptensoftware
Last active October 26, 2016 04:44
Show Gist options
  • Select an option

  • Save toptensoftware/99db03941e25e852bedb3f10ad3ded68 to your computer and use it in GitHub Desktop.

Select an option

Save toptensoftware/99db03941e25e852bedb3f10ad3ded68 to your computer and use it in GitHub Desktop.
// Parse a unary operator (eg: negative/positive)
Node ParseUnary()
{
// Positive operator is a no-op so just skip it
if (_tokenizer.Token == Token.Add)
{
// Skip
_tokenizer.NextToken();
return ParseUnary();
}
// Negative operator
if (_tokenizer.Token == Token.Subtract)
{
// Skip
_tokenizer.NextToken();
// Parse RHS
// Note this recurses to self to support negative of a negative
var rhs = ParseUnary();
// Create unary node
return new NodeUnary(rhs, (a) => -a);
}
// No positive/negative operator so parse a leaf node
return ParseLeaf();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment