Example #1
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);
              }
        }
Example #2
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);
        }
Example #3
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);
        }