public override void Compile(Emitter.Emitter emitter) { var leftType = Left.GetExpressionType(emitter); var rightType = Right.GetExpressionType(emitter); var type = GetExpressionType(emitter); // subtract matrices if(type == "matrix") { Left.Compile(emitter); Right.Compile(emitter); var matrixType = typeof(MN.Matrix<double>); var method = emitter.AssemblyImport(matrixType.GetMethod("Subtract", new [] { matrixType } )); emitter.EmitCall(method); } // subtract dicts else if (type == "dict") { Left.Compile(emitter); Right.Compile(emitter); var dictType = typeof(Dict); var method = emitter.AssemblyImport(dictType.GetMethod("Subtract", new[] { dictType })); emitter.EmitCall(method); } // subtract complex numbers else if (type == "complex") { Left.Compile(emitter); if (leftType != "complex") { emitter.EmitUpcastBasicType(leftType, "float"); emitter.EmitLoadFloat(0); emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float")); } Right.Compile(emitter); if (rightType != "complex") { emitter.EmitUpcastBasicType(rightType, "float"); emitter.EmitLoadFloat(0); emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float")); } emitter.EmitCall(emitter.AssemblyImport(typeof(SN.Complex).GetMethod("op_Subtraction", new[] { typeof(SN.Complex), typeof(SN.Complex) }))); } // add floating point numbers or integers else if (type.IsAnyOf("int", "float")) { Left.Compile(emitter); emitter.EmitUpcastBasicType(leftType, type); Right.Compile(emitter); emitter.EmitUpcastBasicType(rightType, type); emitter.EmitSub(); } }
public override void Compile(Emitter.Emitter emitter) { var leftType = Left.GetExpressionType(emitter); var rightType = Right.GetExpressionType(emitter); var type = GetExpressionType(emitter); // divide matrix by a number if(type == "matrix") { Left.Compile(emitter); // division is broken, therefore multiply by an inverse value emitter.EmitLoadFloat(1); Right.Compile(emitter); if (rightType != "float") emitter.EmitConvertToFloat(); emitter.EmitDiv(); var matrixType = typeof(MN.Matrix<double>); var method = emitter.AssemblyImport(matrixType.GetMethod("Multiply", new[] { typeof(double) })); emitter.EmitCall(method); } // divide complex numbers else if (type == "complex") { Left.Compile(emitter); if (leftType != "complex") { emitter.EmitUpcastBasicType(leftType, "float"); emitter.EmitLoadFloat(0); emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float")); } Right.Compile(emitter); if (rightType != "complex") { emitter.EmitUpcastBasicType(rightType, "float"); emitter.EmitLoadFloat(0); emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float")); } emitter.EmitCall(emitter.AssemblyImport(typeof(SN.Complex).GetMethod("op_Division", new[] { typeof(SN.Complex), typeof(SN.Complex) }))); } // divide floating point numbers or integers else if (type.IsAnyOf("int", "float")) { Left.Compile(emitter); emitter.EmitUpcastBasicType(leftType, type); Right.Compile(emitter); emitter.EmitUpcastBasicType(rightType, type); emitter.EmitDiv(); } }
public override void Compile(Emitter.Emitter emitter) { // declare variables and methods var tmpVar = emitter.CurrentMethod.Scope.Introduce("dict", emitter.ResolveType("dict")); var ctor = emitter.AssemblyImport(typeof(MirelleStdlib.Dict).GetConstructor(new Type[] { })); var set = emitter.FindMethod("dict", "set", "string", "string"); // var tmp = new dict emitter.EmitNewObj(ctor); emitter.EmitSaveVariable(tmpVar); // tmp[key] = value foreach(var curr in Data) { var keyType = curr.Item1.GetExpressionType(emitter); var valueType = curr.Item2.GetExpressionType(emitter); if (keyType != "string") Error(Resources.errDictItemTypeMismatch, curr.Item1.Lexem); if (valueType != "string") Error(Resources.errDictItemTypeMismatch, curr.Item2.Lexem); emitter.EmitLoadVariable(tmpVar); curr.Item1.Compile(emitter); curr.Item2.Compile(emitter); emitter.EmitCall(set); } emitter.EmitLoadVariable(tmpVar); }
public override void Compile(Emitter.Emitter emitter) { var fromType = From.GetExpressionType(emitter); var toType = To.GetExpressionType(emitter); if (fromType != "int" || toType != "int") Error(Resources.errRangeLimitsIntExpected); From.Compile(emitter); To.Compile(emitter); emitter.EmitNewObj(emitter.FindMethod("range", ".ctor", "int", "int")); }
public override void Compile(Emitter.Emitter emitter) { var type = Expression.GetExpressionType(emitter); if (type.IsAnyOf("int", "float", "complex")) { if(type == "complex") { // simple case: the complex is a constant if(Expression is ComplexNode) { (Expression as ComplexNode).Imaginary *= -1; Expression.Compile(emitter); } // complex case: complex is an expression result else { Expression.Compile(emitter); // tmp = ... var tmpVar = emitter.CurrentMethod.Scope.Introduce("complex", emitter.FindType("complex").Type); emitter.EmitSaveVariable(tmpVar); // tmp.real emitter.EmitLoadVariableAddress(tmpVar); emitter.EmitCall(emitter.AssemblyImport(typeof(SN.Complex).GetMethod("get_Real"))); // tmp.imaginary * -1 emitter.EmitLoadVariableAddress(tmpVar); emitter.EmitCall(emitter.AssemblyImport(typeof(SN.Complex).GetMethod("get_Imaginary"))); emitter.EmitLoadFloat(-1); emitter.EmitMul(); // new complex emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float")); } } else { // multiply by -1 Expression.Compile(emitter); if (type == "int") emitter.EmitLoadInt(-1); else emitter.EmitLoadFloat(-1); emitter.EmitMul(); } } else Error(String.Format(Resources.errInvertType, type)); }
public override void Compile(Emitter.Emitter emitter) { // retrieve matrix dimensions var height = MatrixItems.Count; var width = MatrixItems[0].Count; // retrieve info about matrices var matrixType = typeof(MN.DenseMatrix); var matrixCtor = emitter.AssemblyImport(matrixType.GetConstructor(new[] { typeof(int), typeof(int) })); var matrixSet = emitter.AssemblyImport(matrixType.GetMethod("At", new[] { typeof(int), typeof(int), typeof(double) })); var tmpVar = emitter.CurrentMethod.Scope.Introduce("matrix", emitter.AssemblyImport(matrixType)); // create matrix emitter.EmitLoadInt(height); emitter.EmitLoadInt(width); emitter.EmitNewObj(matrixCtor); emitter.EmitSaveVariable(tmpVar); // set items for(var idx1 = 0; idx1 < MatrixItems.Count; idx1++) { // ensure all lines have the same number of items if (idx1 > 0 && MatrixItems[0].Count != MatrixItems[idx1].Count) Error(String.Format(Resources.errMatrixLineLengthMismatch, MatrixItems[0].Count, idx1+1, MatrixItems[idx1].Count)); for(var idx2 = 0; idx2 < MatrixItems[idx1].Count; idx2++) { var item = MatrixItems[idx1][idx2]; var itemType = item.GetExpressionType(emitter); if (!itemType.IsAnyOf("int", "float")) Error(Resources.errMatrixItemTypeMismatch, item.Lexem); emitter.EmitLoadVariable(tmpVar); emitter.EmitLoadInt(idx1); emitter.EmitLoadInt(idx2); item.Compile(emitter); if (itemType != "float") emitter.EmitConvertToFloat(); emitter.EmitCall(matrixSet); } } emitter.EmitLoadVariable(tmpVar); }
public override void Compile(Emitter.Emitter emitter) { // check if constructor exists TypeNode type = null; try { type = emitter.FindType(Name); } catch { Error(String.Format(Resources.errTypeNotFound, Name)); } // check if type is an enum? if (type.Enum) { // the constructor in an enum is pseudo-private // so that it can only be called from another method // of the same enum type if(emitter.CurrentMethod == null || emitter.CurrentMethod.Owner != type) Error(String.Format(Resources.errConstructEnum, Name)); } MethodNode method = null; var signature = GetSignature(emitter); try { method = emitter.FindMethod(Name, ".ctor", signature); } catch { Error(String.Format(Resources.errConstructorNotFound, Name, signature.Join(" "))); } // parameters for (int idx = 0; idx < Parameters.Count; idx++) { Parameters[idx].Compile(emitter); emitter.EmitUpcastBasicType(Parameters[idx].GetExpressionType(emitter), method.Parameters[idx].Type.Signature); } emitter.EmitNewObj(method); }
public override void Compile(Emitter.Emitter emitter) { emitter.EmitLoadFloat(Real); emitter.EmitLoadFloat(Imaginary); emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float")); }
/// <summary> /// Generate code to setup the currently defined emitter /// </summary> /// <param name="emitter"></param> private void CompileInitiation(Emitter.Emitter emitter) { var flowSimType = emitter.FindType(PlannerID); var tmpVar = emitter.CurrentMethod.Scope.Introduce(PlannerID, flowSimType.Type); // tmp = new flowsimN() emitter.EmitNewObj(emitter.FindMethod(PlannerID, ".ctor")); emitter.EmitSaveVariable(tmpVar); SaveClosuredVariables(emitter, tmpVar); // call FlowSimulation.Start(planner) emitter.EmitLoadVariable(tmpVar); var method = emitter.AssemblyImport(typeof(FlowSimulation).GetMethod("Start", new[] { typeof(Planner) })); emitter.EmitCall(method); }
public override void Compile(Emitter.Emitter emitter) { var leftType = Left.GetExpressionType(emitter); var rightType = Right.GetExpressionType(emitter); var type = GetExpressionType(emitter); // concat strings if (type == "string") { Left.Compile(emitter); Right.Compile(emitter); emitter.EmitCall(emitter.FindMethod("string", "concat", "string", "string")); } // add matrices else if(type == "matrix") { Left.Compile(emitter); Right.Compile(emitter); var matrixType = typeof(MN.Matrix<double>); var method = emitter.AssemblyImport(matrixType.GetMethod("Add", new [] { matrixType } )); emitter.EmitCall(method); } // add dicts else if (type == "dict") { Left.Compile(emitter); Right.Compile(emitter); var dictType = typeof(Dict); var method = emitter.AssemblyImport(dictType.GetMethod("Add", new[] { dictType })); emitter.EmitCall(method); } // add complex numbers else if(type == "complex") { Left.Compile(emitter); if(leftType != "complex") { emitter.EmitUpcastBasicType(leftType, "float"); emitter.EmitLoadFloat(0); emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float")); } Right.Compile(emitter); if (rightType != "complex") { emitter.EmitUpcastBasicType(rightType, "float"); emitter.EmitLoadFloat(0); emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float")); } emitter.EmitCall(emitter.AssemblyImport(typeof(SN.Complex).GetMethod("op_Addition", new[] { typeof(SN.Complex), typeof(SN.Complex) }))); } // add floating point numbers or integers else if(type.IsAnyOf("int", "float")) { Left.Compile(emitter); emitter.EmitUpcastBasicType(leftType, type); Right.Compile(emitter); emitter.EmitUpcastBasicType(rightType, type); emitter.EmitAdd(); } // array addition else if(type.Contains("[]")) { Left.Compile(emitter); Right.Compile(emitter); var method = emitter.AssemblyImport(typeof(ArrayHelper).GetMethod("AddArrays", new[] { typeof(object), typeof(object) } )); emitter.EmitCall(method); } }
/// <summary> /// Generate code to setup the currently defined emitter /// </summary> /// <param name="emitter"></param> private void CompileInitiation(Emitter.Emitter emitter) { var emitterType = emitter.FindType(EmitterID); var tmpVar = emitter.CurrentMethod.Scope.Introduce(EmitterID, emitterType.Type); // tmp = new emitterN() emitter.EmitNewObj(emitter.FindMethod(EmitterID, ".ctor")); emitter.EmitSaveVariable(tmpVar); // step if (Step != null) { // validate step var stepType = Step.GetExpressionType(emitter); if (!stepType.IsAnyOf("int", "float")) Error(Resources.errEmitStepExpected); // tmp.Step = step emitter.EmitLoadVariable(tmpVar); Step.Compile(emitter); if (stepType == "int") emitter.EmitConvertToFloat(); emitter.EmitSaveField(emitter.FindField(EmitterID, "step")); } // distribution if (Distribution != null) { // validate distr if (Distribution.GetExpressionType(emitter) != "distr") Error(Resources.errEmitDistributionExpected); // tmp.Distr = distr emitter.EmitLoadVariable(tmpVar); Distribution.Compile(emitter); emitter.EmitSaveField(emitter.FindField(EmitterID, "distr")); } // limit if (Limit != null) { // validate distr if (Limit.GetExpressionType(emitter) != "int") Error(Resources.errEmitLimitExpected); // tmp.Distr = distr emitter.EmitLoadVariable(tmpVar); Limit.Compile(emitter); emitter.EmitSaveField(emitter.FindField(EmitterID, "limit")); } SaveClosuredVariables(emitter, tmpVar); // register emitter in the system emitter.EmitLoadVariable(tmpVar); var registerMethod = emitter.AssemblyImport(typeof(Simulation).GetMethod("RegisterEmitter", new[] { typeof(EventEmitter) })); emitter.EmitCall(registerMethod); }
public override void Compile(Emitter.Emitter emitter) { var leftType = Left.GetExpressionType(emitter); var rightType = Right.GetExpressionType(emitter); var type = GetExpressionType(emitter); // repeat a string if (type == "string") { Left.Compile(emitter); Right.Compile(emitter); emitter.EmitCall(emitter.FindMethod("string", "repeat", "int")); } // multiply matrices else if(type == "matrix") { var matrixType = typeof(MN.Matrix<double>); // matrix by matrix if(leftType == rightType) { Left.Compile(emitter); Right.Compile(emitter); var method = emitter.AssemblyImport(matrixType.GetMethod("Multiply", new[] { matrixType })); emitter.EmitCall(method); } else { // matrix should be the first in stack if(leftType == "matrix") { Left.Compile(emitter); Right.Compile(emitter); if (rightType != "float") emitter.EmitConvertToFloat(); } else { Right.Compile(emitter); Left.Compile(emitter); if (leftType != "float") emitter.EmitConvertToFloat(); } var method = emitter.AssemblyImport(matrixType.GetMethod("Multiply", new[] { typeof(double) })); emitter.EmitCall(method); } } // multiply complex numbers else if (type == "complex") { Left.Compile(emitter); if (leftType != "complex") { emitter.EmitUpcastBasicType(leftType, "float"); emitter.EmitLoadFloat(0); emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float")); } Right.Compile(emitter); if (rightType != "complex") { emitter.EmitUpcastBasicType(rightType, "float"); emitter.EmitLoadFloat(0); emitter.EmitNewObj(emitter.FindMethod("complex", ".ctor", "float", "float")); } emitter.EmitCall(emitter.AssemblyImport(typeof(SN.Complex).GetMethod("op_Multiply", new[] { typeof(SN.Complex), typeof(SN.Complex) }))); } // repeat array else if (leftType.EndsWith("[]")) { Left.Compile(emitter); Right.Compile(emitter); var method = emitter.AssemblyImport(typeof(MirelleStdlib.ArrayHelper).GetMethod("RepeatArray", new[] { typeof(object), typeof(int) })); emitter.EmitCall(method); } // multiply floating point numbers or integers else if (type.IsAnyOf("int", "float")) { Left.Compile(emitter); emitter.EmitUpcastBasicType(leftType, type); Right.Compile(emitter); emitter.EmitUpcastBasicType(rightType, type); emitter.EmitMul(); } }