public Type Compile(ProjectContext context, ZCompileClassModel zCompileClassModel) { string srcPath = zCompileClassModel.SourceFileInfo.FullName;// zCompileClassModel.GetSrcFullPath(); if (!File.Exists(srcPath)) { Messager.Error("文件" + zCompileClassModel.SourceFileInfo + "不存在"); return(null); } FileInfo fi = new FileInfo(srcPath); Messager.FileName = fi.Name; //Console.WriteLine("--------------- 词法分析 " + Messager.FileName + "--------------- "); List <Token> tokens = new List <Token>( ); List <Token> preTokens = scanPreCode(zCompileClassModel); tokens.AddRange(preTokens); List <Token> fileTokens = scanFileCode(zCompileClassModel); tokens.AddRange(fileTokens); //outTokenList(tokens);//Console.ReadKey(); return null; //Console.WriteLine("--------------- 语法分析 (LEX) " + Messager.FileName + "-------------"); Parser lexparser = new Parser(tokens); var fileAST = lexparser.ParseFile(); fileAST.Init(srcPath, context); //Console.WriteLine(fileAST.ToCode()); //Console.ReadKey(); return null; //Console.WriteLine("--------------- 语义分析 文件生成 " + Messager.FileName + "---------------"); Type type = fileAST.CompileFile(); return(type); }
void lexError(string msg) { Messager.Error(line, col, msg); }
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); }
protected void error(CodePostion postion, string msg) { Messager.Error(postion.Line, postion.Col, /*this.GetType().Name + ":" + */ msg); TrueAnalyed = false; }
protected void error(Token tok, string str) { Messager.Error(tok.Line, tok.Col, /*" " + CurrentToken.ToString() + " - " +*/ str); }
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); }