Last active
October 26, 2016 04:44
-
-
Save toptensoftware/99db03941e25e852bedb3f10ad3ded68 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
| // 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