public static void Init(BytecodeGenerator bytecodeGenerator) { PrintMenu(); var n = Console.ReadLine(); var num = int.Parse(n); switch (num) { case 1: Lexico(bytecodeGenerator, false, null); break; case 2: Sintatico(bytecodeGenerator, false, null); break; case 3: BytecodeGenerator(bytecodeGenerator); break; case 4: Environment.Exit(0); break; default: break; } }
public void Compile(ICharStream input) { try { AssemblerLexer lex = new AssemblerLexer(input); CommonTokenStream tokens = new CommonTokenStream(lex); AssemblerParser p = new AssemblerParser(tokens); BytecodeGenerator gen = new BytecodeGenerator(Defaults.SystemMethods.Values); p.SetGenerator(gen); p.TraceDestination = _traceDestination; p.program(); if (p.NumberOfSyntaxErrors > 0 && _listener != null) { _listener.Error(Convert.ToString(p.NumberOfSyntaxErrors) + " syntax error(s)"); return; } _result = gen.Result; } catch (GenerationException ex) { _listener.Error(ex.Message); } }
public static void Lexico(BytecodeGenerator bytecodeGenerator, Boolean isCalledFromBytecodeGenerator, String filePath) { var a = new Analisador(); var c = ""; Console.Clear(); if (String.IsNullOrEmpty(filePath)) { Console.WriteLine("Digite o caminho do arquivo:"); c = Console.ReadLine(); bytecodeGenerator.filePath = c; } else { c = filePath; } int counter = 1; string line; using (StreamReader file = new StreamReader(c)) { while ((line = file.ReadLine()) != null) { if (line.Length > 0) { a.linha = counter; a.codigo = line; a.Analizar(); } counter++; } } a.lastIndentation(); bytecodeGenerator.lexicalTokens = a.tks; bytecodeGenerator.lexicAnalyzed = true; if (!isCalledFromBytecodeGenerator) { a.PrintTokens(); Console.WriteLine("\nPressione tecla"); Console.ReadLine(); Console.Clear(); Init(bytecodeGenerator); } }
public static void Sintatico(BytecodeGenerator bytecodeGenerator, Boolean isCalledFromBytecodeGenerator, String filePath) { var s = new Sintatico(); var c = ""; Console.Clear(); if (String.IsNullOrEmpty(filePath)) { Console.WriteLine("Digite o caminho do arquivo:"); c = Console.ReadLine(); bytecodeGenerator.filePath = c; } else { c = filePath; } using (StreamReader file = new StreamReader(c)) { var text = file.ReadToEnd(); int result = s.Analizar(text); if (result == 1) { Console.WriteLine("\nReconhecido com sucesso"); bytecodeGenerator.correctSyntax = true; } else { s.PrintErro(); bytecodeGenerator.correctSyntax = false; } } bytecodeGenerator.syntacticAnalyzed = true; if (!isCalledFromBytecodeGenerator) { Console.WriteLine("\nPressione tecla"); Console.ReadLine(); Console.Clear(); Init(bytecodeGenerator); } }
private static void BytecodeGenerator(BytecodeGenerator bytecodeGenerator) { Console.Clear(); if (!String.IsNullOrEmpty(bytecodeGenerator.filePath)) { Console.WriteLine("Utilizando último caminho de arquivo informado\n\n"); } else { Console.WriteLine("Digite o caminho do arquivo:"); bytecodeGenerator.filePath = Console.ReadLine(); } // If lexyc was not executed yet if (!bytecodeGenerator.lexicAnalyzed) { Lexico(bytecodeGenerator, true, bytecodeGenerator.filePath); } if (!bytecodeGenerator.syntacticAnalyzed) { Sintatico(bytecodeGenerator, true, bytecodeGenerator.filePath); } if (bytecodeGenerator.correctSyntax) { bytecodeGenerator.generateBytecode(); } else { Console.WriteLine("\nErro de sintaxe foi identificado, bytecode não gerado"); } Console.WriteLine("\n\nPressione tecla"); Console.ReadLine(); Console.Clear(); Init(bytecodeGenerator); }
public VM.CompiledScript Compile(ICharStream input) { try { LSLTreeAdaptor lslAdaptor = new LSLTreeAdaptor(); // // Initial parse and AST creation // LSLLexer lex = new LSLLexer(input); CommonTokenStream tokens = new CommonTokenStream(lex); LSLParser p = new LSLParser(tokens); p.TreeAdaptor = lslAdaptor; p.TraceDestination = _traceRedirect; lex.TraceDestination = _traceRedirect; LSLParser.prog_return r = p.prog(); if (p.NumberOfSyntaxErrors > 0) { _listener.Error(Convert.ToString(p.NumberOfSyntaxErrors) + " syntax error(s)"); return(null); } // // Definitions // CommonTree t = (CommonTree)r.Tree; CommonTreeNodeStream nodes = new CommonTreeNodeStream(lslAdaptor, t); nodes.TokenStream = tokens; SymbolTable symtab = new SymbolTable(tokens, Defaults.SystemMethods.Values, DefaultConstants.Constants.Values); symtab.StatusListener = _listener; Def def = new Def(nodes, symtab); def.TraceDestination = _traceRedirect; def.Downup(t); nodes.Reset(); if (_listener.HasErrors() || def.NumberOfSyntaxErrors > 0) { return(null); } // // Type and more semantic checks // Compiler.Types types = new Compiler.Types(nodes, symtab); types.TraceDestination = _traceRedirect; types.Downup(t); nodes.Reset(); if (_listener.HasErrors() || types.NumberOfSyntaxErrors > 0) { return(null); } StringTemplateGroup templates; using (TextReader fr = new StreamReader(Path.Combine(_templatePath, "ByteCode.stg"))) { templates = new StringTemplateGroup(fr); fr.Close(); } if (_outputAstGraph) { DotTreeGenerator dotgen = new DotTreeGenerator(); string dot = dotgen.ToDot(t); TextWriter tw = new StreamWriter("ast.txt"); tw.WriteLine(dot); tw.Close(); } Analyze analyze = new Analyze(nodes, symtab); analyze.TraceDestination = _traceRedirect; analyze.Downup(t); nodes.Reset(); foreach (Compiler.BranchAnalyze.FunctionBranch b in analyze.FunctionBranches.Where(pred => pred.Type != null)) { if (!b.AllCodePathsReturn()) { if (_listener != null) { _listener.Error("line: " + b.Node.Line + ":" + b.Node.CharPositionInLine + " " + b.Node.Text + "(): Not all control paths return a value"); } } } if (_listener.HasErrors() || analyze.NumberOfSyntaxErrors > 0) { return(null); } // // Bytecode generation // Gen g = new Gen(nodes, symtab); g.TemplateGroup = templates; g.TraceDestination = _traceRedirect; Gen.script_return ret = g.script(); if (_listener.HasErrors() || g.NumberOfSyntaxErrors > 0) { return(null); } if (ret.Template == null) { return(null); } StringTemplate template = ret.Template; if (_byteCodeDebugging) { _byteCode = template.ToString(); } // // Bytecode compilation // AssemblerLexer alex = new AssemblerLexer(new ANTLRStringStream(template.ToString())); CommonTokenStream atokens = new CommonTokenStream(alex); AssemblerParser ap = new AssemblerParser(atokens); BytecodeGenerator bcgen = new BytecodeGenerator(Defaults.SystemMethods.Values); ap.SetGenerator(bcgen); ap.TraceDestination = _traceRedirect; try { ap.program(); if (_listener.HasErrors() || p.NumberOfSyntaxErrors > 0) { _listener.Error(Convert.ToString(ap.NumberOfSyntaxErrors) + " bytecode generation error(s)"); return(null); } return(bcgen.Result); } catch (GenerationException e) { _listener.Error(e.Message); } return(null); } catch (TooManyErrorsException e) { _listener.Error(String.Format("Too many errors {0}", e.InnerException.Message)); } catch (RecognitionException e) { _listener.Error("line: " + e.Line.ToString() + ":" + e.CharPositionInLine.ToString() + " " + e.Message); } catch (Exception e) { string message = e.Message; while ((e = e.InnerException) != null) { message += "\n" + e.Message; } _listener.Error(message); } return(null); }
public CompilerResults compileFromCompilationUnits(CompilerParameters parameters, CompilationUnitNode[] compilationUnits) { var results = new CompilerResults(); this.context = new CompilerContext(parameters, results); this.statementValidator = new StatementValidator(this.context); this.expressionValidator = new ExpressionValidator(this.context); this.statementValidator.ExpressionValidator = this.expressionValidator; this.expressionValidator.StatementValidator = this.statementValidator; this.reachabilityChecker = new ReachabilityChecker(context); this.assignmentChecker = new AssignmentChecker(context); this.bytecodeGenerator = new BytecodeGenerator(context); foreach (var cu in compilationUnits) { context.CompilationUnits.add(cu); } doCompile(); this.context = null; this.statementValidator = null; this.expressionValidator = null; this.reachabilityChecker = null; this.assignmentChecker = null; this.queryTranslator = null; this.documentationBuilder = null; if (parameters.ProgressTracker != null) { parameters.ProgressTracker.compilationFinished(); } return results; }
public CompilerResults compileFromFiles(CompilerParameters parameters, File[] files) { var results = new CompilerResults(); this.context = new CompilerContext(parameters, results); this.statementValidator = new StatementValidator(this.context); this.expressionValidator = new ExpressionValidator(this.context); this.statementValidator.ExpressionValidator = this.expressionValidator; this.expressionValidator.StatementValidator = this.statementValidator; this.reachabilityChecker = new ReachabilityChecker(context); this.assignmentChecker = new AssignmentChecker(context); this.bytecodeGenerator = new BytecodeGenerator(context); bool tragicError = false; var buffer = new char[4096]; var sb = new StringBuilder(); var parser = new Parser(); foreach (var file in files) { sb.setLength(0); InputStreamReader reader = null; try { reader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")); int read; while ((read = reader.read(buffer)) != -1) { sb.append(buffer, 0, read); } var text = new char[sb.length()]; sb.getChars(0, sizeof(text), text, 0); if (sizeof(text) > 0) { if (text[sizeof(text) - 1] == '\u001a') { text[sizeof(text) - 1] = ' '; } } var preprocessor = new Preprocessor(results.codeErrorManager, text); preprocessor.Filename = file.getAbsolutePath(); preprocessor.Symbols.addAll(parameters.Symbols); var scanner = new PreprocessedTextScanner(results.codeErrorManager, preprocessor.preprocess()); scanner.Filename = file.getAbsolutePath(); var compilationUnit = parser.parseCompilationUnit(scanner); if (compilationUnit != null) { compilationUnit.Symbols = preprocessor.Symbols; context.CompilationUnits.add(compilationUnit); } } catch (CodeErrorException) { } catch (Exception e) { e.printStackTrace(); tragicError = true; break; } finally { if (reader != null) { try { reader.close(); } catch (IOException) { } } } } if (!tragicError) { if (!context.HasErrors) { if (parameters.ProgressTracker != null) { parameters.ProgressTracker.compilationStageFinished(CompilationStage.Parsing); } doCompile(); } } this.context = null; this.statementValidator = null; this.expressionValidator = null; this.reachabilityChecker = null; this.assignmentChecker = null; this.queryTranslator = null; this.documentationBuilder = null; if (parameters.ProgressTracker != null) { parameters.ProgressTracker.compilationFinished(); } return results; }
public static bool BytecodeGenerator_IsValid(BytecodeGenerator value, out string failReason) { failReason = ""; return(true); }
public JumpTable(BytecodeGenerator bytecode) { mBytecode = bytecode; }
static void Main(string[] args) { BytecodeGenerator bytecodeGenerator = new BytecodeGenerator(); Init(bytecodeGenerator); }