Package ro.pub.cpl.sq2.hello1

Source Code of ro.pub.cpl.sq2.hello1.Interpreter

package ro.pub.cpl.sq2.hello1;

import java.io.IOException;
import java.io.PrintStream;

import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;

public class Interpreter {

  /**
   * @param args
   */
  public static void main(String[] args) {
    if (args.length == 0) {
      System.out.println("Usage:\n"
          +"java ro.pub.cpl.sq2.hello1.Interpreter <file.sq2>"
          +"java ro.pub.cpl.sq2.hello1.Interpreter <file.sq2> <file.ir>"
          );
    }
    else try {
      ANTLRFileStream input = new ANTLRFileStream(args[0]);
      Sq2Lexer lexer = new Sq2Lexer(input);
      CommonTokenStream tokens = new CommonTokenStream (lexer);
      Sq2Parser parser = new Sq2Parser(tokens);       
      Program p = parser.program();
      // TODO Populate symbol table with predefine functions
      // Check that the program is semantically correct
      p.check();
      if (args.length == 1)
        p.run(); // Interpreter
      else
        p.generate(new PrintStream(args[1])); // Compiler
    } catch (IOException e) {
      System.out.println("Could not process file : " + e.getMessage());
    } catch (RecognitionException e) {
      System.out.println("File could not be parsed: " + e);
    } catch (RuntimeException e) {
      System.out.println("File could not be parsed: " + e);     
    } catch (LanguageException e) {
      System.out.println(e);
    }
  }
}
TOP

Related Classes of ro.pub.cpl.sq2.hello1.Interpreter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.