Esempio n. 1
0
        protected void CreateEachSymbols()
        {
            var procContext    = this.ExpContext.ProcContext;
            int foreachIndex   = this.ExpContext.ProcContext.CreateEachIndex();
            var listSymbolName = "@each" + foreachIndex + "_list";
            var indexName      = "@each" + foreachIndex + "_index";
            var elementName    = "@each" + foreachIndex + "_item";
            var countName      = "@each" + foreachIndex + "_count";

            ListSymbol = new ZCLocalVar(listSymbolName, SubjectExp.RetType, true);
            //ListSymbol.LoacalVarIndex = procContext.CreateLocalVarIndex(ListSymbol.ZName);
            this.ProcContext.AddLocalVar(ListSymbol);

            Type[] genericTypes = GenericUtil.GetInstanceGenriceType(ZTypeUtil.GetTypeOrBuilder(SubjectExp.RetType), typeof(列表 <>));
            //Type[] genericTypes = GenericUtil.GetInstanceGenriceType(SubjectExp.RetType.SharpType, typeof(列表<>));
            Type ElementType = genericTypes[0];

            IndexSymbol = new ZCLocalVar(indexName, ZLangBasicTypes.ZINT, true);
            //IndexSymbol.LoacalVarIndex = procContext.CreateLocalVarIndex(indexName);
            this.ProcContext.AddLocalVar(IndexSymbol);

            CountSymbol = new ZCLocalVar(countName, ZLangBasicTypes.ZINT, true);
            //CountSymbol.LoacalVarIndex = procContext.CreateLocalVarIndex(countName);
            this.ProcContext.AddLocalVar(CountSymbol);
        }
Esempio n. 2
0
        private Exp AnalyGeneric()
        {
            int count = GenericUtil.GetGenericTypeArgCount(ZTypeUtil.GetTypeOrBuilder(mainZType));// (mainZType.SharpType);

            if (tsize > count + 1)
            {
                Errorf(mainTypeToken.Position, "泛型类型'{0}'声明类型过多", mainTypeToken.Text);
            }
            else if (tsize < count + 1)
            {
                Errorf(mainTypeToken.Position, "泛型类型'{0}'缺少参数类型声明", mainTypeToken.Text);
            }
            else
            {
                if (count == 1)
                {
                    return(AnalyGeneric_1());
                }
                else if (count == 2)
                {
                    return(AnalyGeneric_2());
                }
                else
                {
                    throw new ZLibRTException("Z语言不支持泛型参数超过2的泛型");
                }
            }
            return(noneType);
        }
Esempio n. 3
0
        public override Exp Analy( )
        {
            if (this.IsAnalyed)
            {
                return(this);
            }
            SubjectExp = AnalySubExp(SubjectExp);
            ArgExp     = AnalySubExp(ArgExp);
            if (!this.AnalyCorrect)
            {
                return(this);
            }

            var propertyName = ZLangUtil.ZListItemPropertyName;
            var subjType     = SubjectExp.RetType;

            if (subjType is ZLClassInfo)
            {
                ZLClassInfo zclass  = subjType as ZLClassInfo;
                Type        argType = ZTypeUtil.GetTypeOrBuilder(ArgExp.RetType);
                Property = zclass.SharpType.GetProperty(propertyName, new Type[] { argType });
            }

            if (Property == null)
            {
                Errorf(SubjectExp.Position, "不存在索引");
            }
            else
            {
                RetType = ZTypeManager.GetBySharpType(Property.PropertyType) as ZType;
            }
            IsAnalyed = true;
            return(this);
        }
Esempio n. 4
0
        public void EmitBody()
        {
            if (IsExists)
            {
                return;
            }

            var  classContext = this.ParentProperties.ASTClass.ClassContext;
            var  method       = classContext.InitPropertyMethod;
            var  IL           = method.GetILGenerator();
            bool isStatic     = classContext.IsStatic();

            if (!isStatic)
            {
                IL.Emit(OpCodes.Ldarg_0);
            }
            if (HasValue())
            {
                PropertyValueExp.Emit();
            }
            else
            {
                Type type = ZTypeUtil.GetTypeOrBuilder(PropertyZType);
                EmitHelper.LoadDefaultValue(IL, type);
            }
            EmitHelper.StormField(IL, _fieldBuilder);
        }
Esempio n. 5
0
 public static void EmitLocalVar(ContextProc procContext, bool isStatic, ILGenerator IL, List <ZCLocalVar> localList)
 {
     localList.Reverse();
     for (int i = 0; i < localList.Count; i++)
     {
         ZCLocalVar varSymbol = localList[i];
         varSymbol.VarBuilder = IL.DeclareLocal(ZTypeUtil.GetTypeOrBuilder(varSymbol.GetZType()));
         varSymbol.VarBuilder.SetLocalSymInfo(varSymbol.ZName);
     }
     for (int i = 0; i < localList.Count; i++)
     {
         ZCLocalVar varSymbol = localList[i];
         if (varSymbol.IsNestedClassInstance)
         {
             LocalBuilder       lanmbdaLocalBuilder = procContext.NestedInstance.VarBuilder;
             ConstructorBuilder newBuilder          = procContext.GetNestedClassContext().DefaultConstructorBuilder;
             IL.Emit(OpCodes.Newobj, newBuilder);
             EmitHelper.StormVar(IL, lanmbdaLocalBuilder);
             if (!isStatic)
             {
                 ZCFieldInfo masterClassField = procContext.GetNestedClassContext().MasterClassField;
                 if (masterClassField != null)
                 {
                     ILGeneratorUtil.LoadLocal(IL, lanmbdaLocalBuilder);
                 }
                 IL.Emit(OpCodes.Ldarg_0);
                 EmitSymbolHelper.EmitStorm(IL, masterClassField);
             }
         }
     }
 }
Esempio n. 6
0
        private void AnalyCountMethod()
        {
            ZType        subjectZType  = SubjectExp.RetType;
            Type         mainType      = ZTypeUtil.GetTypeOrBuilder(subjectZType);               // subjectZType.SharpType;
            PropertyInfo countProperty = mainType.GetProperty(ZLangUtil.ZListCountPropertyName); //"Count");

            getCountMethod = countProperty.GetGetMethod();
        }
Esempio n. 7
0
 public void BuildVar(ILGenerator IL)
 {
     foreach (ZCLocalVar localVar in LocalVarList)
     {
         if (!localVar.IsReplaceToNestedFiled)
         {
             localVar.VarBuilder = IL.DeclareLocal(ZTypeUtil.GetTypeOrBuilder(localVar.GetZType()));
             localVar.VarBuilder.SetLocalSymInfo(localVar.ZName);
         }
     }
 }
Esempio n. 8
0
        public bool ReplaceLocalToField(string varName)
        {
            if (this.ContainsPropertyName(varName))
            {
                return(false);
            }
            ZCLocalVar  localVar           = this.MasterProcContext.LocalManager.GetDefLocal(varName);
            ZType       ztype              = localVar.GetZType();
            Type        varSharpType       = ZTypeUtil.GetTypeOrBuilder(ztype);
            var         NestedClassBuilder = this.SelfCompilingType.ClassBuilder;
            ZCFieldInfo zf = this.SelfCompilingType.DefineFieldPublic(varName, (ZAClassInfo)ztype);

            localVar.IsReplaceToNestedFiled = true;
            this.MasterProcContext.LocalManager.DecLocalIndex(varName);
            return(true);
        }
Esempio n. 9
0
        public void EmitName()
        {
            var          classBuilder = this.MethodContext.ClassContext.GetTypeBuilder();
            bool         isStatic     = this.MethodContext.ClassContext.IsStatic();
            ZCMethodDesc ProcDesc     = NamePart.GetZDesc();
            List <Type>  argTypes     = new List <Type>();
            var          parameters   = MethodContext.ZMethodInfo.ZParams;

            foreach (var zparam in parameters)
            {
                argTypes.Add(ZTypeUtil.GetTypeOrBuilder(zparam.ZParamType));
            }
            var MethodName = NamePart.GetMethodName();
            MethodAttributes methodAttributes;

            if (isStatic)
            {
                methodAttributes = MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig;
            }
            else
            {
                methodAttributes = MethodAttributes.Public | MethodAttributes.Virtual;
            }
            MethodBuilder methodBuilder = classBuilder.DefineMethod(MethodName, methodAttributes,
                                                                    ZTypeUtil.GetTypeOrBuilder(RetZType), argTypes.ToArray());

            if (MethodName == "启动")
            {
                Type                   myType           = typeof(STAThreadAttribute);
                ConstructorInfo        infoConstructor  = myType.GetConstructor(new Type[] { });
                CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(infoConstructor, new object[] { });
                methodBuilder.SetCustomAttribute(attributeBuilder);
            }
            else
            {
                string code = this.NamePart.GetZDesc().ToZCode();
                ASTUtil.SetAttrZCode(methodBuilder, code);
            }
            this.MethodContext.SetBuilder(methodBuilder);
            this.NamePart.EmitName();
        }
Esempio n. 10
0
        public override void Emit()
        {
            Type toType  = ZTypeUtil.GetTypeOrBuilder(TypeExp.RetType);
            Type argType = ZTypeUtil.GetTypeOrBuilder(ArgExp.RetType);

            if (toType == typeof(int))
            {
                if (argType == typeof(int))
                {
                    ArgExp.Emit();
                    return;
                }
                else if (argType == typeof(float))
                {
                    ArgExp.Emit();
                    IL.Emit(OpCodes.Conv_I);
                    return;
                }
            }
            else if (toType == typeof(float))
            {
                if (argType == typeof(int))
                {
                    ArgExp.Emit();
                    IL.Emit(OpCodes.Conv_R4);
                    return;
                }
                else if (argType == typeof(float))
                {
                    ArgExp.Emit();
                    return;
                }
            }

            caseMethod         = MakeCastMethod(TypeExp.RetType);
            ArgExp.RequireType = ZLangBasicTypes.ZOBJECT;
            ArgExp.Emit();
            EmitHelper.CallDynamic(IL, caseMethod);
            base.EmitConv();
        }
Esempio n. 11
0
        //private void AnalyListExp()
        //{
        //    ListExp = AnalyExpRaw();
        //    if (ListExp == null)
        //    {
        //        Errorf(ForeachToken.Position, "循环每一个语句没有表达式");
        //    }
        //    else
        //    {
        //        ListExp = ListExp.Analy();
        //    }
        //}

        //private Exp AnalyExpRaw()
        //{
        //    ExpRaw rawExp = Raw.ListExp;// (Exp)ListExp;
        //    ContextExp context = new ContextExp(this.ProcContext, this);
        //    //rawExp.SetContext(context);
        //    List<LexToken> tokens = rawExp.Seg();
        //    ExpParser parser = new ExpParser();
        //    Exp exp = parser.Parse(tokens, this.ProcContext.ClassContext.FileContext);
        //    //exp.SetContext(rawExp.ExpContext);
        //    return exp;
        //}

        protected void CreateEachSymbols()
        {
            var procContext = this.ProcContext;

            int foreachIndex = procContext.CreateRepeatIndex();
            var indexName    = "@foreach" + foreachIndex + "_index";
            var countName    = "@foreach" + foreachIndex + "_count";
            var listName     = "@foreach" + foreachIndex + "_list";
            var itemName     = this.Raw.ItemToken.Text;

            indexSymbol = new ZCLocalVar(indexName, ZLangBasicTypes.ZINT, true);
            //indexSymbol.LoacalVarIndex = procContext.CreateLocalVarIndex(indexName);
            this.ProcContext.AddLocalVar(indexSymbol);

            countSymbol = new ZCLocalVar(countName, ZLangBasicTypes.ZINT, true);
            //countSymbol.LoacalVarIndex = procContext.CreateLocalVarIndex(countName);
            this.ProcContext.AddLocalVar(countSymbol);

            listSymbol = new ZCLocalVar(listName, this.ForeachListExp.RetType, true);
            //listSymbol.LoacalVarIndex = procContext.CreateLocalVarIndex(listName);
            this.ProcContext.AddLocalVar(listSymbol);

            var listType = ZTypeUtil.GetTypeOrBuilder(this.ForeachListExp.RetType);

            Type[] genericTypes = GenericUtil.GetInstanceGenriceType(listType, typeof(列表 <>));
            if (genericTypes.Length == 0)
            {
                genericTypes = GenericUtil.GetInstanceGenriceType(listType, typeof(IList <>));
            }

            Type ElementType = genericTypes[0];

            itemSymbol = new ZCLocalVar(itemName, (ZType)(ZTypeManager.GetBySharpType(ElementType)), true);
            //itemSymbol.LoacalVarIndex = procContext.CreateLocalVarIndex(itemName);
            this.ProcContext.AddLocalVar(itemSymbol);
        }