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
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using System.Linq.Expressions;
using GraphClimber.ExpressionCompiler;
using Mono.Linq.Expressions;

namespace GraphClimber.Debug.ExpressionCompiler
{
public class CSharpExpressionDescriber : IExpressionDescriber
{
public static readonly IExpressionDescriber Empty = new CSharpExpressionDescriber();

private CSharpExpressionDescriber()
{

}

public string Describe(Expression expression)
{
var stringWriter = new StringWriter();
var csharpWriter = new CSharpWriter(new TextFormatter(stringWriter));

csharpWriter.Write(expression);

return stringWriter.ToString();
}
}
}
65 changes: 65 additions & 0 deletions src/GraphClimber.Debug/GraphClimber.Debug.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{B003B6BA-6A13-403E-8C1E-D26AE60C58A0}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GraphClimber.Debug</RootNamespace>
<AssemblyName>GraphClimber.Debug</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Mono.Linq.Expressions">
<HintPath>..\packages\Mono.Linq.Expressions.1.2.0.0\lib\Mono.Linq.Expressions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ExpressionCompiler\CSharpExpressionDescriber.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GraphClimber\GraphClimber.csproj">
<Project>{f7fa4635-c6c6-4d82-b344-b737438e1651}</Project>
<Name>GraphClimber</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
36 changes: 36 additions & 0 deletions src/GraphClimber.Debug/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("GraphClimber.Debug")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("GraphClimber.Debug")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("8e268eed-bd81-4354-be31-618dd525e6fa")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
4 changes: 4 additions & 0 deletions src/GraphClimber.Debug/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Mono.Linq.Expressions" version="1.2.0.0" targetFramework="net45" />
</packages>
10 changes: 8 additions & 2 deletions src/GraphClimber.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.22310.1
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GraphClimber", "GraphClimber\GraphClimber.csproj", "{F7FA4635-C6C6-4D82-B344-B737438E1651}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GraphClimber.Tests", "GraphClimber.Tests\GraphClimber.Tests.csproj", "{99B3FD79-4D4B-4536-9F6B-2F53E86F8603}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GraphClimber.Debug", "GraphClimber.Debug\GraphClimber.Debug.csproj", "{B003B6BA-6A13-403E-8C1E-D26AE60C58A0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{99B3FD79-4D4B-4536-9F6B-2F53E86F8603}.Debug|Any CPU.Build.0 = Debug|Any CPU
{99B3FD79-4D4B-4536-9F6B-2F53E86F8603}.Release|Any CPU.ActiveCfg = Release|Any CPU
{99B3FD79-4D4B-4536-9F6B-2F53E86F8603}.Release|Any CPU.Build.0 = Release|Any CPU
{B003B6BA-6A13-403E-8C1E-D26AE60C58A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B003B6BA-6A13-403E-8C1E-D26AE60C58A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B003B6BA-6A13-403E-8C1E-D26AE60C58A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B003B6BA-6A13-403E-8C1E-D26AE60C58A0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
33 changes: 18 additions & 15 deletions src/GraphClimber/ExpressionCompiler/AccessPrivateFieldVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using GraphClimber.ExpressionCompiler.Extensions;

namespace GraphClimber.ExpressionCompiler
{
Expand Down Expand Up @@ -93,21 +94,21 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
if (!node.Method.IsPublic)
{
// In case of static methods, instance should be null.
var reflectionInstance = node.Object ?? Expression.Constant(null);
var reflectionInstance = node.Object ?? ExpressionExtensions.Null;

// In case of generic method,
// we create an array of the assembly qualified names of the generic arguments.
var genericArgumentTypes = node.Method.IsGenericMethod
? (Expression)Expression.NewArrayInit(typeof(string), node.Method.GetGenericArguments().Select(t => Expression.Constant(t.AssemblyQualifiedName)))
: Expression.Constant(null);
? (Expression)Expression.NewArrayInit(typeof(string), node.Method.GetGenericArguments().Select(t => t.AssemblyQualifiedName.Constant()))
: ExpressionExtensions.Null;

return
Expression.Convert(Expression.Call(null, typeof(AccessPrivateFieldVisitor).GetMethod("CallMethod"),
Expression.Constant(node.Method.DeclaringType.AssemblyQualifiedName),
Expression.Constant(node.Method.Name),
Expression.Call(null, typeof(AccessPrivateFieldVisitor).GetMethod("CallMethod"),
node.Method.DeclaringType.AssemblyQualifiedName.Constant(),
node.Method.Name.Constant(),
genericArgumentTypes,
reflectionInstance,
Expression.NewArrayInit(typeof(object), node.Arguments)), node.Type);
Expression.NewArrayInit(typeof(object), node.Arguments)).Convert(node.Type);
}

return base.VisitMethodCall(node);
Expand Down Expand Up @@ -140,12 +141,13 @@ private static Expression VisitFieldMemberExpression(MemberExpression node, Fiel

if (isPrivateField)
{
var argument = node.Expression ?? Expression.Constant(null);
var argument = node.Expression ?? ExpressionExtensions.Null;

return Expression.Convert(Expression.Call(null,
return Expression.Call(null,
typeof (AccessPrivateFieldVisitor).GetMethod("GetFieldValue"),
Expression.Constant(fieldInfo.DeclaringType.AssemblyQualifiedName),
Expression.Constant(fieldInfo.Name), argument), node.Type);
fieldInfo.DeclaringType.AssemblyQualifiedName.Constant(),
fieldInfo.Name.Constant(),
argument).Convert(node.Type);
}

return node;
Expand All @@ -160,12 +162,13 @@ private static Expression VisitPropertyMemberExpression(MemberExpression node, P

if (isPrivate)
{
var argument = node.Expression ?? Expression.Constant(null);
var argument = node.Expression ?? ExpressionExtensions.Null;

return Expression.Convert(Expression.Call(null,
return Expression.Call(null,
typeof (AccessPrivateFieldVisitor).GetMethod("GetPropertyValue"),
Expression.Constant(propertyInfo.DeclaringType.AssemblyQualifiedName),
Expression.Constant(propertyInfo.Name), argument), node.Type);
propertyInfo.DeclaringType.AssemblyQualifiedName.Constant(),
propertyInfo.Name.Constant(),
argument).Convert(node.Type);
}

return node;
Expand Down
8 changes: 5 additions & 3 deletions src/GraphClimber/ExpressionCompiler/DebugExpressionVistor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ public override Expression Visit(Expression node)
return node;
}

Expression innerExpression = base.Visit(node);

// Unsymboled is not symboled, the node children are.
if (ContainsType(_unsymboledExpressionTypes, node))
{
return innerExpression;
return base.Visit(node);
}

Range<Position> range = GetCurrentExpressionRange(node);
Expand All @@ -62,6 +61,8 @@ public override Expression Visit(Expression node)
range.End.Line + 1,
range.End.Column + 1);

Expression innerExpression = base.Visit(node);

return Expression.Block(debugInfoExpression, innerExpression);
}

Expand All @@ -71,7 +72,8 @@ private Range<Position> GetCurrentExpressionRange(Expression node)
string[] debugViewLines = debugView.Split(_newLineSeperator, StringSplitOptions.RemoveEmptyEntries);
string firstDebugViewLine = debugViewLines.First();

_currentIndex = _initialDebugView.IndexOf(firstDebugViewLine, _currentIndex, StringComparison.Ordinal);
var newIndex = _initialDebugView.IndexOf(firstDebugViewLine, _currentIndex, StringComparison.Ordinal);
_currentIndex = newIndex;

Position start = _initialDebugView.GetPosition(_currentIndex);
int spacing = _initialDebugView.Split(_newLineSeperator, StringSplitOptions.RemoveEmptyEntries)[start.Line].IndexOfNot(' ');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Linq.Expressions;

namespace GraphClimber.ExpressionCompiler.Extensions
{
/// <summary>
/// Provides extension methods to create
/// expressions easier.
/// </summary>
public static class ExpressionExtensions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this class internal, as it spams the intellisense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done .

{

/// <summary>
/// Expression of Null.
/// </summary>
public static readonly ConstantExpression Null = Expression.Constant(null);

/// <summary>
/// Creates an <see cref="ConstantExpression"/>
/// with the given value
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static Expression Constant<T>(this T value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, perhaps, you can make this method internal. This one really spam the intellisense.

{
return Expression.Constant(value, typeof (T));
}

/// <summary>
/// Converts the expression value to the given type
/// <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public static Expression Convert<T>(this Expression expression)
{
return expression.Convert(typeof (T));
}

/// <summary>
/// Converts the expression value to the given
/// <param name="newType"></param>
/// </summary>
/// <param name="expression"></param>
/// <param name="newType"></param>
/// <returns></returns>
public static Expression Convert(this Expression expression, Type newType)
{
return Expression.Convert(expression, newType);
}

}
}
1 change: 1 addition & 0 deletions src/GraphClimber/GraphClimber.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Compile Include="ExpressionCompiler\DebugExpressionCompiler.cs" />
<Compile Include="ExpressionCompiler\DebugExpressionVistor.cs" />
<Compile Include="ExpressionCompiler\DebugViewExpressionDescriber.cs" />
<Compile Include="ExpressionCompiler\Extensions\ExpressionExtensions.cs" />
<Compile Include="ExpressionCompiler\IExpressionCompiler.cs" />
<Compile Include="ExpressionCompiler\IExpressionDescriber.cs" />
<Compile Include="ExpressionCompiler\StringExtensions.cs" />
Expand Down
10 changes: 6 additions & 4 deletions src/GraphClimber/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Xml.Linq;
using GraphClimber.Examples;
using GraphClimber.ExpressionCompiler;
using GraphClimber.ExpressionCompiler.Extensions;
using GraphClimber.ValueDescriptor;

namespace GraphClimber
Expand Down Expand Up @@ -57,11 +58,12 @@ public static void Play()
var variable = Expression.Variable(typeof(int), "myInt");
var @break = Expression.Label();
Expression exp = Expression.Assign(variable, Expression.Divide(propertyOrField, Expression.Subtract(property, method)));
exp = Expression.Block(new[] { variable }, exp, Expression.Add(Expression.Constant(2), Expression.Constant(5)), Expression.Call(typeof(Console).GetMethod("WriteLine", new[] { typeof(string) }), Expression.Constant("Hello Worlda")),
Expression.Goto(@break));
exp = Expression.Block(new[] { variable }, exp, Expression.Add(2.Constant(), 5.Constant()), Expression.Call(typeof(Console).GetMethod("WriteLine", new[] { typeof(string) }), Expression.Constant("Hello Worlda")));


exp = Expression.Loop(exp, @break);
var iVariable = Expression.Variable(typeof(int), "i");
// exp = CustomExpression.For(iVariable, 0.Constant(), Expression.LessThan(iVariable, 5.Constant()), Expression.PostIncrementAssign(iVariable), exp);

exp = Expression.Block(exp,
Expression.Call(typeof(Console).GetMethod("WriteLine", new[] { typeof(string) }),
Expression.Constant("Goodbye")));
Expand All @@ -88,7 +90,7 @@ public static void DoSomething<T>(StrongBox<T> hello)

static void Main(string[] args)
{
// ExpressionDebugGames.Play();
ExpressionDebugGames.Play();
//IStore store = new TrivialStore();

//store.Set("A", 5);
Expand Down
Loading