Created
October 26, 2016 03:45
-
-
Save toptensoftware/01ebf0e6c5805350f287b0ed6cae4f89 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
| class MyFunctionContext : IContext | |
| { | |
| public MyFunctionContext() | |
| { | |
| } | |
| public double ResolveVariable(string name) | |
| { | |
| throw new InvalidDataException($"Unknown variable: '{name}'"); | |
| } | |
| public double CallFunction(string name, double[] arguments) | |
| { | |
| if (name == "rectArea") | |
| { | |
| return arguments[0] * arguments[1]; | |
| } | |
| if (name == "rectPerimeter") | |
| { | |
| return (arguments[0] + arguments[1]) * 2; | |
| } | |
| throw new InvalidDataException($"Unknown function: '{name}'"); | |
| } | |
| } | |
| [TestMethod] | |
| public void Functions() | |
| { | |
| var ctx = new MyFunctionContext(); | |
| Assert.AreEqual(Parser.Parse("rectArea(10,20)").Eval(ctx), 200); | |
| Assert.AreEqual(Parser.Parse("rectPerimeter(10,20)").Eval(ctx), 60); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment