public SortedDictionary <int, IStatement> Parse(Stream inputStream, TextWriter output, TextWriter error)
        {
            var input  = new AntlrInputStream(inputStream);
            var lexer  = new BasicLexer(input, output, error);
            var tokens = new CommonTokenStream(lexer);
            var parser = new BasicParser(tokens, output, error);
            var tree   = parser.program();

            var visitor = new ProgramVisitor();

            visitor.Visit(tree);

            var statements = visitor.Statements;

            return(statements);
        }
Ejemplo n.º 2
0
 static void Translate(string filename)
 {
     try
     {
         Console.WriteLine("---=== " + filename + " ===---");
         string pyCode = "";
         using (StreamReader fileStream = new StreamReader(filename))
         {
             AntlrInputStream  inputStream       = new AntlrInputStream(fileStream);
             BasicLexer        basicLexer        = new BasicLexer(inputStream);
             CommonTokenStream commonTokenStream = new CommonTokenStream(basicLexer);
             BasicParser       basicParser       = new BasicParser(commonTokenStream);
             PyErrorListener   pyErrorListener   = new PyErrorListener();
             basicParser.AddErrorListener(pyErrorListener);
             var context = basicParser.program();
             if (pyErrorListener.ExceptionList.Count > 0)
             {
                 foreach (var msg in pyErrorListener.ExceptionList)
                 {
                     Console.WriteLine(msg);
                 }
             }
             else
             {
                 PyVisitor visitor = new PyVisitor();
                 pyCode = visitor.Visit(context);
                 File.WriteAllText(Path.ChangeExtension(filename, "py"), pyCode);
                 Console.WriteLine("Successfull translated");
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(filename + " Error: " + ex);
     }
 }