https://github.com/simulation-tree/expression-machine
Mathmetical expression interpreter
https://github.com/simulation-tree/expression-machine
csharp dotnet expressions interpreter scripting
Last synced: 5 months ago
JSON representation
Mathmetical expression interpreter
- Host: GitHub
- URL: https://github.com/simulation-tree/expression-machine
- Owner: simulation-tree
- License: mit
- Created: 2024-05-17T09:45:59.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-09-24T03:03:22.000Z (9 months ago)
- Last Synced: 2025-09-24T05:18:29.861Z (9 months ago)
- Topics: csharp, dotnet, expressions, interpreter, scripting
- Language: C#
- Homepage:
- Size: 70.3 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Expression Machine
[](https://github.com/simulation-tree/expression-machine/actions/workflows/test.yml)
Library for evaluating logic expressions at runtime.
### Features
- Basic arithmetic operations (addition, subtraction, multiplication, division)
- Parentheses for grouping operations
- Injectable `float` variables
- 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.
```cs
public void GetCirclePositions(float radius, Span 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:
```cs
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.