SimulationTree.ExpressionMachine
0.3.9
Prefix Reserved
dotnet add package SimulationTree.ExpressionMachine --version 0.3.9
NuGet\Install-Package SimulationTree.ExpressionMachine -Version 0.3.9
<PackageReference Include="SimulationTree.ExpressionMachine" Version="0.3.9" />
<PackageVersion Include="SimulationTree.ExpressionMachine" Version="0.3.9" />
<PackageReference Include="SimulationTree.ExpressionMachine" />
paket add SimulationTree.ExpressionMachine --version 0.3.9
#r "nuget: SimulationTree.ExpressionMachine, 0.3.9"
#:package SimulationTree.ExpressionMachine@0.3.9
#addin nuget:?package=SimulationTree.ExpressionMachine&version=0.3.9
#tool nuget:?package=SimulationTree.ExpressionMachine&version=0.3.9
Expression Machine
Library for evaluating logic expressions at runtime.
Features
- Basic arithmetic operations (addition, subtraction, multiplication, division)
- Parentheses for grouping operations
- Injectable
floatvariables - Injectable functions accepting one or no input arguments
Example
Below is an example that fills a destination span with coordinates for the points of a circle, with either a radius or a diameter as input. While reusing the same machine instance by modifying its source and variables, and re-evaluating the expression.
public void GetCirclePositions(float radius, Span<Vector2> positions)
{
using Machine vm = new();
vm.SetVariable("value", value);
vm.SetFunction("cos", MathF.Cos);
vm.SetFunction("sin", MathF.Sin);
int length = positions.Length;
for (int i = 0; i < length; i++)
{
float t = i * MathF.PI / (length * 0.5f);
vm.SetVariable("t", t);
vm.SetSource("cos(t) * radius");
float x = vm.Evaluate();
vm.SetSource("sin(t) * radius");
float y = vm.Evaluate();
positions[i] = new Vector2(x, y);
}
}
Checking for compilation issues
When a text source is assigned to the machine, it returns a compilation result. This result value can be used to check if there were issues. And can do so with the try-do pattern:
if (vm.TrySetSource("5 +", out Exception? exception))
{
//success
}
else
{
//error
throw exception;
}
Contributions and direction
This library is small and isn't mean to substitute things like Lua or other languages within interpreters. Instead, it's more fitting as a base to extend upon and branch away. And it should remain as unmanaged as it is. Without extending it, it's most useful fruit is allowing your code to express different values, all through a single C# variable via different expressions.
Contributions that align with this are welcome.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- SimulationTree.Collections (>= 0.3.9)
- SimulationTree.Unmanaged.Core (>= 0.3.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.3.9 | 187 | 9/24/2025 |