public void Generate(AnalyzeResult options)
        {
            SymbolUpdatingVisitor symbolUpdatingVisitor = new SymbolUpdatingVisitor();

            symbolUpdatingVisitor.VisitTree(options.AbstractSyntaxTree);

            AssemblyBuildingVisitor assemblyBuildingVisitor = new AssemblyBuildingVisitor(options.SymbolTable);

            assemblyBuildingVisitor.VisitTree(options.AbstractSyntaxTree);

            if (_generateOutputUnits)
            {
                foreach (string filesContent in options.FilesContents)
                {
                    _ouBuilder.ParseText(filesContent);
                }

                _ouBuilder.SaveOutputUnits(_outputPathOuDir);
            }

            DatBuilder datBuilder = new DatBuilder(options.SymbolTable, options.SymbolsWithInstructions);

            DatFile = datBuilder.GetDatFile();
            DatFile.Save(_outputPathDat);
        }
Beispiel #2
0
        public bool CompileFromSrc(
            string srcFilePath,
            bool compileToAssembly,
            bool verbose             = true,
            bool generateOutputUnits = true
            )
        {
            var absoluteSrcFilePath = Path.GetFullPath(srcFilePath);

            try
            {
                string[] paths       = SrcFileHelper.LoadScriptsFilePaths(absoluteSrcFilePath).ToArray();
                string   srcFileName = Path.GetFileNameWithoutExtension(absoluteSrcFilePath).ToLower();

                string runtimePath = Path.Combine(GetBuiltinsPath(), srcFileName + ".d");
                if (File.Exists(runtimePath))
                {
                    _assemblyBuilder.IsCurrentlyParsingExternals = true;
                    if (verbose)
                    {
                        Console.WriteLine($"[0/{paths.Length}]Compiling runtime: {runtimePath}");
                    }
                    DaedalusParser parser = GetParserForScriptsFile(runtimePath);
                    ParseTreeWalker.Default.Walk(new DaedalusListener(_assemblyBuilder, 0), parser.daedalusFile());
                    _assemblyBuilder.IsCurrentlyParsingExternals = false;
                }

                for (int i = 0; i < paths.Length; i++)
                {
                    if (verbose)
                    {
                        Console.WriteLine($"[{i + 1}/{paths.Length}]Compiling: {paths[i]}");
                    }

                    string         fileContent = GetFileContent(paths[i]);
                    DaedalusParser parser      = GetParserForText(fileContent);

                    _assemblyBuilder.ErrorContext.FileContentLines = fileContent.Split(Environment.NewLine);
                    _assemblyBuilder.ErrorContext.FilePath         = paths[i];
                    _assemblyBuilder.ErrorContext.FileIndex        = i;
                    ParseTreeWalker.Default.Walk(new DaedalusListener(_assemblyBuilder, i), parser.daedalusFile());
                    if (generateOutputUnits)
                    {
                        _ouBuilder.ParseText(fileContent);
                    }
                }

                if (!compileToAssembly)
                {
                    Directory.CreateDirectory(_outputDirPath);
                }

                if (generateOutputUnits)
                {
                    _ouBuilder.SaveOutputUnits(_outputDirPath);
                }

                _assemblyBuilder.Finish();
                if (_assemblyBuilder.Errors.Any())
                {
                    _assemblyBuilder.Errors.Sort((x, y) => x.CompareTo(y));

                    string lastErrorFilePath  = "";
                    string lastErrorBlockName = null;
                    var    logger             = new StdErrorLogger();
                    foreach (CompilationMessage error in _assemblyBuilder.Errors)
                    {
                        if (lastErrorFilePath != error.FilePath)
                        {
                            lastErrorFilePath = error.FilePath;
                            Console.WriteLine(error.FilePath);
                        }

                        if (lastErrorBlockName != error.ExecBlockName)
                        {
                            lastErrorBlockName = error.ExecBlockName;
                            if (error.ExecBlockName == null)
                            {
                                Console.WriteLine($"{error.FileName}: In global scope:");
                            }
                            else
                            {
                                Console.WriteLine($"{error.FileName}: In {error.ExecBlockType} ‘{error.ExecBlockName}’:");
                            }
                        }

                        error.Print(logger);
                    }
                    return(false);
                }

                if (compileToAssembly)
                {
                    Console.WriteLine(_assemblyBuilder.GetAssembler());
                }
                else
                {
                    Directory.CreateDirectory(_outputDirPath);
                    string datPath = Path.Combine(_outputDirPath, srcFileName + ".dat");
                    _assemblyBuilder.SaveToDat(datPath);
                }

                return(true);
            }
            catch (Exception exc)
            {
                Console.WriteLine("SRC compilation failed");
                Console.WriteLine($"{exc}");
                return(false);
            }
        }