예제 #1
0
        public Type Compile()
        {
            bool yes = true;

            AnalyImport();
            CompileClassName();

            TypeBuilder classBuilder = ClassContext.ClassSymbol.ClassBuilder;

            AnalyPropertyName(IsStatic);
            AnalyMethodName(IsStatic);

            yes = yes && !Messager.HasErrorOrWarning();
            yes = yes && compilePropertyBody(IsStatic);
            yes = yes && compileMethodBody(IsStatic);
            if (this.ClassContext.ZeroConstructor == null && ClassContext.InitMemberValueMethod != null)
            {
                ClassContext.ZeroConstructor = ClassContext.EmitContext.CurrentTypeBuilder.DefineConstructor(MethodAttributes.Static, CallingConventions.Standard, new Type[] { });
                var il = ClassContext.ZeroConstructor.GetILGenerator();
                EmitHelper.CallDynamic(il, ClassContext.InitMemberValueMethod, true);//EmitHelper.CallDynamic(il, ClassContext.InitMemberValueMethod);
                il.Emit(OpCodes.Ret);
            }
            if (yes)
            {
                Type type = classBuilder.CreateType();
                return(type);
            }
            else
            {
                return(null);
            }
        }
예제 #2
0
        public Type Compile()
        {
            bool yes = true;

            AnalyImport();
            CompileClassName();

            TypeBuilder classBuilder = ClassContext.ClassSymbol.ClassBuilder;

            AnalyPropertyName(IsStatic);
            AnalyMethodName(IsStatic);
            yes = yes && !Messager.HasErrorOrWarning();
            yes = yes && compilePropertyBody(IsStatic);
            yes = yes && compileMethodBody(IsStatic);

            if (yes)
            {
                Type type = classBuilder.CreateType();
                return(type);
            }
            else
            {
                return(null);
            }
        }
예제 #3
0
 protected bool compilePropertyBody(bool isStatic)
 {
     if (Messager.HasErrorOrWarning() == false)
     {
         propertyAnalyer.CompileBody(isStatic);
         return(true);
     }
     return(false);
 }
예제 #4
0
        protected bool compileMethodBody(bool isStatic)
        {
            TypeBuilder classBuilder = ClassContext.ClassSymbol.ClassBuilder;

            foreach (var item in MethodList)
            {
                item.AnalyBody();
                if (Messager.HasErrorOrWarning() == false)
                {
                    item.Generate(classBuilder, isStatic);
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
예제 #5
0
        public static CompileResult Compile(string srcfile, ProjectContext project)
        {
            Messager.Clear();
            //ContextClass fileContext = new ContextClass(project);
            //fileContext.SourceFileInfo = new FileInfo(srcfile);

            SourceReader reader = new SourceReader();

            reader.ReadFile(srcfile);

            Console.WriteLine("--------------- 词法分析 --------------- ");
            Tokenizer tokenizer = new Tokenizer(reader);
            var       tokens    = tokenizer.Scan();

            //outTokenList(tokens);//Console.ReadKey(); return null;

            Console.WriteLine("--------------- 语法分析 (LEX) -------------");
            Parser lexparser = new Parser(tokens);
            var    fileAST   = lexparser.ParseFile();

            //fileAST.FileContext = fileContext;
            fileAST.Init(srcfile, project);
            Console.WriteLine(fileAST.ToCode());
            //Console.ReadKey(); return null;

            //Console.WriteLine("--------------- 语义分析 ---------------");
            Console.WriteLine("--------------- 语义分析 文件生成 ---------------");
            //Analysiser angly = new Analysiser(fileAST, project);
            //angly.Analysise();
            project.LoadRefTypes();
            fileAST.CompileFile();

            string exeFileName = fileAST.GetBinFilePath();

            if (Messager.HasErrorOrWarning() == false && !string.IsNullOrEmpty(exeFileName))
            {
                Console.WriteLine("编译成功");
                Process.Start(exeFileName);
            }
            return(Messager.Results);
        }