Skip to content

Instantly share code, notes, and snippets.

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

  • Save toptensoftware/139e03cea3ee549f127b2026898dd298 to your computer and use it in GitHub Desktop.

Select an option

Save toptensoftware/139e03cea3ee549f127b2026898dd298 to your computer and use it in GitHub Desktop.
class MyContext : IContext
{
public MyContext(double r)
{
_r = r;
}
double _r;
public double ResolveVariable(string name)
{
switch (name)
{
case "pi": return Math.PI;
case "r": return _r;
}
throw new InvalidDataException($"Unknown variable: '{name}'");
}
}
[TestMethod]
public void Variables()
{
var ctx = new MyContext(10);
Assert.AreEqual(Parser.Parse("2 * pi * r").Eval(ctx), 2 * Math.PI * 10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment