Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,40 @@ Below is an example that fills a destination span with coordinates for the point
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 unsafe void GetCirclePositions(float value, bool isDiameter, USpan<Vector2> positions)
public void GetCirclePositions(float radius, Span<Vector2> positions)
{
using Machine vm = new();
vm.SetVariable("value", value);
vm.SetVariable("multiplier", isDiameter ? 2 : 1);
vm.SetFunction("cos", &Cos);
vm.SetFunction("sin", &Sin);
vm.SetFunction("cos", MathF.Cos);
vm.SetFunction("sin", MathF.Sin);

uint length = positions.Length;
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) * (value * multiplier)");
vm.SetSource("cos(t) * radius");
float x = vm.Evaluate();
vm.SetSource("sin(t) * (value * multiplier)");
vm.SetSource("sin(t) * radius");
float y = vm.Evaluate();
positions[i] = new Vector2(x, y);
}
}
```

[UnmanagedCallersOnly]
static float Cos(float value)
{
return MathF.Cos(value);
}
### Checking for compilation issues

[UnmanagedCallersOnly]
static float Sin(float value)
{
return MathF.Sin(value);
}
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;
}
```

Expand Down
60 changes: 60 additions & 0 deletions source/CompilationResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;

namespace ExpressionMachine
{
/// <summary>
/// Represents the result of compilation.
/// </summary>
public readonly struct CompilationResult : IEquatable<CompilationResult>
{
/// <summary>
/// Success compilation result.
/// </summary>
public static CompilationResult Success => new(null);

/// <summary>
/// Compilation exception if there was one.
/// </summary>
public readonly Exception? exception;

/// <summary>
/// Checks if the compilation was successful.
/// </summary>
public readonly bool IsSuccess => exception is null;

internal CompilationResult(Exception? exception)
{
this.exception = exception;
}

/// <inheritdoc/>
public readonly override bool Equals(object? obj)
{
return obj is CompilationResult result && Equals(result);
}

/// <inheritdoc/>
public readonly bool Equals(CompilationResult other)
{
return exception == other.exception;
}

/// <inheritdoc/>
public readonly override int GetHashCode()
{
return exception?.GetHashCode() ?? 0;
}

/// <inheritdoc/>
public static bool operator ==(CompilationResult left, CompilationResult right)
{
return left.Equals(right);
}

/// <inheritdoc/>
public static bool operator !=(CompilationResult left, CompilationResult right)
{
return !(left == right);
}
}
}
20 changes: 20 additions & 0 deletions source/Exceptions/MissingTokenException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace ExpressionMachine
{
/// <summary>
/// Error when an expected token is missing.
/// </summary>
public class MissingTokenException : Exception
{

}

/// <summary>
/// Error when a token to close a group is missing.
/// </summary>
public class MissingGroupCloseToken : Exception
{

}
}
13 changes: 13 additions & 0 deletions source/ExpressionMachine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Title>Expression Machine</Title>
<Authors>popcron</Authors>
<Company>simulation-tree</Company>
<Description>Library for evaluating logic expressions at runtime.</Description>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/simulation-tree/expression-machine</RepositoryUrl>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand All @@ -23,6 +29,13 @@
<WarningLevel>7</WarningLevel>
</PropertyGroup>

<ItemGroup>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\collections\source\Collections.csproj" />
<ProjectReference Include="..\..\unmanaged\core\Unmanaged.Core.csproj" />
Expand Down
Loading