예제 #1
0
        public void GenerateName(bool isStatic)
        {
            MethodAttributes attr = getMethodAttributes(isStatic);

            if (FnName.IsConstructor(this.ClassName) != -1)
            {
                List <Type>        argTypes           = FnName.ProcDesc.GetArgTypes(1);
                var                ClassSymbol        = MethodContext.ClassContext.ClassSymbol;
                ConstructorBuilder constructorBuilder = ClassSymbol.ClassBuilder.DefineConstructor(attr, CallingConventions.Standard, argTypes.ToArray());
                MethodContext.EmitContext.SetBuilder(constructorBuilder);
                MethodContext.EmitContext.ClassContext = this.ClassContext.EmitContext;
                if (FnName.IsMinConstructor())
                {
                    MethodContext.ClassContext.ZeroConstructor = constructorBuilder;
                }
                else
                {
                    List <TKTProcArg> normalArgs = this.MethodContext.ProcDesc.GetSpecialArgs(1);
                    int start_i = isStatic ? 0 : 1;
                    for (var i = 0; i < normalArgs.Count; i++)
                    {
                        constructorBuilder.DefineParameter(i + start_i, ParameterAttributes.None, normalArgs[i].ArgName);
                    }
                }
                MethodContext.ClassContext.ConstructorBuilderList.Add(constructorBuilder);
            }
            else
            {
                List <Type> argTypes    = FnName.ProcDesc.GetArgTypes(1);
                var         ClassSymbol = MethodContext.ClassContext.ClassSymbol;
                MethodName = getMethodName(isStatic);
                MethodBuilder methodBuilder = ClassSymbol.ClassBuilder.DefineMethod(MethodName, attr, MethodContext.RetType, 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
                {
                    setAttrMappingCode(methodBuilder);
                }
                MethodContext.EmitContext.SetBuilder(methodBuilder);
                MethodContext.EmitContext.ClassContext = this.ClassContext.EmitContext;
                List <TKTProcArg> genericArgs = this.MethodContext.ProcDesc.GetSpecialArgs(2);
                if (genericArgs.Count > 0)
                {
                    string[] names = genericArgs.Select(p => p.ArgName).ToArray();
                    methodBuilder.DefineGenericParameters(names);
                }

                List <TKTProcArg> normalArgs = this.MethodContext.ProcDesc.GetSpecialArgs(1);
                int start_i = isStatic ? 0 : 1;
                for (var i = 0; i < normalArgs.Count; i++)
                {
                    methodBuilder.DefineParameter(i + start_i, ParameterAttributes.None, normalArgs[i].ArgName);
                }
            }
        }
예제 #2
0
        public void Generate(TypeBuilder classBuilder, bool isStatic)
        {
            var             symbols     = MethodContext.Symbols;
            EmitStmtContext stmtContext = new EmitStmtContext(MethodContext.EmitContext);
            var             il          = MethodContext.EmitContext.ILout;

            for (int i = 0; i < MethodContext.LoacalVarList.Count; i++)
            {
                string    ident     = MethodContext.LoacalVarList[i];
                SymbolVar varSymbol = symbols.Get(ident) as SymbolVar;
                varSymbol.VarBuilder = il.DeclareLocal(varSymbol.DimType);
                varSymbol.VarBuilder.SetLocalSymInfo(varSymbol.SymbolName);
            }
            if (MethodContext.RetType != typeof(void))
            {
                if (MethodContext.RetType.IsValueType)
                {
                    EmitHelper.LoadInt(il, 0);
                }
                else
                {
                    il.Emit(OpCodes.Ldnull);
                }
                EmitHelper.StormVar(il, (symbols.Get(MethodContext.LoacalVarList[0]) as SymbolVar).VarBuilder);
            }

            if (FnName.IsConstructor(this.ClassName) != -1)
            {
                if (!isStatic)
                {
                    callBaseDefault();
                    if (ClassContext.InitMemberValueMethod != null)
                    {
                        il.Emit(OpCodes.Ldarg_0);
                        EmitHelper.CallDynamic(il, ClassContext.InitMemberValueMethod, true);//EmitHelper.CallDynamic(il, ClassContext.InitMemberValueMethod);
                    }
                }
                else
                {
                    if (ClassContext.InitMemberValueMethod != null)
                    {
                        EmitHelper.CallDynamic(il, ClassContext.InitMemberValueMethod, true);//EmitHelper.CallDynamic(il, ClassContext.InitMemberValueMethod);
                    }
                }
            }
            //var MethodName = getMethodName(isStatic);
            //if(MethodName=="启动")
            //{
            //    //EmitHelper.CallDynamic(il, typeof(DebugHelper).GetMethod("PrintBaseDirectory"));
            //}
            FnBody.Generate(stmtContext);

            if (MethodContext.RetType != typeof(void))
            {
                il.Emit(OpCodes.Ldloc, retResult.VarBuilder);
                EmitHelper.EmitConv(il, MethodContext.RetType, retResult.DimType);
            }
            il.Emit(OpCodes.Ret);
        }
예제 #3
0
        public override string ToCode()
        {
            FnBody.Deep = 3;
            StringBuilder buf = new StringBuilder();

            buf.AppendLine();
            buf.Append("过程:");
            buf.Append(FnName.ToCode());
            buf.AppendLine(FnBody.ToCode());
            return(buf.ToString());
        }
예제 #4
0
        FnName parseFnName()
        {
            FnName fname   = new FnName();
            var    curline = CurrentToken.Line;

            while (CurrentToken.Kind != TokenKind.EOF && curline == CurrentToken.Line)
            {
                if (CurrentKind == TokenKind.LBS)
                {
                    FnBracket arg = parseFnMuArg();
                    if (arg != null)
                    {
                        fname.NameTerms.Add(arg);
                    }
                }
                else if (CurrentKind == TokenKind.Ident)
                {
                    FnText textt = parseFnText();
                    if (textt != null)
                    {
                        fname.NameTerms.Add(textt);
                    }
                }
                else if (CurrentKind == TokenKind.AssignTo)
                {
                    if (fname.NameTerms == null || fname.NameTerms.Count == 0)
                    {
                        error("过程没有名称");
                        MoveNext();
                    }
                    else
                    {
                        MoveNext();
                        if (/*CurrentToken.IsAtomData()||*/ CurrentKind == TokenKind.Ident)
                        {
                            fname.RetToken = CurrentToken;
                            MoveNext();
                        }
                        else
                        {
                            error("错误的过程结果");
                            MoveNext();
                        }
                    }
                }
                else
                {
                    error("错误的过程名称");
                    MoveNext();
                }
            }
            return(fname);
        }
예제 #5
0
 string getMethodName(bool isStatic)
 {
     if (!isStatic)
     {
         if (this.ClassContext.ClassSymbol.BaseGcl != null)
         {
             TKTProcDesc desc = this.ClassContext.ClassSymbol.BaseGcl.SearchProc(this.FnName.ProcDesc);
             if (desc != null)
             {
                 return(desc.ExMethod.Method.Name);
             }
         }
     }
     return(FnName.CreateMethodName());
 }
예제 #6
0
 MethodAttributes getMethodAttributes(bool isStatic)
 {
     if (isStatic)
     {
         return(MethodAttributes.Public | MethodAttributes.Static);
     }
     else
     {
         if (FnName.IsConstructor(this.ClassName) != -1)
         {
             return(MethodAttributes.Public);
         }
         else
         {
             return(MethodAttributes.Public | MethodAttributes.Virtual);
         }
     }
 }
예제 #7
0
        public MethodAST ParseMethod()
        {
            var tempToken = CurrentToken;

            MoveNext(); //跳过"过程"
            MoveNext(); //跳过":"
            FnName    fnname = parseFnName();
            BlockStmt body   = parseBlock(tempToken.Postion, 2);

            if (fnname != null /*&&body!=null*/)
            {
                MethodAST fnp = new MethodAST();
                fnp.KeyToken = tempToken;
                fnp.FnName   = fnname;
                fnp.FnBody   = body;
                return(fnp);
            }
            return(null);
        }
예제 #8
0
        public bool CompileName(ClassAST classAst, int index, bool isStatic)
        {
            this.ClassAST             = classAst;
            this.ClassContext         = classAst.ClassContext;
            this.ClassName            = classAst.ClassName;
            MethodContext             = new MethodContext(this.ClassContext, FnName.ToCode());
            MethodContext.MethodIndex = index;
            bool b = FnName.Analy(this, isStatic);

            if (b)
            {
                GenerateName(isStatic);
            }
            else
            {
                return(false);
            }
            return(true);
        }