Exemplo n.º 1
0
        /// <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();
        }
Exemplo n.º 2
0
        public void CompileArray(Emitter.Emitter emitter)
        {
            // make sure it's an array
              var type = emitter.GetArrayItemType(ExpressionPrefix.GetExpressionType(emitter));
              if (type == "")
            Error(Resources.errIndexingNotAnArray);

              // make sure expression type matches array type
              var exprType = Expression.GetExpressionType(emitter);
              if (!emitter.TypeIsParent(type, exprType))
            Error(String.Format(Resources.errAssignTypeMismatch, exprType, type));

              ExpressionPrefix.Compile(emitter);
              Index.Compile(emitter);

              if (type == "complex")
              {
            // special case of valuetypes
            var typeRef = emitter.ResolveType(type);
            emitter.EmitLoadIndexAddress(typeRef);
            Expression.Compile(emitter);
            emitter.EmitSaveObject(typeRef);
              }
              else
              {
            Expression.Compile(emitter);
            emitter.EmitSaveIndex(type);
              }
        }
Exemplo n.º 3
0
        public override void Compile(Emitter.Emitter emitter)
        {
            var leftType = Left.GetExpressionType(emitter);
              var rightType = Right.GetExpressionType(emitter);

              // an array of values
              if (rightType == leftType + "[]")
              {
            Right.Compile(emitter);
            Left.Compile(emitter);
            if (leftType.IsAnyOf("int", "bool", "float", "complex"))
              emitter.EmitBox(emitter.ResolveType(leftType));

            var method = typeof(MirelleStdlib.ArrayHelper).GetMethod("Has", new[] { typeof(object), typeof(object) });
            emitter.EmitCall(emitter.AssemblyImport(method));
              }

              // an object has a "has" method that accepts the lefthand expression
              else
              {
            try
            {
              Expr.IdentifierInvoke("has", Right, Left).Compile(emitter);
              return;
            }
            catch { }

            Error(String.Format(Resources.errOperatorTypesMismatch, "in", leftType, rightType));
              }
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        public override void Compile(Emitter.Emitter emitter)
        {
            // check variable for redefinition
              if (emitter.CurrentMethod.Scope.Exists(Name))
            Error(String.Format(Resources.errVariableRedefinition, Name));

              // get variable type
              var type = Expression.GetExpressionType(emitter);
              if (type == "null")
            Error(Resources.errVariableDefinedNull);
              if (type.IsAnyOf("", "void"))
            Error(Resources.errVoidExpression);

              // compile expression
              Expression.Compile(emitter);

              // declare variable in the current scope
              var varDecl = emitter.CurrentMethod.Scope.Introduce(type, emitter.ResolveType(type), Name);
              // emit save opcode
              emitter.EmitSaveVariable(varDecl);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Compile an array indexer
        /// </summary>
        /// <param name="emitter"></param>
        private void CompileArray(Emitter.Emitter emitter)
        {
            // ensure this is an array
              var type = emitter.GetArrayItemType(ExpressionPrefix.GetExpressionType(emitter));
              if (type == "")
            Error(Resources.errIndexingNotAnArray);

              // ensure index is integer
              if (Index.GetExpressionType(emitter) != "int")
            Error(Resources.errIntIndexExpected, Index.Lexem);

              ExpressionPrefix.Compile(emitter);
              Index.Compile(emitter);

              if (type == "complex")
              {
            // special case of valuetypes
            var typeRef = emitter.ResolveType(type);
            emitter.EmitLoadIndexAddress(typeRef);
            emitter.EmitLoadObject(typeRef);
              }
              else
            emitter.EmitLoadIndex(type);
        }
Exemplo n.º 9
0
        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++;
              }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Emit code for loading local variables from fields
        /// </summary>
        /// <param name="list">List of closures</param>
        private void LoadClosuredVariables(Dictionary<string, string> list, Emitter.Emitter emitter)
        {
            foreach (var curr in list)
              {
            emitter.EmitLoadThis();

            // load field
            var currField = emitter.FindField(PlannerID, "_" + curr.Key);
            emitter.EmitLoadField(currField);

            // save into variable
            var currVar = emitter.CurrentMethod.Scope.Introduce(curr.Value, emitter.ResolveType(curr.Value), curr.Key);
            emitter.EmitSaveVariable(currVar);
              }
        }
Exemplo n.º 11
0
        /// <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();
        }
Exemplo n.º 12
0
        /// <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();
        }
Exemplo n.º 13
0
        public override void Compile(Emitter.Emitter emitter)
        {
            var fromType = Expression.GetExpressionType(emitter);
              var toType = ToType;
              var simpleTypes = new[] { "bool", "int", "float", "long", "complex" };

              if (emitter.ResolveType(toType) == null)
            Error(String.Format(Resources.errTypeNotFound, toType));

              // compile the expression itself
              Expression.Compile(emitter);

              // idiotic case?
              if (fromType == toType) return;

              if(fromType == "null")
              {
            if(toType.IsAnyOf(simpleTypes))
              Error(String.Format(Resources.errInvalidNullCast, toType));

            // no actual casting is required
              }
              // cast simple types
              else if(fromType.IsAnyOf(simpleTypes))
              {
            if (toType.IsAnyOf(simpleTypes))
            {
              switch(toType)
              {
            case "bool": emitter.EmitConvertToBool(); break;
            case "int": emitter.EmitConvertToInt(); break;
            case "float": emitter.EmitConvertToFloat(); break;
            default: throw new NotImplementedException();
              }
            }
            else
              Error(String.Format(Resources.errInvalidCast, fromType, toType));
              }
              else
              {
            // complex type to simple type: search for a conversion method
            if(toType.IsAnyOf(simpleTypes))
            {
              // to_b, to_f, to_i
              string methodName = "to_" + toType[0];
              MethodNode converter = null;
              try
              {
            converter = emitter.FindMethod(fromType, methodName);
              }
              catch
              {
            Error(String.Format(Resources.errInvalidCast, fromType, toType));
              }

              // call the method
              emitter.EmitCall(converter);
            }
            else
            {
              if (fromType.Contains("[]") || toType.Contains("[]"))
            Error(Resources.errCastArray);

              if(!emitter.TypeCastable(fromType, toType))
            Error(String.Format(Resources.errInvalidCast, fromType, toType));

              var type = emitter.FindType(toType);
              emitter.EmitCast(type.Type);
            }
              }
        }