示例#1
0
        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
        }
示例#2
0
        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;
        }
示例#3
0
        private static void CopyNativeLibs(CompilerSettings compilerSettings)
        {
            string targetPath = compilerSettings.CodeGenerationTarget == CodeGenerationTarget.Dotnet
                ? CompilerSettings.DotNetNativesPath
                : CompilerSettings.JavaNativesPath;

            IOHelper.CopyDirItems(targetPath, CompilerSettings.CurrentDirectory);
        }
示例#4
0
        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);
        }
示例#5
0
        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}");
            }
        }
示例#6
0
        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;
            }
        }
示例#7
0
        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;
                }
            }
        }
示例#8
0
        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();
        }