Skip to content

Commit

Permalink
Support to expressions in bellvue and fixing float32 support
Browse files Browse the repository at this point in the history
  • Loading branch information
ElemarJR committed Oct 28, 2015
1 parent 1be325a commit 334a93a
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 135 deletions.
18 changes: 9 additions & 9 deletions demo/bellevue/Bellevue/Program.cs
@@ -1,10 +1,8 @@
using System;
using System.Globalization;
using System.IO;

using FluentIL;

using Bellevue.Parser;
using FluentIL;
using FluentIL.ExpressionParser;

namespace Bellevue
{
Expand Down Expand Up @@ -51,11 +49,11 @@ static void Main(string[] args)
}
else if (block.IsFormula)
{
ParseResult result;
var formula = ((Tokens.Formula) block).Item;
// TODO: Which version of write should it use?!
body
.Parse(formula)
.Write<int>();
.Parse(formula, out result)
.Write(result.ExpressionType);
}
}
body.Ret();
Expand All @@ -64,9 +62,11 @@ static void Main(string[] args)
assembly.Save();
}

static string Number()
public float Print(float input)
{
return (3.1415).ToString(CultureInfo.InvariantCulture);
return input/2;
}


}
}
4 changes: 3 additions & 1 deletion demo/bellevue/Bellevue/sample.txt
Expand Up @@ -2,6 +2,8 @@ Hello World! @*this is a comment*@

This source text @*this is a comment*@will be compiled into an executable.

Yeap. It works! @{2+2/2}
Int operation: @{2+2/2}
Int operation: @{5/2}
Float operation: @{7.0/2}

Bye!
9 changes: 8 additions & 1 deletion src/FluentIL/Emitters/DynamicMethodBody.cs
Expand Up @@ -39,13 +39,20 @@ public DynamicMethodBody Write(string message)
}

public DynamicMethodBody Write<T>()
{
return Write(typeof (T));
}

public DynamicMethodBody Write(Type t)
{
var minfo = typeof(Console).GetMethod(
"Write",
new[] { typeof(T) });
new[] { t });
return Call(minfo);
}



public object Invoke(params object[] args)
{
return _methodInfo.AsDynamicMethod.Invoke(null, args);
Expand Down
58 changes: 32 additions & 26 deletions src/FluentIL/Emitters/FluentHelpers/DynamicMethodBody.Expression.cs
@@ -1,26 +1,32 @@
using System.Linq.Expressions;
using FluentIL.ExpressionInterpreter;
using FluentIL.ExpressionParser;

// ReSharper disable CheckNamespace
namespace FluentIL.Emitters
// ReSharper restore CheckNamespace
{
partial class DynamicMethodBody
{
public DynamicMethodBody Parse(string expression)
{
Parser.Parse(expression, this);
return this;
}

public DynamicMethodBody Expression(Expression expression)
{
expression = new ExpressionSimplifierVisitor().Visit(expression);
new ILEmitterVisitor(this).Visit(
expression
);
return this;
}
}
}
using System.Linq.Expressions;
using FluentIL.ExpressionInterpreter;
using FluentIL.ExpressionParser;

// ReSharper disable CheckNamespace
namespace FluentIL.Emitters
// ReSharper restore CheckNamespace
{
partial class DynamicMethodBody
{
public DynamicMethodBody Parse(string expression)
{
Parser.Parse(expression, this);
return this;
}

public DynamicMethodBody Parse(string expression, out ParseResult result)
{
result = Parser.Parse(expression, this);
return this;
}

public DynamicMethodBody Expression(Expression expression)
{
expression = new ExpressionSimplifierVisitor().Visit(expression);
new ILEmitterVisitor(this).Visit(
expression
);
return this;
}
}
}
191 changes: 99 additions & 92 deletions src/FluentIL/Emitters/ILEmitter.cs
@@ -1,93 +1,100 @@
using System;
using System.Reflection;
using System.Reflection.Emit;

namespace FluentIL.Emitters
{
public abstract class ILEmitter
{
public void DeclareLocal(Type type)
{
OnDeclareLocal(type);
}

protected abstract void OnDeclareLocal(Type type);

public void MarkLabel(Label label)
{
OnMarkLabel(label);
}

protected abstract void OnMarkLabel(Label label);

public Label DefineLabel()
{
return OnDefineLabel();
}

protected abstract Label OnDefineLabel();

public void Emit(OpCode opcode)
{
OnEmit(opcode);
}

protected abstract void OnEmit(OpCode opcode);

public void Emit(OpCode opcode, string arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, string arg);

public void Emit(OpCode opcode, int arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, int arg);

public void Emit(OpCode opcode, double arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, double arg);

public void Emit(OpCode opcode, Label arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, Label arg);

public void Emit(OpCode opcode, MethodInfo arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, MethodInfo arg);

public void Emit(OpCode opcode, ConstructorInfo arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, ConstructorInfo arg);

public void Emit(OpCode opcode, FieldInfo arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, FieldInfo arg);

public void Emit(OpCode opcode, Type arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, Type arg);
}
using System;
using System.Reflection;
using System.Reflection.Emit;

namespace FluentIL.Emitters
{
public abstract class ILEmitter
{
public void DeclareLocal(Type type)
{
OnDeclareLocal(type);
}

protected abstract void OnDeclareLocal(Type type);

public void MarkLabel(Label label)
{
OnMarkLabel(label);
}

protected abstract void OnMarkLabel(Label label);

public Label DefineLabel()
{
return OnDefineLabel();
}

protected abstract Label OnDefineLabel();

public void Emit(OpCode opcode)
{
OnEmit(opcode);
}

protected abstract void OnEmit(OpCode opcode);

public void Emit(OpCode opcode, string arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, string arg);

public void Emit(OpCode opcode, int arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, int arg);

public void Emit(OpCode opcode, double arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, double arg);

public void Emit(OpCode opcode, float arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, float arg);

public void Emit(OpCode opcode, Label arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, Label arg);

public void Emit(OpCode opcode, MethodInfo arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, MethodInfo arg);

public void Emit(OpCode opcode, ConstructorInfo arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, ConstructorInfo arg);

public void Emit(OpCode opcode, FieldInfo arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, FieldInfo arg);

public void Emit(OpCode opcode, Type arg)
{
OnEmit(opcode, arg);
}

protected abstract void OnEmit(OpCode opcode, Type arg);
}
}
5 changes: 5 additions & 0 deletions src/FluentIL/Emitters/ReflectionILEmitter.cs
Expand Up @@ -27,6 +27,11 @@ protected override void OnEmit(OpCode opcode, int arg)
_ilGenerator.Emit(opcode, arg);
}

protected override void OnEmit(OpCode opcode, float arg)
{
_ilGenerator.Emit(opcode, arg);
}

protected override void OnEmit(OpCode opcode, double arg)
{
_ilGenerator.Emit(opcode, arg);
Expand Down
15 changes: 14 additions & 1 deletion src/FluentIL/Emitters/T4/DynamicMethodBody.Emit.cs
Expand Up @@ -58,7 +58,20 @@ public DynamicMethodBody Emit(OpCode opcode, int arg)
return this;
}

public DynamicMethodBody Emit(OpCode opcode, double arg)
public DynamicMethodBody Emit(OpCode opcode, float arg)
{
ExecutePreEmitActions();
#if DEBUG
Console.WriteLine("\t{0} {1}", opcode, arg);
#endif

_methodInfo.GetILEmitter()
.Emit(opcode, arg);

return this;
}

public DynamicMethodBody Emit(OpCode opcode, double arg)
{
ExecutePreEmitActions();
#if DEBUG
Expand Down
22 changes: 22 additions & 0 deletions src/FluentIL/ExpressionParser/ParseResult.cs
@@ -0,0 +1,22 @@
using System;

namespace FluentIL.ExpressionParser
{
public class ParseResult
{
public Type ExpressionType { get; internal set; }

internal void AnalyzeType(Type type)
{
var shouldUse =
(ExpressionType == null) ||
(type == typeof(bool)) ||
(type == typeof(double) && ExpressionType == typeof(int));

if (shouldUse)
{
ExpressionType = type;
}
}
}
}

0 comments on commit 334a93a

Please sign in to comment.