示例#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);
              }
        }
示例#2
0
        public override string GetExpressionType(Emitter.Emitter emitter)
        {
            if (ExpressionType == "")
              {
            var type = ExpressionPrefix.GetExpressionType(emitter);
            if (type == "dict")
              ExpressionType = "string";
            else
              ExpressionType = emitter.GetArrayItemType(type);
              }

              return ExpressionType;
        }
示例#3
0
文件: ForNode.cs 项目: menozz/mirelle
        /// <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();
        }
示例#4
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);
        }
示例#5
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++;
              }
        }