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) { // 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) { var args = new[] { Parameters.Count == 1 ? typeof(object) : typeof(IEnumerable<dynamic>), typeof(bool) }; var printMethod = emitter.AssemblyImport(typeof(MirelleStdlib.Printer).GetMethod("Print", args)); if (Parameters.Count == 1) { var currType = Parameters[0].GetExpressionType(emitter); Parameters[0].Compile(emitter); if (currType.IsAnyOf("int", "bool", "float", "complex")) emitter.EmitBox(emitter.ResolveType(currType)); } else { var objType = emitter.AssemblyImport(typeof(object)); var arrType = new ArrayType(objType); var tmpVariable = emitter.CurrentMethod.Scope.Introduce("object[]", arrType); // load count & create emitter.EmitLoadInt(Parameters.Count); emitter.EmitNewArray(objType); emitter.EmitSaveVariable(tmpVariable); int idx = 0; foreach (var curr in Parameters) { var currType = curr.GetExpressionType(emitter); emitter.EmitLoadVariable(tmpVariable); emitter.EmitLoadInt(idx); curr.Compile(emitter); if (currType.IsAnyOf("int", "bool", "float", "complex")) emitter.EmitBox(emitter.ResolveType(currType)); emitter.EmitSaveIndex("object"); idx++; } // return the created array emitter.EmitLoadVariable(tmpVariable); } emitter.EmitLoadBool(PrintLine); emitter.EmitCall(printMethod); }
public override void Compile(Emitter.Emitter emitter) { var typeName = Values[0].GetExpressionType(emitter); if (typeName == "null") Error(Resources.errArrayItemNull, Values[0].Lexem); if (typeName.IsAnyOf("", "void")) Error(Resources.errVoidExpression, Values[0].Lexem); TypeReference type; try { type = emitter.ResolveType(typeName); } catch (CompilerException ex) { ex.AffixToLexem(Values[0].Lexem); throw; } var tmpVariable = emitter.CurrentMethod.Scope.Introduce(typeName, emitter.ResolveType(typeName + "[]")); // load count & create emitter.EmitLoadInt(Values.Count); emitter.EmitNewArray(type); emitter.EmitSaveVariable(tmpVariable); int idx = 0; foreach (var curr in Values) { var currType = curr.GetExpressionType(emitter); if (currType != typeName) Error(String.Format(Resources.errArrayTypeMismatch, currType, typeName), curr.Lexem); emitter.EmitLoadVariable(tmpVariable); emitter.EmitLoadInt(idx); curr.Compile(emitter); emitter.EmitSaveIndex(typeName); idx++; } // return the created array emitter.EmitLoadVariable(tmpVariable); }
/// <summary> /// Create iteration code for an array /// </summary> /// <param name="emitter"></param> public void CompileArray(Emitter.Emitter emitter) { var iterType = Iterable.GetExpressionType(emitter); // make local variables only visible inside the scope emitter.CurrentMethod.Scope.EnterSubScope(); var idxVar = emitter.CurrentMethod.Scope.Introduce("int", emitter.ResolveType("int"), Key == null ? null : Key.Data); var arrVar = emitter.CurrentMethod.Scope.Introduce(iterType, emitter.ResolveType(iterType)); var currType = emitter.GetArrayItemType(iterType); var currVar = emitter.CurrentMethod.Scope.Introduce(currType, emitter.ResolveType(currType), Item.Data); // prelude: arr = ..., idx = -1 Iterable.Compile(emitter); emitter.EmitSaveVariable(arrVar); emitter.EmitLoadInt(-1); emitter.EmitSaveVariable(idxVar); emitter.PlaceLabel(BodyStart); // increment emitter.EmitLoadVariable(idxVar); emitter.EmitLoadInt(1); emitter.EmitAdd(); emitter.EmitSaveVariable(idxVar); // loop exit condition emitter.EmitLoadVariable(idxVar); emitter.EmitLoadVariable(arrVar); emitter.EmitLoadArraySize(); emitter.EmitCompareLess(); emitter.EmitBranchFalse(BodyEnd); // variable: curr = array[idx] emitter.EmitLoadVariable(arrVar); emitter.EmitLoadVariable(idxVar); emitter.EmitLoadIndex(currType); emitter.EmitSaveVariable(currVar); // body var preCurrLoop = emitter.CurrentLoop; emitter.CurrentLoop = this; Body.Compile(emitter); emitter.CurrentLoop = preCurrLoop; emitter.EmitBranch(BodyStart); emitter.PlaceLabel(BodyEnd); emitter.CurrentMethod.Scope.LeaveSubScope(); }
public override void Compile(Emitter.Emitter emitter) { // make sure there is an array var exprType = Expression.GetExpressionType(emitter); if (!exprType.Contains("[]")) Error(Resources.errSplatArrayExpected); // define required variables var type = emitter.GetArrayItemType(exprType); var typeRef = emitter.ResolveType(type); var idx = 0; var tmpVar = emitter.CurrentMethod.Scope.Introduce(exprType, emitter.ResolveType(exprType)); // compile array Expression.Compile(emitter); emitter.EmitSaveVariable(tmpVar); foreach(var curr in Names) { // check for variable redefinition if (emitter.CurrentMethod.Scope.Exists(curr.Data)) Error(String.Format(Resources.errVariableRedefinition, curr.Data), curr); var varDecl = emitter.CurrentMethod.Scope.Introduce(type, emitter.ResolveType(type), curr.Data); var elseLabel = emitter.CreateLabel(); var endLabel = emitter.CreateLabel(); // make sure the array is not a null emitter.EmitLoadNull(); emitter.EmitLoadVariable(tmpVar); emitter.EmitCompareEqual(); emitter.EmitBranchTrue(elseLabel); // make sure there are items in the array emitter.EmitLoadInt(idx); emitter.EmitLoadVariable(tmpVar); emitter.EmitLoadArraySize(); emitter.EmitCompareLess(); emitter.EmitBranchFalse(elseLabel); // retrieve the current value emitter.EmitLoadVariable(tmpVar); emitter.EmitLoadInt(idx); if(type == "complex") { emitter.EmitLoadIndexAddress(typeRef); emitter.EmitLoadObject(typeRef); } else emitter.EmitLoadIndex(type); emitter.EmitBranch(endLabel); // or create a default emitter.PlaceLabel(elseLabel); emitter.EmitLoadDefaultValue(type); // assign the variable emitter.PlaceLabel(endLabel); emitter.EmitSaveVariable(varDecl); idx++; } }
/// <summary> /// Emit code for saving local variables into fields /// </summary> private void SaveClosuredVariables(Emitter.Emitter emitter, Utils.ScopeVariable variable) { foreach (var curr in Closures) { emitter.EmitLoadVariable(variable); // load variable var currVar = emitter.CurrentMethod.Scope.Find(curr.Key); emitter.EmitLoadVariable(currVar); // save into field var currField = emitter.FindField(PlannerID, "_" + curr.Key); emitter.EmitSaveField(currField); } }
/// <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); }
/// <summary> /// Create iteration code for a range /// </summary> /// <param name="emitter"></param> public void CompileRange(Emitter.Emitter emitter) { // make local variables only visible inside the scope emitter.CurrentMethod.Scope.EnterSubScope(); var idxVar = emitter.CurrentMethod.Scope.Introduce("int", emitter.ResolveType("int"), Key == null ? null : Key.Data); var rangeVar = emitter.CurrentMethod.Scope.Introduce("range", emitter.ResolveType("range")); var currVar = emitter.CurrentMethod.Scope.Introduce("int", emitter.ResolveType("int"), Item.Data); // preface: range = ..., range.reset Iterable.Compile(emitter); emitter.EmitSaveVariable(rangeVar); emitter.EmitLoadVariable(rangeVar); emitter.EmitCall(emitter.FindMethod("range", "reset")); // set key if exists if(Key != null) { emitter.EmitLoadInt(-1); emitter.EmitSaveVariable(idxVar); } // range.next == false ? exit emitter.PlaceLabel(BodyStart); emitter.EmitLoadVariable(rangeVar); emitter.EmitCall(emitter.FindMethod("range", "next")); emitter.EmitLoadBool(false); emitter.EmitCompareEqual(); emitter.EmitBranchTrue(BodyEnd); // curr = range.current emitter.EmitLoadVariable(rangeVar); emitter.EmitCall(emitter.FindMethod("range", "current")); emitter.EmitSaveVariable(currVar); // increment key if exists if(Key != null) { emitter.EmitLoadVariable(idxVar); emitter.EmitLoadInt(1); emitter.EmitAdd(); emitter.EmitSaveVariable(idxVar); } // body var preCurrLoop = emitter.CurrentLoop; emitter.CurrentLoop = this; Body.Compile(emitter); emitter.CurrentLoop = preCurrLoop; emitter.EmitBranch(BodyStart); emitter.PlaceLabel(BodyEnd); emitter.CurrentMethod.Scope.LeaveSubScope(); }
/// <summary> /// Create iteration code for a dictionary /// </summary> /// <param name="emitter"></param> public void CompileDict(Emitter.Emitter emitter) { // check if key defined if (Key == null) Error(Resources.errDictIterKeyRequired); // make local variables only visible inside the scope emitter.CurrentMethod.Scope.EnterSubScope(); var dictVar = emitter.CurrentMethod.Scope.Introduce("dict", emitter.ResolveType("dict")); var currVar = emitter.CurrentMethod.Scope.Introduce("string[]", emitter.ResolveType("string[]")); var keyVar = emitter.CurrentMethod.Scope.Introduce("string", emitter.ResolveType("string"), Key.Data); var itemVar = emitter.CurrentMethod.Scope.Introduce("string", emitter.ResolveType("string"), Item.Data); // preface: dictVar = ...; Iterable.Compile(emitter); emitter.EmitSaveVariable(dictVar); emitter.EmitLoadVariable(dictVar); emitter.EmitCall(emitter.FindMethod("dict", "reset")); // dict.next == false ? exit emitter.PlaceLabel(BodyStart); emitter.EmitLoadVariable(dictVar); emitter.EmitCall(emitter.FindMethod("dict", "next")); emitter.EmitLoadBool(false); emitter.EmitCompareEqual(); emitter.EmitBranchTrue(BodyEnd); // curr = dict.current emitter.EmitLoadVariable(dictVar); emitter.EmitCall(emitter.FindMethod("dict", "current")); emitter.EmitSaveVariable(currVar); // (key, value) = curr emitter.EmitLoadVariable(currVar); emitter.EmitLoadInt(0); emitter.EmitLoadIndex("string"); emitter.EmitSaveVariable(keyVar); emitter.EmitLoadVariable(currVar); emitter.EmitLoadInt(1); emitter.EmitLoadIndex("string"); emitter.EmitSaveVariable(itemVar); // body var preCurrLoop = emitter.CurrentLoop; emitter.CurrentLoop = this; Body.Compile(emitter); emitter.CurrentLoop = preCurrLoop; emitter.EmitBranch(BodyStart); emitter.PlaceLabel(BodyEnd); emitter.CurrentMethod.Scope.LeaveSubScope(); }
public override void Compile(Emitter.Emitter emitter) { try { Resolve(emitter); } catch (CompilerException ex) { ex.AffixToLexem(Lexem); throw; } switch (Kind) { case IdentifierKind.StaticField: emitter.EmitLoadField(emitter.FindField(OwnerType, Name)); break; case IdentifierKind.Field: if(ExpressionPrefix != null) ExpressionPrefix.Compile(emitter); else emitter.EmitLoadThis(); emitter.EmitLoadField(emitter.FindField(OwnerType, Name)); break; case IdentifierKind.StaticMethod: emitter.EmitCall(emitter.FindMethod(OwnerType, Name)); break; case IdentifierKind.Method: if (ExpressionPrefix != null) ExpressionPrefix.Compile(emitter); else emitter.EmitLoadThis(); emitter.EmitCall(emitter.FindMethod(OwnerType, Name)); break; case IdentifierKind.Variable: emitter.EmitLoadVariable(emitter.CurrentMethod.Scope.Find(Name)); break; case IdentifierKind.Parameter: emitter.EmitLoadParameter(emitter.CurrentMethod.Parameters[Name].Id); break; case IdentifierKind.SizeProperty: ExpressionPrefix.Compile(emitter); emitter.EmitLoadArraySize(); break; } }
/// <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); }