public static void Main(string[] args) { try { ProcessArguments(args, out var files, out var parameters); var paths = ExpandWildCard(files); var compilerSettings = CompilerSettings.ParseCompilerSettings(paths, parameters); if (!compilerSettings.NJCLibFound) { Console.WriteLine($"NJCLib was not found"); } if (compilerSettings.CodeGenerationTarget == CodeGenerationTarget.None) { throw new Exception("Specify code generation target with argument --target=java or --target=net"); } CopyNativeLibs(compilerSettings); RunCompiler(compilerSettings); } catch (Exception e) { #if DEBUG Console.WriteLine("Error: {0}", e); #else Console.WriteLine("Error: {0}", e.Message); #endif } #if DEBUG Console.ReadKey(); #endif }
private static void ProcessFiles(IEnumerable <string> files, CompilerSettings compilerSettings) { var filesList = files.ToList(); if (filesList.Count == 0) { throw new Exception("Select a file"); } var duplicateNames = filesList.GetDuplicatedItems().ToList(); if (duplicateNames.Any()) { throw new Exception($"Files ({string.Join(", ", duplicateNames)}) were specified more than once"); } if (filesList.Any(s => s.Contains(NJCLibName))) { compilerSettings.NJCLibPath = filesList.Find(s => s.Contains(NJCLibName)); filesList.Remove(compilerSettings.NJCLibPath); } if (!compilerSettings.NJCLibFound) { FindNJCLib(compilerSettings); } compilerSettings.FilesPaths = filesList; }
private static void CopyNativeLibs(CompilerSettings compilerSettings) { string targetPath = compilerSettings.CodeGenerationTarget == CodeGenerationTarget.Dotnet ? CompilerSettings.DotNetNativesPath : CompilerSettings.JavaNativesPath; IOHelper.CopyDirItems(targetPath, CompilerSettings.CurrentDirectory); }
public static CompilerSettings ParseCompilerSettings(IEnumerable <string> files, IEnumerable <string> parameters) { CompilerSettings compilerSettings = new CompilerSettings(); ProcessFiles(files, compilerSettings); parameters.ForEach(parameter => { if (parameter.Contains("--target")) { ProcessTargetSetting(compilerSettings, parameter); } if (parameter.Contains("--print")) { ProcessPrintSetting(compilerSettings, parameter); } }); return(compilerSettings); }
private static void ProcessTargetSetting(CompilerSettings settings, string targetSetting) { if (settings.CodeGenerationTarget != CodeGenerationTarget.None) { throw new Exception("--target setting was already specified"); } switch (targetSetting) { case "--target=net": settings.CodeGenerationTarget = CodeGenerationTarget.Dotnet; break; case "--target=java": settings.CodeGenerationTarget = CodeGenerationTarget.Java; break; default: throw new Exception($"target setting does not support value {targetSetting}"); } }
private static void ProcessPrintSetting(CompilerSettings settings, string printSettting) { switch (printSettting) { case "--print-ast": if (settings.PrintAstTree) { throw new Exception(DuplicateSetting(printSettting)); } settings.PrintAstTree = true; break; case "--print-syntax": if (settings.PrintSyntaxTree) { throw new Exception(DuplicateSetting(printSettting)); } settings.PrintSyntaxTree = true; break; } }
private static void FindNJCLib(CompilerSettings settings) { FindNJCLibInternal(CurrentDirectory); if (!settings.NJCLibFound) { FindNJCLibInternal(HomeDirectory); } void FindNJCLibInternal(string path) { DirectoryInfo directoryInfo = new DirectoryInfo(path); var filesInfos = directoryInfo.GetFiles(NJCLibName); settings.NJCLibPath = filesInfos.FirstOrDefault()?.FullName; if (!settings.NJCLibFound) { var libsDir = directoryInfo.GetDirectories(LibsFolderName); settings.NJCLibPath = libsDir.FirstOrDefault() ?.GetFiles(NJCLibName).FirstOrDefault() ?.FullName; } } }
private static void RunCompiler(CompilerSettings compilerSettings) { var astProgram = new Tree.Nodes.Program(); compilerSettings.FilesPaths.ForEach(path => { ITree tree = RunSyntaxAnalysys(path); if (compilerSettings.PrintSyntaxTree) { AstNodePrinter.Print(tree); } if (ErrorService.Instance.HasErrors) { ErrorService.Instance.PrintErrorsToConsole(); } else { astProgram.Construct(tree.CastTo <CommonTree>()); } }); if (compilerSettings.NJCLibFound) { astProgram.Construct( RunSyntaxAnalysys(compilerSettings.NJCLibPath) .CastTo <CommonTree>()); } //AST if (ErrorService.Instance.HasErrors) { return; } SemanticsRunner.Run(astProgram, compilerSettings.CodeGenerationTarget == CodeGenerationTarget.Dotnet ? FunctionIndexingStrategy.Splitted : FunctionIndexingStrategy.United); if (compilerSettings.PrintAstTree) { TreeConsolePrinter tp = new TreeConsolePrinter(); tp.Print(astProgram); } if (compilerSettings.CodeGenerationTarget == CodeGenerationTarget.Java) { JasminCodeGenerator generator = new JasminCodeGenerator(); generator.GenerateCode(astProgram); generator.SaveFiles(); RunJasminBuildScript(); } else if (compilerSettings.CodeGenerationTarget == CodeGenerationTarget.Dotnet) { var trimEnd = Path.ChangeExtension(Path.GetFileName(compilerSettings.FilesPaths.First()), "") .TrimEnd('.'); CodeGenerator cg = new CodeGenerator(trimEnd, astProgram); } //Helpers.FilePrinter.WriteTextToFile(generator.CodeListing, "output.j"); //Console.WriteLine(generator.CodeListing); //RunJasminBuildScript(); }