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
| // No parens | |
| Assert.AreEqual(Parser.Parse("10 + 20 * 30").Eval(), 610); | |
| // Parens | |
| Assert.AreEqual(Parser.Parse("(10 + 20) * 30").Eval(), 900); | |
| // Parens and negative | |
| Assert.AreEqual(Parser.Parse("-(10 + 20) * 30").Eval(), -900); | |
| // Nested |
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
| // Parenthesis? | |
| if (_tokenizer.Token == Token.OpenParens) | |
| { | |
| // Skip '(' | |
| _tokenizer.NextToken(); | |
| // Parse a top-level expression | |
| var node = ParseAddSubtract(); | |
| // Check and skip ')' |
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
| // Negative | |
| Assert.AreEqual(Parser.Parse("-10").Eval(), -10); | |
| // Positive | |
| Assert.AreEqual(Parser.Parse("+10").Eval(), 10); | |
| // Negative of a negative | |
| Assert.AreEqual(Parser.Parse("--10").Eval(), 10); | |
| // Woah! |
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(); | |
| } |
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
| [TestMethod] | |
| public void AddSubtractTest() | |
| { | |
| // Add | |
| Assert.AreEqual(Parser.Parse("10 + 20").Eval(), 30); | |
| // Subtract | |
| Assert.AreEqual(Parser.Parse("10 - 20").Eval(), -10); | |
| // Sequence |
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
| public class Parser | |
| { | |
| // Constructor - just store the tokenizer | |
| public Parser(Tokenizer tokenizer) | |
| { | |
| _tokenizer = tokenizer; | |
| } | |
| Tokenizer _tokenizer; |
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
| // Construct an expression tree | |
| var expr = new NodeBinary( | |
| new NodeNumber(10), | |
| new NodeNumber(20), | |
| (a,b) => a + b | |
| ); | |
| // Evaluate it | |
| var result = expr.Eval(); |
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
| // NodeBinary for binary operations such as Add, Subtract etc... | |
| class NodeBinary : Node | |
| { | |
| // Constructor accepts the two nodes to be operated on and function | |
| // that performs the actual operation | |
| public NodeBinary(Node lhs, Node rhs, Func<double, double, double> op) | |
| { | |
| _lhs = lhs; | |
| _rhs = rhs; | |
| _op = op; |
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
| public abstract class Node | |
| { | |
| public abstract double Eval(); | |
| } | |
| class NodeNumber : Node | |
| { | |
| public NodeNumber(double number) | |
| { | |
| _number = number; |
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
| var testString = "10 + 20 - 30.123"; | |
| var t = new Tokenizer(new StringReader(testString)); | |
| // "10" | |
| Assert.AreEqual(t.Token, Token.Number); | |
| Assert.AreEqual(t.Number, 10); | |
| t.NextToken(); | |
| // "+" | |
| Assert.AreEqual(t.Token, Token.Add); |