Skip to content

Commit aede1c7

Browse files
authored
Merge pull request #3 from simulation-tree/dev/catching-compilation-errors
Catching compilation errors
2 parents 126a2d0 + 8afcf30 commit aede1c7

9 files changed

Lines changed: 567 additions & 260 deletions

File tree

README.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,40 @@ Below is an example that fills a destination span with coordinates for the point
1515
with either a radius or a diameter as input. While reusing the same machine instance by modifying
1616
its source and variables, and re-evaluating the expression.
1717
```cs
18-
public unsafe void GetCirclePositions(float value, bool isDiameter, USpan<Vector2> positions)
18+
public void GetCirclePositions(float radius, Span<Vector2> positions)
1919
{
2020
using Machine vm = new();
2121
vm.SetVariable("value", value);
22-
vm.SetVariable("multiplier", isDiameter ? 2 : 1);
23-
vm.SetFunction("cos", &Cos);
24-
vm.SetFunction("sin", &Sin);
22+
vm.SetFunction("cos", MathF.Cos);
23+
vm.SetFunction("sin", MathF.Sin);
2524

26-
uint length = positions.Length;
25+
int length = positions.Length;
2726
for (int i = 0; i < length; i++)
2827
{
2928
float t = i * MathF.PI / (length * 0.5f);
3029
vm.SetVariable("t", t);
31-
vm.SetSource("cos(t) * (value * multiplier)");
30+
vm.SetSource("cos(t) * radius");
3231
float x = vm.Evaluate();
33-
vm.SetSource("sin(t) * (value * multiplier)");
32+
vm.SetSource("sin(t) * radius");
3433
float y = vm.Evaluate();
3534
positions[i] = new Vector2(x, y);
3635
}
36+
}
37+
```
3738

38-
[UnmanagedCallersOnly]
39-
static float Cos(float value)
40-
{
41-
return MathF.Cos(value);
42-
}
39+
### Checking for compilation issues
4340

44-
[UnmanagedCallersOnly]
45-
static float Sin(float value)
46-
{
47-
return MathF.Sin(value);
48-
}
41+
When a text source is assigned to the machine, it returns a compilation result.
42+
This result value can be used to check if there were issues. And can do so with the try-do pattern:
43+
```cs
44+
if (vm.TrySetSource("5 +", out Exception? exception))
45+
{
46+
//success
47+
}
48+
else
49+
{
50+
//error
51+
throw exception;
4952
}
5053
```
5154

source/CompilationResult.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
3+
namespace ExpressionMachine
4+
{
5+
/// <summary>
6+
/// Represents the result of compilation.
7+
/// </summary>
8+
public readonly struct CompilationResult : IEquatable<CompilationResult>
9+
{
10+
/// <summary>
11+
/// Success compilation result.
12+
/// </summary>
13+
public static CompilationResult Success => new(null);
14+
15+
/// <summary>
16+
/// Compilation exception if there was one.
17+
/// </summary>
18+
public readonly Exception? exception;
19+
20+
/// <summary>
21+
/// Checks if the compilation was successful.
22+
/// </summary>
23+
public readonly bool IsSuccess => exception is null;
24+
25+
internal CompilationResult(Exception? exception)
26+
{
27+
this.exception = exception;
28+
}
29+
30+
/// <inheritdoc/>
31+
public readonly override bool Equals(object? obj)
32+
{
33+
return obj is CompilationResult result && Equals(result);
34+
}
35+
36+
/// <inheritdoc/>
37+
public readonly bool Equals(CompilationResult other)
38+
{
39+
return exception == other.exception;
40+
}
41+
42+
/// <inheritdoc/>
43+
public readonly override int GetHashCode()
44+
{
45+
return exception?.GetHashCode() ?? 0;
46+
}
47+
48+
/// <inheritdoc/>
49+
public static bool operator ==(CompilationResult left, CompilationResult right)
50+
{
51+
return left.Equals(right);
52+
}
53+
54+
/// <inheritdoc/>
55+
public static bool operator !=(CompilationResult left, CompilationResult right)
56+
{
57+
return !(left == right);
58+
}
59+
}
60+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace ExpressionMachine
4+
{
5+
/// <summary>
6+
/// Error when an expected token is missing.
7+
/// </summary>
8+
public class MissingTokenException : Exception
9+
{
10+
11+
}
12+
13+
/// <summary>
14+
/// Error when a token to close a group is missing.
15+
/// </summary>
16+
public class MissingGroupCloseToken : Exception
17+
{
18+
19+
}
20+
}

source/ExpressionMachine.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
<Nullable>enable</Nullable>
77
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
88
<GenerateDocumentationFile>True</GenerateDocumentationFile>
9+
<Title>Expression Machine</Title>
10+
<Authors>popcron</Authors>
11+
<Company>simulation-tree</Company>
12+
<Description>Library for evaluating logic expressions at runtime.</Description>
13+
<PackageReadmeFile>README.md</PackageReadmeFile>
14+
<RepositoryUrl>https://github.com/simulation-tree/expression-machine</RepositoryUrl>
915
</PropertyGroup>
1016

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

32+
<ItemGroup>
33+
<None Include="..\README.md">
34+
<Pack>True</Pack>
35+
<PackagePath>\</PackagePath>
36+
</None>
37+
</ItemGroup>
38+
2639
<ItemGroup>
2740
<ProjectReference Include="..\..\collections\source\Collections.csproj" />
2841
<ProjectReference Include="..\..\unmanaged\core\Unmanaged.Core.csproj" />

0 commit comments

Comments
 (0)