Skip to content

Commit

Permalink
API Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
RupertAvery committed May 5, 2014
1 parent 21a77f6 commit 8acb0b9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
22 changes: 11 additions & 11 deletions CompiledExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public CompiledExpression(string expression)

public Func<TResult> Compile(bool isCall = false)
{
if (Expression == null) Expression = WrapExpression(BuildTree(), false);
Expression = WrapExpression(BuildTree(), false);
return Expression.Lambda<Func<TResult>>(Expression).Compile();
}

Expand All @@ -37,7 +37,7 @@ public Func<TResult> Compile(bool isCall = false)
/// <returns></returns>
public Action CompileCall()
{
if (Expression == null) Expression = BuildTree(null, true);
Expression = BuildTree(null, true);
return Expression.Lambda<Action>(Expression).Compile();
}

Expand All @@ -49,22 +49,22 @@ public Action CompileCall()
public Action<TParam> ScopeCompileCall<TParam>()
{
var scopeParam = Expression.Parameter(typeof(TParam), "scope");
if (Expression == null) Expression = BuildTree(scopeParam, true);
Expression = BuildTree(scopeParam, true);
return Expression.Lambda<Action<TParam>>(Expression, new ParameterExpression[] { scopeParam }).Compile();
}


public Func<object, TResult> ScopeCompile()
{
var scopeParam = Expression.Parameter(typeof(object), "scope");
if (Expression == null) Expression = WrapExpression(BuildTree(scopeParam), false);
Expression = WrapExpression(BuildTree(scopeParam), false);
return Expression.Lambda<Func<dynamic, TResult>>(Expression, new ParameterExpression[] { scopeParam }).Compile();
}

public Func<TParam, TResult> ScopeCompile<TParam>()
{
var scopeParam = Expression.Parameter(typeof(TParam), "scope");
if (Expression == null) Expression = WrapExpression(BuildTree(scopeParam), false);
Expression = WrapExpression(BuildTree(scopeParam), false);
return Expression.Lambda<Func<TParam, TResult>>(Expression, new ParameterExpression[] { scopeParam }).Compile();
}

Expand Down Expand Up @@ -121,7 +121,7 @@ public CompiledExpression(string expression)
/// <returns></returns>
public Func<object> Compile()
{
if (Expression == null) Expression = WrapExpression(BuildTree(), true);
Expression = WrapExpression(BuildTree(), true);
return Expression.Lambda<Func<object>>(Expression).Compile();
}

Expand All @@ -132,7 +132,7 @@ public Func<object> Compile()
/// <returns></returns>
public Action CompileCall()
{
if (Expression == null) Expression = BuildTree(null, true);
Expression = BuildTree(null, true);
return Expression.Lambda<Action>(Expression).Compile();
}

Expand All @@ -143,7 +143,7 @@ public Action CompileCall()
public Func<object, object> ScopeCompile()
{
var scopeParam = Expression.Parameter(typeof(object), "scope");
if (Expression == null) Expression = WrapExpression(BuildTree(scopeParam), true);
Expression = WrapExpression(BuildTree(scopeParam), true);
return Expression.Lambda<Func<dynamic, object>>(Expression, new ParameterExpression[] { scopeParam }).Compile();
}

Expand All @@ -154,7 +154,7 @@ public Func<object, object> ScopeCompile()
public Action<object> ScopeCompileCall()
{
var scopeParam = Expression.Parameter(typeof(object), "scope");
if (Expression == null) Expression = BuildTree(scopeParam, true);
Expression = BuildTree(scopeParam, true);
return Expression.Lambda<Action<dynamic>>(Expression, new ParameterExpression[] { scopeParam }).Compile();
}

Expand All @@ -165,7 +165,7 @@ public Action<object> ScopeCompileCall()
public Action<TParam> ScopeCompileCall<TParam>()
{
var scopeParam = Expression.Parameter(typeof(TParam), "scope");
if (Expression == null) Expression = WrapToVoid(BuildTree(scopeParam));
Expression = WrapToVoid(BuildTree(scopeParam));
return Expression.Lambda<Action<TParam>>(Expression, new ParameterExpression[] { scopeParam }).Compile();
}

Expand All @@ -177,7 +177,7 @@ public Action<TParam> ScopeCompileCall<TParam>()
public Func<TParam, object> ScopeCompile<TParam>()
{
var scopeParam = Expression.Parameter(typeof(TParam), "scope");
if (Expression == null) Expression = WrapExpression(BuildTree(scopeParam), true);
Expression = WrapExpression(BuildTree(scopeParam), true);
return Expression.Lambda<Func<TParam, object>>(Expression, new ParameterExpression[] { scopeParam }).Compile();
}

Expand Down
6 changes: 1 addition & 5 deletions ExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,9 @@ public T Compile<T>(params string[] parameters)
{
var f = typeof (T);
var argTypes = f.GetGenericArguments();
if (argTypes.Length - parameters.Length != 1)
{
throw new Exception("Type arguments must be 1 more than the number of parameters");
}
var argParams = parameters.Select((t, i) => Expression.Parameter(argTypes[i], t)).ToList();
Parser.ExternalParameters = argParams;
if (Expression == null) Expression = WrapExpression(BuildTree(), true);
Expression = BuildTree();
return Expression.Lambda<T>(Expression, argParams).Compile();
}

Expand Down
6 changes: 5 additions & 1 deletion Parser/AntlrParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public Expression Parse(Expression scope, bool isCall = false)
var lexer = new ExprEvalLexer(input);
var tokens = new TokenRewriteStream(lexer);
if (TypeRegistry == null) TypeRegistry = new TypeRegistry();
var parser = new ExprEvalParser(tokens) { TypeRegistry = TypeRegistry, Scope = scope, IsCall = isCall, ExternalParameters = ExternalParameters };
var parser = new ExprEvalParser(tokens) { TypeRegistry = TypeRegistry, Scope = scope, IsCall = isCall };
if (ExternalParameters != null)
{
parser.ParameterList.Add(ExternalParameters);
}
switch (ExpressionType)
{
case CompiledExpressionType.Expression:
Expand Down
4 changes: 1 addition & 3 deletions Parser/ExprEval.g3.parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public partial class ExprEvalParser
public bool HasReturn { get; private set; }
public TypeRegistry TypeRegistry { get; set; }

public List<ParameterExpression> ExternalParameters { get; set; }

//partial void EnterRule(string ruleName, int ruleIndex)
//{
// base.TraceIn(ruleName, ruleIndex);
Expand Down Expand Up @@ -81,7 +79,7 @@ public Type GetType(string type)
return Type.GetType(type);
}

private ParameterList ParameterList = new ParameterList();
public ParameterList ParameterList = new ParameterList();

}
}
49 changes: 49 additions & 0 deletions TestProject1/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,54 @@ public void MethodOverLoading()
// f9(data);
// Console.WriteLine(data.X);
//}

[TestMethod]
public void CompileToGenericFunc()
{
var data = new MyClass();
data.Y = new List<int>() { 1, 2, 3, 4, 5, 4, 4, 3, 4, 2 };
var c9 = new CompiledExpression() { StringToParse = "y == 4" };
var f9 = c9.Compile<Func<int, bool>>("y");
Assert.AreEqual(4, data.Y.Where(f9).Count());
}

[TestMethod]
public void DynamicValue()
{
var registry = new TypeRegistry();
var obj = new objHolder() { Value = "aa" };
registry.RegisterSymbol("obj", obj);
registry.RegisterDefaultTypes();

// okay
var cc = new CompiledExpression() { StringToParse = "obj.Value == 'aa'", TypeRegistry = registry };
var ret = cc.Eval();
Assert.AreEqual(true, ret);

// okay
obj.Value = 10;
cc = new CompiledExpression() { StringToParse = "obj.Value == 10", TypeRegistry = registry };
ret = cc.Eval();
Assert.AreEqual(true, ret);

// fails
obj.Value = 10.0;
cc = new CompiledExpression() { StringToParse = "obj.Value == 10", TypeRegistry = registry };
ret = cc.Eval();
Assert.AreEqual(true, ret);

// fails
obj.Value = 10.0;
cc = new CompiledExpression() { StringToParse = "obj.Value = 5", TypeRegistry = registry };
ret = cc.Eval();
Assert.AreEqual(5, obj.Value);

// fails
obj.Value = 10;
cc = new CompiledExpression() { StringToParse = "obj.Value == 10.0", TypeRegistry = registry };
ret = cc.Eval();
Assert.AreEqual(true, ret);
}
}

public class MyClass
Expand Down Expand Up @@ -759,6 +807,7 @@ public class objHolder
public IEnumerable<string> iterator;
public IEnumerable objectIterator;
public string[] stringIterator;
public dynamic Value;
}

public enum NumEnum
Expand Down
1 change: 1 addition & 0 deletions Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using ExpressionEvaluator;

Expand Down

0 comments on commit 8acb0b9

Please sign in to comment.