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

📄 demoharness.java

📁 program assegment for compiler
💻 JAVA
字号:
/*
 * Created on Aug 19, 2004
 *
 */
package edu.berkeley.cs164.util;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;

/**
 * @author bodik
 *
 * This class provides example uses of the utilities included in the 
 * edu.berkeley.cs164.util package. 
 */
public class DemoHarness {

	public static void main(String[] args) {
		// First, you must call Util.processArgs().
		Util.processArgs(args);
		
		// Now do the compilation.  In this demo harness, 
		// just parse a Java/Decaf file into an AST and print it
		
		// Parse the Java file ...
		CompilationUnit astRoot = Parser.parseJavaFile("tests/test.decaf");
		
		// ... and print the resulting AST.
		Util.debug('p', astRoot.toString());
		ASTPrinter printer = new ASTPrinter();
		astRoot.accept(printer);
		Util.debug('p', printer.buffer.toString());
		
		// Finally, you must call Util.done(), to enable test coverage.  
		Util.done();
	}

	/*
	 * @author bodik
	 *
	 * An AST printer used in this demo.  
	 */
	static class ASTPrinter extends ASTVisitor {
		StringBuffer buffer = new StringBuffer();

		public void preVisit(ASTNode node) {
			//write the name of the node being visited
			printDepth(node);
			String name = node.getClass().getName();
			name = name.substring(name.lastIndexOf('.') + 1);
			buffer.append(name);
			buffer.append(" {\r\n");
		}

		public void postVisit(ASTNode node) {
			//write a closing brace to indicate end of the node
			printDepth(node);
			buffer.append("}\r\n");
		}

		void printDepth(ASTNode node) {
			//indent the current line to an appropriate depth
			while (node != null) {
				node = node.getParent();
				buffer.append("  ");
			}
		}
	}
}

⌨️ 快捷键说明

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