Skip to content

Instantly share code, notes, and snippets.

@toptensoftware
Created October 26, 2016 03:45
Show Gist options
  • Select an option

  • Save toptensoftware/01ebf0e6c5805350f287b0ed6cae4f89 to your computer and use it in GitHub Desktop.

Select an option

Save toptensoftware/01ebf0e6c5805350f287b0ed6cae4f89 to your computer and use it in GitHub Desktop.
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