Esempio n. 1
0
        private Type CompileClass(ZCompileClassModel zCompileClassModel, ProjectContext projectContext, ProjectCompileResult result)
        {
            FileCompiler fc   = new FileCompiler();
            Type         type = fc.Compile(projectContext, zCompileClassModel);

            if (type != null)
            {
                //projectContext.LoadProjectType(type);
                result.CompiledTypes.Add(type);
            }
            else
            {
                result.Errors.AddRange(Messager.Results.Errors);
                result.Errors.AddRange(Messager.Results.Warnings);
                Messager.Clear();
            }
            return(type);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
 public void Clear()
 {
     Messager.Clear();
 }
Esempio n. 4
0
        private ProjectCompileResult CompileProject(ZCompileProjectModel zCompileProjectModel,
                                                    ProjectContext projectContext)
        {
            ProjectCompileResult result = new ProjectCompileResult();

            Messager.Clear();

            projectContext.RootNameSpace           = zCompileProjectModel.ProjectPackageName; //设置项目包名称
            projectContext.BinaryFileKind          = zCompileProjectModel.BinaryFileKind;     //设置项目目标文件类型
            projectContext.BinaryFileNameNoEx      = zCompileProjectModel.BinaryFileNameNoEx;
            projectContext.BinarySaveDirectoryInfo = zCompileProjectModel.BinarySaveDirectoryInfo;

            /*--------------------------------------------- 添加DLL -------------------------------------*/
            if (zCompileProjectModel.RefDllList != null && zCompileProjectModel.RefDllList.Count > 0)
            {
                foreach (var dll in zCompileProjectModel.RefDllList)
                {
                    try
                    {
                        Assembly asm = Assembly.LoadFile(dll.FullName);
                        projectContext.AddAssembly(asm);
                    }
                    catch (Exception ex)
                    {
                        Messager.Error("加载DLL文件夹错误:" + ex.Message);
                    }
                }
            }

            /*--------------------------------------------- 添加Package -------------------------------------*/
            if (zCompileProjectModel.RefPackageList != null && zCompileProjectModel.RefPackageList.Count > 0)
            {
                foreach (var packageName in zCompileProjectModel.RefPackageList)
                {
                    projectContext.AddPackage(packageName);
                }
            }
            /*--------------------------------------------- 加载引用Class -------------------------------------*/
            projectContext.LoadRefTypes();


            CompileUtil.GenerateBinary(projectContext);
            /*--------------------------------------------- 编译类 -------------------------------------*/
            if (zCompileProjectModel.SouceFileList != null && zCompileProjectModel.SouceFileList.Count > 0)
            {
                foreach (ZCompileClassModel zCompileClassModle in zCompileProjectModel.SouceFileList)
                {
                    Type type = CompileClass(zCompileClassModle, projectContext, result);
                    if (type != null)
                    {
                        projectContext.LoadProjectType(type);
                    }
                    else
                    {
                        result.Errors.AddRange(Messager.Results.Errors);
                        result.Errors.AddRange(Messager.Results.Warnings);
                        Messager.Clear();
                    }
                }
            }

            if (result.HasError() == false)
            {
                if (zCompileProjectModel.NeedSave)
                {
                    result.BinaryFilePath = saveBinaryFile(projectContext /*, zCompileProjectModel*/);
                }
            }
            return(result);
        }
Esempio n. 5
0
        private ProjectCompileResult CompileProject(ZCompileProjectModel zCompileProjectModel, ProjectContext projectContext)
        {
            ProjectCompileResult result = new ProjectCompileResult();

            Messager.Clear();

            projectContext.RootNameSpace           = zCompileProjectModel.ProjectPackageName; //设置项目包名称
            projectContext.BinaryFileKind          = zCompileProjectModel.BinaryFileKind;     //设置项目目标文件类型
            projectContext.BinaryFileNameNoEx      = zCompileProjectModel.BinaryFileNameNoEx;
            projectContext.BinarySaveDirectoryInfo = zCompileProjectModel.BinarySaveDirectoryInfo;
            projectContext.EntryClassName          = zCompileProjectModel.EntryClassName;

            /*--------------------------------------------- 添加DLL -------------------------------------*/
            if (zCompileProjectModel.RefDllList != null && zCompileProjectModel.RefDllList.Count > 0)
            {
                foreach (var dll in zCompileProjectModel.RefDllList)
                {
                    try
                    {
                        Assembly asm = Assembly.LoadFile(dll.FullName);
                        projectContext.AddAssembly(asm);
                    }
                    catch (Exception ex)
                    {
                        Messager.Error("加载DLL文件" + dll.Name + "错误:" + ex.Message);
                    }
                }
            }

            /*--------------------------------------------- 添加Package -------------------------------------*/
            if (zCompileProjectModel.RefPackageList != null && zCompileProjectModel.RefPackageList.Count > 0)
            {
                foreach (var packageName in zCompileProjectModel.RefPackageList)
                {
                    projectContext.AddPackage(packageName);
                }
            }
            /*--------------------------------------------- 加载引用Class -------------------------------------*/
            projectContext.LoadRefTypes();

            CompileUtil.GenerateBinary(projectContext);
            /*--------------------------------------------- 编译类 -------------------------------------*/
            if (zCompileProjectModel.SouceFileList != null && zCompileProjectModel.SouceFileList.Count > 0)
            {
                foreach (ZCompileClassModel zCompileClassModle in zCompileProjectModel.SouceFileList)
                {
                    Type type = CompileClass(zCompileClassModle, projectContext, result);
                    if (type != null)
                    {
                        projectContext.LoadProjectType(type);
                    }
                    else
                    {
                        result.Errors.AddRange(Messager.Results.Errors);
                        result.Errors.AddRange(Messager.Results.Warnings);
                        Messager.Clear();
                    }
                }
            }
            /*--------------------------------------------- 设置入口点 -------------------------------------*/
            if (projectContext.BinaryFileKind != PEFileKinds.Dll && !string.IsNullOrEmpty(projectContext.EntryClassName))
            {
                var  name = projectContext.EntryClassName;
                Type type = result.GetCompiledType(projectContext.EntryClassName);
                var  file = projectContext.ProjectFileName;
                var  i    = -1;
                if (type == null)
                {
                    errorf(result, file, i + 1, 6, "入口类型'{0}'不存在", name);
                    //continue;
                }
                else
                {
                    MethodInfo main = type.GetMethod("启动");
                    if (main == null)
                    {
                        errorf(result, file, i + 1, 6, "入口类型'{0}'不存在'启动'过程", name);
                        //continue;
                    }
                    else if (!main.IsStatic)
                    {
                        errorf(result, file, i + 1, 6, "入口类型'{0}'不是唯一类型,不能作为启动入口", name);
                        //continue;
                    }
                    projectContext.EmitContext.AssemblyBuilder.SetEntryPoint(main, projectContext.BinaryFileKind);
                }
            }

            if (result.HasError() == false)
            {
                if (zCompileProjectModel.NeedSave)
                {
                    result.BinaryFilePath = saveBinaryFile(projectContext);
                }
            }
            return(result);
        }