Skip to content

Commit

Permalink
Fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
vanifatovvlad committed Dec 20, 2022
1 parent 2c69d99 commit 4aa5c21
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
19 changes: 12 additions & 7 deletions Runtime/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public abstract class ExpressionParser<T>
private readonly Dictionary<string, ExprBuilder> _builderCached = new Dictionary<string, ExprBuilder>();
private Parser<ExprBuilder> _parserCached;

public Expression<bool> CompilePredicate(string input, ExpresionContext<T> context, bool cache)
public Expression<bool> CompilePredicate(string input, ExpressionContext<T> context, bool cache)
{
var expr = Compile(input, context, cache);
return () => IsTrue(expr.Invoke());
}

public Expression<T> Compile(string input, ExpresionContext<T> context, bool cache)
public Expression<T> Compile(string input, ExpressionContext<T> context, bool cache)
{
context = context ?? new ExpresionContext<T>();
context = context ?? new ExpressionContext<T>();

_parserCached = _parserCached ?? CreateParser();

Expand Down Expand Up @@ -177,7 +177,7 @@ select MakeUnary(Negate, fact)
private T Min(T a, T b) => IsTrue(GreaterThan(a, b)) ? b : a;
private T Max(T a, T b) => IsTrue(GreaterThan(b, a)) ? b : a;

private delegate Expression<T> ExprBuilder(ExpresionContext<T> context);
private delegate Expression<T> ExprBuilder(ExpressionContext<T> context);

private delegate T UnaryFunc(T a);

Expand Down Expand Up @@ -330,14 +330,14 @@ private static ExprBuilder MakeConstant(string valueString, Func<string, T> pars
}
}

public class ExpresionContext<T>
public class ExpressionContext<T>
{
private readonly ExpresionContext<T> _parent;
private readonly ExpressionContext<T> _parent;
private readonly Func<string, Expression<T>> _unregisteredVariableResolver;

private readonly Dictionary<string, Expression<T>> _variables = new Dictionary<string, Expression<T>>();

public ExpresionContext(ExpresionContext<T> parent = null,
public ExpressionContext(ExpressionContext<T> parent = null,
Func<string, Expression<T>> unregisteredVariableResolver = null)
{
_parent = parent;
Expand Down Expand Up @@ -385,6 +385,11 @@ public Expression<T> GetVariable(string name, bool nullIsOk = false)
}
}

[Obsolete("ExpresionContext contains a typo. Use ExpressionContext instead", true)]
public class ExpresionContext<T> : ExpressionContext<T>
{
}

public class VariableNotDefinedException : Exception
{
public VariableNotDefinedException(string name)
Expand Down
18 changes: 9 additions & 9 deletions Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public class ParserTests
[TestCase("NOT(a) AND NOT(b)", 1, 0, ExpectedResult = 0)]
public float Parse_Variable(string input, float a, float b)
{
var context = new ExpresionContext<float>();
var context = new ExpressionContext<float>();
context.RegisterVariable("a", () => a);
context.RegisterVariable("b", () => b);
return Execute(input, context);
Expand All @@ -133,15 +133,15 @@ public float Parse_Variable(string input, float a, float b)
[Test]
public void ComplexVariableName()
{
var context = new ExpresionContext<float>();
var context = new ExpressionContext<float>();
context.RegisterVariable("Some_Variable", () => 1);
Assert.AreEqual(1, Execute("Some_Variable", context));
}

[Test]
public void ReadmeSample()
{
var context = new ExpresionContext<float>();
var context = new ExpressionContext<float>();

context.RegisterVariable("a", () => 1);
context.RegisterVariable("b", () => 2);
Expand All @@ -157,7 +157,7 @@ public void ReadmeSample()
[Test]
public void Parse_Variable_Names()
{
var context = new ExpresionContext<float>();
var context = new ExpressionContext<float>();
context.RegisterVariable("o", () => 1);
context.RegisterVariable("one", () => 1);
context.RegisterVariable("one123", () => 1);
Expand Down Expand Up @@ -192,7 +192,7 @@ public void Parse_MinMax_Invalid()
[Test]
public void Parse_If()
{
var context = new ExpresionContext<float>();
var context = new ExpressionContext<float>();

var n = 0;
context.RegisterVariable("n", () => n);
Expand All @@ -218,8 +218,8 @@ public void Parse_If()
[Test]
public void ContextTree()
{
var rootContext = new ExpresionContext<float>();
var subContext = new ExpresionContext<float>(rootContext);
var rootContext = new ExpressionContext<float>();
var subContext = new ExpressionContext<float>(rootContext);

var rootA = 1;
var rootB = 2;
Expand Down Expand Up @@ -252,12 +252,12 @@ public bool Parse_Predicate(string input)
return FloatExpressionParser.Instance.CompilePredicate(input, null, false).Invoke();
}

private static float Execute(string input, ExpresionContext<float> context)
private static float Execute(string input, ExpressionContext<float> context)
{
return Compile(input, context).Invoke();
}

private static Expression<float> Compile(string input, ExpresionContext<float> context)
private static Expression<float> Compile(string input, ExpressionContext<float> context)
{
return FloatExpressionParser.Instance.Compile(input, context, false);
}
Expand Down

0 comments on commit 4aa5c21

Please sign in to comment.