private static PParser.ProgramContext Parse(ICompilationJob job, FileInfo inputFile) { string fileText = File.ReadAllText(inputFile.FullName); AntlrInputStream fileStream = new AntlrInputStream(fileText); PLexer lexer = new PLexer(fileStream); CommonTokenStream tokens = new CommonTokenStream(lexer); PParser parser = new PParser(tokens); parser.RemoveErrorListeners(); // As currently implemented, P can be parsed by SLL. However, if extensions to the // language are later added, this will remain robust. There is a performance penalty // when a file doesn't parse (it is parsed twice), but most of the time we expect // programs to compile and for code generation to take about as long as parsing. try { // Stage 1: use fast SLL parsing strategy parser.Interpreter.PredictionMode = PredictionMode.Sll; parser.ErrorHandler = new BailErrorStrategy(); return(parser.program()); } catch (Exception e) when(e is RecognitionException || e is OperationCanceledException) { // Stage 2: use slower LL(*) parsing strategy job.Output.WriteMessage("Reverting to LL(*) parsing strategy.", SeverityKind.Warning); tokens.Reset(); parser.AddErrorListener(new PParserErrorListener(inputFile, job.Handler)); parser.Interpreter.PredictionMode = PredictionMode.Ll; parser.ErrorHandler = new DefaultErrorStrategy(); return(parser.program()); } }
public void Compile(ICompilationJob job) { // Run parser on every input file var trees = job.InputFiles.Select(file => { var tree = Parse(job, file); job.LocationResolver.RegisterRoot(tree, file); return(tree); }).ToArray(); // Run typechecker and produce AST var scope = Analyzer.AnalyzeCompilationUnit(job.Handler, trees); // Convert functions to lowered SSA form with explicit cloning foreach (var fun in scope.GetAllMethods()) { IRTransformer.SimplifyMethod(fun); } // Run the selected backend on the project and write the files. var compiledFiles = job.Backend.GenerateCode(job, scope); foreach (var file in compiledFiles) { job.Output.WriteMessage($"Writing {file.FileName}...", SeverityKind.Info); job.Output.WriteFile(file); } }
public void Compile(ICompilationJob job) { // Run parser on every input file PParser.ProgramContext[] trees = job.InputFiles.Select(file => { PParser.ProgramContext tree = Parse(job, file); job.LocationResolver.RegisterRoot(tree, file); return(tree); }).ToArray(); // Run typechecker and produce AST Scope scope = Analyzer.AnalyzeCompilationUnit(job.Handler, trees); // Convert functions to lowered SSA form with explicit cloning foreach (TypeChecker.AST.Declarations.Function fun in scope.GetAllMethods()) { IRTransformer.SimplifyMethod(fun); } // Run the selected backend on the project and write the files. System.Collections.Generic.IEnumerable <CompiledFile> compiledFiles = job.Backend.GenerateCode(job, scope); foreach (CompiledFile file in compiledFiles) { job.Output.WriteMessage($"Generated {file.FileName}...", SeverityKind.Info); job.Output.WriteFile(file); } }
protected CompilationContextBase(ICompilationJob job) { Job = job; Handler = job.Handler; ProjectName = job.ProjectName; LocationResolver = job.LocationResolver; }
public CompilationContext(ICompilationJob job) : base(job) { Names = new PSharpNameManager("PGEN_"); FileName = $"{ProjectName}.cs"; GlobalFunctionClassName = "GlobalFunctions"; }
public CompilationContext(ICompilationJob job) : base(job) { Names = new PrtNameManager("P_"); HeaderFileName = $"{job.ProjectName}.h"; SourceFileName = $"{job.ProjectName}.c"; registeredInts = new ValueInternmentManager <int>(Names); registeredFloats = new ValueInternmentManager <double>(Names); registeredBools = new ValueInternmentManager <bool>(Names); }
public CompilationContext(ICompilationJob job) : base(job) { Names = new RvmNameManager("PGEN_"); ProjectDependencies = job.ProjectDependencies.Count == 0? new List <string>() { ProjectName } : job.ProjectDependencies; }
public CompilationContext(ICompilationJob job) : base(job) { Names = new CSharpNameManager("PGEN_"); FileName = $"{ProjectName}.cs"; ProjectDependencies = job.ProjectDependencies.Count == 0 ? new List <string>() { ProjectName } : job.ProjectDependencies; GlobalFunctionClassName = "GlobalFunctions"; }
public IEnumerable <CompiledFile> GenerateCode(ICompilationJob job, Scope globalScope) { CompilationContext context = new CompilationContext(job); List <CompiledFile> sources = new List <CompiledFile>(); sources.AddRange( new AjFileGenerator(context).GenerateSources(globalScope)); sources.AddRange( new RvmFileGenerator(context).GenerateSources(globalScope)); sources.AddRange( new EventFileGenerator(context).GenerateSources(globalScope)); sources.Add( new StateBaseFileGenerator(context).GenerateSource(globalScope)); return(sources); }
public void Compile(ICompilationJob job) { job.Output.WriteInfo($"----------------------------------------"); job.Output.WriteInfo($"Parsing .."); // Run parser on every input file PParser.ProgramContext[] trees = job.InputFiles.Select(file => { PParser.ProgramContext tree = Parse(job, file); job.LocationResolver.RegisterRoot(tree, file); return(tree); }).ToArray(); job.Output.WriteInfo($"Type checking ..."); // Run typechecker and produce AST Scope scope = Analyzer.AnalyzeCompilationUnit(job.Handler, trees); // Convert functions to lowered SSA form with explicit cloning foreach (TypeChecker.AST.Declarations.Function fun in scope.GetAllMethods()) { IRTransformer.SimplifyMethod(fun); } job.Output.WriteInfo($"Code generation ...."); // Run the selected backend on the project and write the files. System.Collections.Generic.IEnumerable <CompiledFile> compiledFiles = job.Backend.GenerateCode(job, scope); foreach (CompiledFile file in compiledFiles) { job.Output.WriteInfo($"Generated {file.FileName}"); job.Output.WriteFile(file); } job.Output.WriteInfo($"----------------------------------------"); // Compiling the generated C# code // TODO: This is a special case right now but needs to be factored in after the Java code path is available if (job.OutputLanguage == CompilerOutput.CSharp) { job.Output.WriteInfo($"Compiling {job.ProjectName}.csproj ..\n"); CSharpCodeCompiler.Compile(job); job.Output.WriteInfo($"----------------------------------------"); } }
public static void Compile(ICompilationJob job) { var csprojName = $"{job.ProjectName}.csproj"; var csprojPath = Path.Combine(job.ProjectRootPath.FullName, csprojName); var mainFilePath = Path.Combine(job.ProjectRootPath.FullName, "Test.cs"); string stdout = ""; string stderr = ""; // if the file does not exist then create the file if (!File.Exists(csprojPath)) { csprojTemplate = csprojTemplate.Replace("-directory-", Path.GetRelativePath(job.ProjectRootPath.FullName, job.OutputDirectory.FullName)); File.WriteAllText(csprojPath, csprojTemplate); } // if the Main file does not exist then create the file if (!File.Exists(mainFilePath)) { mainCode = mainCode.Replace("-projectName-", job.ProjectName); File.WriteAllText(mainFilePath, mainCode); } // compile the csproj file string[] args = new[] { "build -c Release", csprojName }; int exitCode = RunWithOutput(job.ProjectRootPath.FullName, out stdout, out stderr, "dotnet", args); if (exitCode != 0) { throw new TranslationException($"Compiling generated C# code FAILED!\n" + $"{stdout}\n" + $"{stderr}\n"); } else { job.Output.WriteInfo($"{stdout}"); } }