Evaluating expressions
The simpler approach
To evaluate an expression with AddyScript, do the following:
- Type the expression into an editor (I'll assume the editor is a text widget named txtExpr).
- Add a reference to AddyScript.dll in your project.
- Create a GUI in your project to allow the user to invoke the scripting engine.
- In your code-behind file, import the AddyScript namespace.
- You can import any additional namespaces from the AddyScript assembly depending on what you intend to do.
- Finally, type a code snippet like this into an event handler:
| C# |
|---|
| var context = new ScriptContext();
context.Bindings["myString"] = "Hello!";
context.Bindings["myFloat"] = 0.9;
var result = ScriptEngine.EvaluateString(txExpr.Text, context);
Console.WriteLine($"Given {context.Bindings["myString"]} and {context.Bindings["myFloat"]}, we obtain {result}");
|
Notes:
Don't forget to embed this code in a try-catch block.
Parsing once, running later
Below is another example where the expression is parsed once and evaluated multiple times in a loop.
Before testing it, you need to create the GUI and provide the logic for the MoveTo and LineTo methods yourself:
| C# |
|---|
| // txtFunction is a text widget where the user types the expression in.
// A single parameter named 'x' is expected.
var expression = ScriptEngine.ParseExpression(txtFunction.Text);
var context = new ScriptContext();
// txtFrom, txtTo and txtBy are text widgets where the user types the plotting range in.
double start = double.Parse(txtFrom.Text);
double end = double.Parse(txtTo.Text);
double step = double.Parse(txtBy.Text);
// Determine the initial point and move the graphical cursor there.
double x = start;
context.Bindings["x"] = x;
double y = ScriptEngine.Evaluate(expression, context).AsDouble;
MoveTo(x, y);
// Plot the curve segment by segment.
do
{
x += step;
context.Bindings["x"] = x;
y = ScriptEngine.Evaluate(expression, context).AsDouble;
LineTo(x, y);
} while (x < end);
|