⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interpreter2.java

📁 是有关解释器的.用JAVA编写.可以解释一般的JAVA程序.
💻 JAVA
字号:
/**
 * This class is used by the automatic tester to test your implementation of the
 * parser and the interpreter. The following methods are called in the given
 * order: parse(String), typeCheck(AbstractSyntaxNode),
 * run(ExecutableSyntaxNode). The KarelJ-World is initialized by the tester, if
 * necessary. <br>
 * If you want to do your own tests, create your KarelJ-World in the main
 * method. If you have to pass information from the parser to the interpreter,
 * use static class variables. <br>
 * For each test case, the above methods are called in the given order. The next
 * test case is started, after run() has returned.
 */
public class Interpreter2 implements JarelInterpreter {


   /** The main method can be used for your own test runs (without the
    * AutomaticTester).
    * call java -classpath KarelLib.jar;. Interpreter [NAME.task]
    */
    public static void main(String[] args) throws ScannerException,
    ParserException, InterpreterException {
    	
    	if((args.length==0)||(args.length>1)){
    		System.out.println("A filename expected");
			System.exit(0);
    	}
    	
        String programFilename = args[0];
        JarelInterpreter interpreter = new Interpreter2();
        try
		{        
        	// Create an abstract syntax tree from the given filename        
        	ParseResult parseResult = interpreter.parse(programFilename);
        	System.out.println("<<<<Parser OK!>>>>");
        	        	
        	// call (optional) semantic analysis.
        	System.out.println("<<<<Type-Check "
                + (interpreter.typeCheck(parseResult.getEnv(), parseResult.getRootNode()) ? "OK>>>>" : "FAILED>>>>"));        
        	// run the program
//        	PrettyPrint it                
        	//System.out.println(parseResult.getRootNode().format(4));
        	interpreter.run(parseResult.getEnv(), parseResult.getRootNode());        	        	
		}
       catch 	(ParserException parserException)
				{
        			System.out.println(">>>>Parser ERROR<<<<");
        			System.out.println("Position:");
        			System.out.println("    line: " + parserException.line() + "  pos: " + parserException.pos());
        			System.out.println("    " + parserException.text());
				}
        catch	(InterpreterException interpreterException)
				{
					System.out.println(">>>>Interpreter ERROR<<<<");
					System.out.println("Information:");
					System.out.println("     " + interpreterException.text());	
				}	
        /*catch	(ClassCastException castException)
	   			{
       				System.out.println("!!!!Type Cast ERROR!!!!");
       				System.out.println("Statement:");       				       			
	   			}*/
        /*catch 	(Throwable exception)
	   			{
       				System.out.println("\n!!!!Unkown ERROR!!!!\n");
       				System.exit(1);
	   			}*/
    }


   /* The parse method should create and fill this Environment which is used
    * by <code>typeCheck(Environment)</code> and <code>run(Environment)</code>.
    */
   /** Use this method to call your implementation of the parser.
    * @return the executable root node of the created abstract syntax tree
    * @throws ParserException propagate any exceptions.
    * @throws ScannerException propagate any exceptions.
    */
   public ParseResult parse(String programFile) throws ParserException, ScannerException
   {
       Environment env = new Environment();

       // add some code here initializing the environment
       return new ParseResult(env, ProgramNode.parseProgram(env, new Scanner(programFile)));
   }


   /** Use this method to start your implementation of the optional type checking.
    * @return true, if all type checks passed
    * @throws InterpreterException propagate any exceptions.
    */
   public boolean typeCheck(Environment env, AbstractSyntaxNode program)
   throws InterpreterException {
       return program.typeCheck(env);
   }

   /** Use this method to start your implementation of the interpreter.
    * @throws InterpreterException propagate any exceptions.
    */
   public void run(Environment env, ExecutableNode program) throws InterpreterException 
   {   		
   		env= program.run(env);    
   		System.out.println(env.toString());
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -