public void Synthesize(ProgramAnalysis sourceProgramAnalysis, TextWriter outputStream) { LivenessVisitor liveness = new LivenessVisitor(sourceProgramAnalysis); liveness.Visit(sourceProgramAnalysis.AST); Out = outputStream; GenText(".386"); GenText(".model flat,c"); GenText("public asm_main"); GenText("extern put:near,memalloc:near;"); CodeGeneratorVisitor CGVisitor = new CodeGeneratorVisitor(sourceProgramAnalysis, outputStream); MethodTableGeneratorVisitor MTGVisitor = new MethodTableGeneratorVisitor(sourceProgramAnalysis, outputStream); SymbolPreprocessorVisitor SPVisitor = new SymbolPreprocessorVisitor(sourceProgramAnalysis); MTGVisitor.Visit(sourceProgramAnalysis.AST); SPVisitor.Visit(sourceProgramAnalysis.AST); CGVisitor.Visit(sourceProgramAnalysis.AST); GenText("end"); }
static void Main(string[] args) { /* Note Comentar estas 2 lineas para ejecutarlo por consola */ args = new string[1]; args[0] = $"{Environment.CurrentDirectory}/examples/cases.cool"; foreach (var arg in args) { #region Check file exist + update path string current = arg; if (!File.Exists(arg)) { current = Environment.CurrentDirectory + "/examples/" + arg; if (!File.Exists(current)) { Console.WriteLine("No such file or directory. Please enter a valid .cool file"); break; } } #endregion #region Parser-Lexer var input = new ANTLRFileStream(current); var lexer = new CoolGrammarLexer(input); var tokens = new CommonTokenStream(lexer); var parser = new CoolGrammarParser(tokens) { TreeAdaptor = new TreeAdaptor() }; var programNode = (ProgramNode)parser.program().Tree; if (Logger.HasError) { Logger.Report(); return; } #endregion #region Semantic var typeCollectorVisitor = new TypeCollectorVisitor(); typeCollectorVisitor.CheckSemantic(programNode); if (Logger.HasError) { Logger.Report(); return; } var typeBuilderVisitor = new TypeBuilderVisitor(); typeBuilderVisitor.CheckSemantic(programNode); if (Logger.HasError) { Logger.Report(); return; } var typeCheckerVisitor = new TypeCheckerVisitor(); typeCheckerVisitor.CheckSemantic(programNode); if (Logger.HasError) { Logger.Report(); return; } #endregion #region Code Generation var codeGeneratorVisitor = new CodeGeneratorVisitor(); codeGeneratorVisitor.GenerateCode(programNode); var mipsGenerator = new MIPS_GeneratorVisitor(); mipsGenerator.GenerateCode(CIL_Factory.Program); string mipsCode = $"{Path.GetFileNameWithoutExtension(arg)}.s"; string path = arg.Replace(Path.GetFileName(arg), ""); writeOutput(mipsGenerator.MipsInstructions, path + mipsCode); #endregion #region qtSpim using (Process openQtSpim = new Process()) { openQtSpim.StartInfo.UseShellExecute = false; openQtSpim.StartInfo.FileName = "/Applications/QtSpim.app/Contents/MacOS/QtSpim"; openQtSpim.StartInfo.Arguments = mipsCode; openQtSpim.Start(); } #endregion } }
private void Generate() { var visitor = new CodeGeneratorVisitor(_writer, _globalSymbolTable); _astTree.Accept(visitor); }
private static void Main(string[] args) { Queue <Token> tokenQueue = new Queue <Token>(); string filePath = Directory.GetCurrentDirectory(); // Check for args if (args.Length > 0) { string fileName = args[0]; filePath += "\\" + fileName; if (!File.Exists(filePath)) { Console.WriteLine("File not found."); return; } } else { // If no arguments are given (filename, save option) end Console.WriteLine("Error: missing arguments(filename)"); Console.ReadKey(); return; } // Scanner using (StreamReaderExpanded reader = new StreamReaderExpanded(filePath)) { do { Token tempToken = Scanner.Scan(reader); if (tempToken != null) { tokenQueue.Enqueue(tempToken); } } while (tokenQueue.Count == 0 || tokenQueue.Last().Type != TokenType.eof_token); } // Parser Parser parser = new Parser(tokenQueue); ProgNode AST = parser.StartParse(); // Pretty printer //PrettyPrintVisitor vis = new PrettyPrintVisitor(); //vis.Visit(AST); // Semantics SymbolTable symbolTable = new SymbolTable(); SemanticsVisitor typeVisitor = new SemanticsVisitor(symbolTable); typeVisitor.Visit(AST); // Codegeneration CodeGeneratorVisitor codeGeneratorVisitor = new CodeGeneratorVisitor(symbolTable); codeGeneratorVisitor.Visit(AST); // If -s parameter is set, save the .cs code if (args.Length > 1 && args[1] == "-s") { SaveProgram(codeGeneratorVisitor.CSharpString.ToString(), filePath); } // After .cs code is generated, run Roslyn compiler CSharpCompiler.CompileAndStartConsole(codeGeneratorVisitor.CSharpString); }