Skip to content

Commit

Permalink
Integrated Oc5Model and Oc5State in CompileVisitor and Oc5Runner
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalpfeil committed May 17, 2018
1 parent d89bb0a commit 1fd6bb8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 111 deletions.
8 changes: 4 additions & 4 deletions Source/SafetyLustre.Oc5Compiler.Tests/Oc5StateTests.cs
Expand Up @@ -23,10 +23,10 @@ public void Example1Test()
Doubles = new List<double>(),
Mappings = new List<PositionInOc5State>
{
new PositionInOc5State {Type=PredefinedObjects.Types._boolean, IndexInOc5State=0 },
new PositionInOc5State {Type=PredefinedObjects.Types._integer, IndexInOc5State=0 },
new PositionInOc5State {Type=PredefinedObjects.Types._integer, IndexInOc5State=1 },
new PositionInOc5State {Type=PredefinedObjects.Types._integer, IndexInOc5State=2 },
new PositionInOc5State {Type=PredefinedObjects.Types._boolean, IndexInOc5StateList=0 },
new PositionInOc5State {Type=PredefinedObjects.Types._integer, IndexInOc5StateList=0 },
new PositionInOc5State {Type=PredefinedObjects.Types._integer, IndexInOc5StateList=1 },
new PositionInOc5State {Type=PredefinedObjects.Types._integer, IndexInOc5StateList=2 },
}
};

Expand Down
50 changes: 0 additions & 50 deletions Source/SafetyLustre.Oc5Compiler.Tests/Oc5Tests.cs
Expand Up @@ -185,55 +185,5 @@ public void TestObjectVariableAccess()
Expression.Lambda<Func<int>>(ex).Compile().Invoke();
Console.WriteLine(myObj.MyProperty);
}

[TestMethod]
public void TestDynamicList()
{
var dynList = new List<dynamic> { 1, 1.5, "hello", new MyClass() };
foreach (var item in dynList)
{
int i = item;
Console.WriteLine($"{item} is {item.GetType()}");
}

Console.WriteLine("-----------");

var objList = new List<object> { 1, 1.5, "hello", new MyClass() };
foreach (var item in objList)
{
Console.WriteLine($"{item} is {item.GetType()}");
}

}

[TestMethod]
public void MiscTest()
{

}

static object DynamicCast(object source, Type type)
{
var parameter = Expression.Parameter(typeof(object), "input");

var cast = Expression.TypeAs(Expression.Convert(parameter, type), typeof(object));

var lambda = Expression.Lambda<Func<object, object>>(cast, parameter);

var func = lambda.Compile();

return func(source);
}
}

public class count
{
bool var0;
int var1;
/* ... */

int currentState;


}
}
2 changes: 1 addition & 1 deletion Source/SafetyLustre.Oc5Compiler/Oc5Model.cs
Expand Up @@ -8,7 +8,7 @@ class Oc5Model
{
public List<ConstantExpression> Constants { get; set; } = new List<ConstantExpression>();
public List<Signal> Signals { get; set; } = new List<Signal>();
public List<ParameterExpression> Variables { get; set; } = new List<ParameterExpression>();
public List<Expression> Variables { get; set; } = new List<Expression>();
public List<Expression> Actions { get; set; } = new List<Expression>();
public List<Expression> States { get; set; } = new List<Expression>();
}
Expand Down
42 changes: 23 additions & 19 deletions Source/SafetyLustre.Oc5Compiler/Oc5Runner.cs
Expand Up @@ -16,6 +16,10 @@ public class Oc5Runner
private Oc5Model Oc5Model { get; set; }
private Oc5State Oc5State { get; set; }



private ParameterExpression Oc5StateParameterExpression { get; set; }

/// <summary>
/// Creates an instance of Oc5Runner
/// </summary>
Expand All @@ -34,32 +38,32 @@ public Oc5Runner(string oc5Source)
var visitor = new CompileVisitor(ocfileContext);
Oc5Model = visitor.Oc5Model;
Oc5State = visitor.Oc5State;
Oc5StateParameterExpression = visitor.Oc5StateParameterExpression;
Console.WriteLine();
}

public void Tick<T1, T2>(T1 input1, T2 input2)
public void Tick(params object[] inputs)
{
int i = -1;
foreach (var signal in Oc5Model.Signals.OfType<SingleInputSignal>())
{
i++;
var index = signal.VarIndex;
}
//int i = -1;
//foreach (var signal in Oc5Model.Signals.OfType<SingleInputSignal>())
//{
// i++;
// var index = signal.VarIndex;
//}

//HACK
Oc5State.Ints[0] = 1;
Oc5State.Ints[1] = 2;

var state = Oc5Model.States[Oc5State.CurrentState];

switch (Oc5Model.Variables.Count)
{
case 4:
var arg1 = false;
var arg2 = input1;
var arg3 = input2;
var arg4 = 0;
Oc5State.CurrentState = CompileAndInvoke(state, Oc5Model.Variables, ref arg1, ref arg2, ref arg3, ref arg4);
break;
default:
throw new NotImplementedException();
}
var le = Expression.Lambda<Func<Oc5State, int>>(state, Oc5StateParameterExpression);
var compiled = le.Compile();
var result = compiled.Invoke(Oc5State);

Console.WriteLine($"result: {result}");
Console.WriteLine($"output: {Oc5State.Ints[2]}");
Console.ReadKey();
}

#region Delegates
Expand Down
2 changes: 1 addition & 1 deletion Source/SafetyLustre.Oc5Compiler/Oc5State.cs
Expand Up @@ -28,6 +28,6 @@ class Oc5State
struct PositionInOc5State
{
public PredefinedObjects.Types Type { get; set; }
public int IndexInOc5State { get; set; }
public int IndexInOc5StateList { get; set; }
}
}
53 changes: 21 additions & 32 deletions Source/SafetyLustre.Oc5Compiler/PredefinedObjects.cs
Expand Up @@ -81,52 +81,41 @@ internal static ConstantExpression GetConstantExpressionOfType(int typeIndex)

#region Variables

internal static ParameterExpression GetVariableExpression(int typeIndex)
{
//TODO statt parametern einen listenzugriff zurückgeben
switch ((Types)typeIndex)
{
case Types._boolean:
return Expression.Parameter(typeof(bool).MakeByRefType());
case Types._integer:
return Expression.Parameter(typeof(int).MakeByRefType());
case Types._string:
return Expression.Parameter(typeof(string).MakeByRefType());
case Types._float:
return Expression.Parameter(typeof(float).MakeByRefType());
case Types._double:
return Expression.Parameter(typeof(double).MakeByRefType());
default:
throw new ArgumentException($"No predefined type with index {typeIndex}!");
}
}

internal static void AddVariableToOc5State(Oc5State oc5State, int typeIndex)
internal static Expression GetVariableExpression(int typeIndex, Oc5State oc5State, ParameterExpression oc5StateParameterExpression)
{
var type = (Types)typeIndex;
//TODO mappings
switch (type)
{
case Types._boolean:
oc5State.Bools.Add(default(bool));
oc5State.Mappings.Add(new PositionInOc5State { Type = type, IndexInOc5State = oc5State.Bools.Count - 1 });
break;
var boolIndex = oc5State.Bools.Count - 1;
oc5State.Mappings.Add(new PositionInOc5State { Type = type, IndexInOc5StateList = boolIndex });
var boolsExpression = Expression.Property(oc5StateParameterExpression, "Bools");
return Expression.Property(boolsExpression, "Item", Expression.Constant(boolIndex));
case Types._integer:
oc5State.Ints.Add(default(int));
oc5State.Mappings.Add(new PositionInOc5State { Type = type, IndexInOc5State = oc5State.Ints.Count - 1 });
break;
var intIndex = oc5State.Ints.Count - 1;
oc5State.Mappings.Add(new PositionInOc5State { Type = type, IndexInOc5StateList = intIndex });
var intsExpression = Expression.Property(oc5StateParameterExpression, "Ints");
return Expression.Property(intsExpression, "Item", Expression.Constant(intIndex));
case Types._string:
oc5State.Strings.Add(default(string));
oc5State.Mappings.Add(new PositionInOc5State { Type = type, IndexInOc5State = oc5State.Strings.Count - 1 });
break;
var stringIndex = oc5State.Strings.Count - 1;
oc5State.Mappings.Add(new PositionInOc5State { Type = type, IndexInOc5StateList = stringIndex });
var stringsExpression = Expression.Property(oc5StateParameterExpression, "Strings");
return Expression.Property(stringsExpression, "Item", Expression.Constant(stringIndex));
case Types._float:
oc5State.Floats.Add(default(float));
oc5State.Mappings.Add(new PositionInOc5State { Type = type, IndexInOc5State = oc5State.Floats.Count - 1 });
break;
var floatIndex = oc5State.Floats.Count - 1;
oc5State.Mappings.Add(new PositionInOc5State { Type = type, IndexInOc5StateList = floatIndex });
var floatsExpression = Expression.Property(oc5StateParameterExpression, "Floats");
return Expression.Property(floatsExpression, "Item", Expression.Constant(floatIndex));
case Types._double:
oc5State.Doubles.Add(default(double));
oc5State.Mappings.Add(new PositionInOc5State { Type = type, IndexInOc5State = oc5State.Doubles.Count - 1 });
break;
var doubleIndex = oc5State.Doubles.Count - 1;
oc5State.Mappings.Add(new PositionInOc5State { Type = type, IndexInOc5StateList = doubleIndex });
var doublesExpression = Expression.Property(oc5StateParameterExpression, "Doubles");
return Expression.Property(doublesExpression, "Item", Expression.Constant(doubleIndex));
default:
throw new ArgumentException($"No predefined type with index {typeIndex}!");
}
Expand Down
7 changes: 3 additions & 4 deletions Source/SafetyLustre.Oc5Compiler/Visitors/CompileVisitor.cs
Expand Up @@ -10,9 +10,10 @@ namespace SafetyLustre.Oc5Compiler.Visitors
{
class CompileVisitor : Oc5BaseVisitor<Expression>
{
private LabelTarget CurrentReturnLabelTarget { get; set; }
public Oc5Model Oc5Model { get; set; } = new Oc5Model();
public Oc5State Oc5State { get; set; } = new Oc5State();
private LabelTarget CurrentReturnLabelTarget { get; set; }
public ParameterExpression Oc5StateParameterExpression { get; set; } = Expression.Parameter(typeof(Oc5State), "Oc5State");

public CompileVisitor([NotNull] OcfileContext context)
{
Expand Down Expand Up @@ -120,11 +121,9 @@ public override Expression VisitVariable([NotNull] VariableContext context)
throw new UnsupportedSyntaxException("User-defined index", context.index().Start);

var predefinedTypeIndex = GetIndexNumber(context.index());
var variableExpression = PredefinedObjects.GetVariableExpression(predefinedTypeIndex);
var variableExpression = PredefinedObjects.GetVariableExpression(predefinedTypeIndex, Oc5State, Oc5StateParameterExpression);
Oc5Model.Variables.Add(variableExpression);

PredefinedObjects.AddVariableToOc5State(Oc5State, predefinedTypeIndex);

return null;
}

Expand Down

0 comments on commit 1fd6bb8

Please sign in to comment.