Skip to content

Commit

Permalink
Fake Dynamics
Browse files Browse the repository at this point in the history
  • Loading branch information
RupertAvery committed Aug 6, 2014
1 parent e08d9b4 commit 9edc9f2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
23 changes: 22 additions & 1 deletion Parser/ExprEval.g3
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,28 @@ public primary_expression returns [Expression value]
input.Rewind(savepoint);
fakedot = true;

$value = GetPrimaryExpressionPart(primary_expression_part(), input, $value, method);
$value = GetPrimaryExpressionPart(primary_expression_part(), input, $value, method, false);

if($value == null)
{
$value = Scope;

var text = $primary_expression_start.text;

var method1 = new TypeOrGeneric() { Identifier = "getVar" };
var args1 = new List<Expression>() { Expression.Constant(text, typeof(string)) };

var pe = (ParameterExpression)$value;

var type1 = GetPropertyType(Scope.Type, text);

$value = Expression.Convert(ExpressionHelper.GetMethod($value, method1, args1, false), type1);

if($value == null)
{
throw new ExpressionParseException(string.Format("Cannot resolve symbol \"{0}\"", input.LT(-1).Text), input);
}
}
}
else
{
Expand Down
8 changes: 8 additions & 0 deletions Parser/ExprEval.g3.parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
using Antlr.Runtime;
using ExpressionEvaluator;
using ExpressionEvaluator.Parser.Expressions;
Expand Down Expand Up @@ -30,6 +31,13 @@ public partial class ExprEvalParser
// Debug.WriteLine("Out: {0} {1}", ruleName, ruleIndex);
//}

protected Type GetPropertyType(object instance, string propertyName)
{
var methodInfo = instance.GetType().GetMethod("getType", BindingFlags.Instance);
return (Type)methodInfo.Invoke(instance, new object[] { propertyName });
}


protected Expression GetPrimaryExpressionPart(PrimaryExpressionPart primary_expression_part2, ITokenStream input, Expression value, TypeOrGeneric method, bool throwsException = true)
{
if (primary_expression_part2.GetType() == typeof(AccessIdentifier))
Expand Down
12 changes: 12 additions & 0 deletions Parser/ExpressionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ public static Expression Assign(Expression le, Expression re)
var type = le.Type;
var isDynamic = type.IsDynamicOrObject();

if (le.NodeType == ExpressionType.Call)
{
var mc = (MethodCallExpression) le;
if (mc.Method.Name == "getVar")
{
var ce = (ConstantExpression) mc.Arguments[0];
var method1 = new TypeOrGeneric() { Identifier = "setVar" };
var args1 = new List<Expression>() { Expression.Constant(ce.Value, typeof(string)), Expression.Convert(re, typeof(object)) };
return GetMethod(mc.Object, method1, args1, true);
}
}

if (type.IsDynamic() || le.NodeType == ExpressionType.Dynamic)
{
var dle = (DynamicExpression)le;
Expand Down
14 changes: 12 additions & 2 deletions Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ public class Super
{
public int x { get; set; }
public object DataContext { get; set; }

public object getVar(string name)
{
return 1;
}

public void setVar(string name, object value)
{
var x = value;
}
}

public class Sub
Expand All @@ -167,11 +177,11 @@ static void Main(string[] args)
var tt = new Super() { DataContext = sobj, x = 2 };


var ee = new CompiledExpression<int>() { StringToParse = "x + y" };
var ee = new CompiledExpression<int>() { StringToParse = "z = (int)z + 1" };
ee.SubScope = "DataContext";
ee.SubScopeType = sobj.GetType();

var ff = ee.ScopeCompile<Super>()(tt);
ee.ScopeCompileCall<Super>()(tt);



Expand Down

0 comments on commit 9edc9f2

Please sign in to comment.