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

📄 tree.java

📁 java语言开发的基于tiny语言的编译器
💻 JAVA
字号:


package jeex.tiny;
import java.util.*;
/**
 * Abstract syntax tree.
 */
abstract class Tree {
	static int count = 0; // tree count
	
	Tree() {
		count ++;
	}
	
	void visit(Visitor v) {
		v._case(this);
	}	
	
	static class StmtSeq extends Tree {
		Vector statements;
		StmtSeq(Vector statements) {
			this.statements = statements;
		}
		
		void visit(Visitor v) {
			v._case(this);
		}
	}
	
	static class IfStmt extends Tree {
		Tree condition;
		Tree thenPart;
		Tree elsePart;
		IfStmt(Tree condition,Tree thenPart,Tree elsePart) {
			this.condition = condition;
			this.thenPart = thenPart;
			this.elsePart = elsePart;
		}
		
		void visit(Visitor v) {
			v._case(this);
		}
	}
	
	static class RepeatStmt extends Tree {
		Tree condition;
		Tree stmtSeq;
		RepeatStmt(Tree condition,Tree stmtSeq) {
			this.condition = condition;
			this.stmtSeq = stmtSeq;
		}
		
		void visit(Visitor v) {
			v._case(this);
		}
	}
	
	static class AssignStmt extends Tree {
		Tree ident;
		Tree expr;
		AssignStmt(Tree ident,Tree expr) {
			this.ident = ident;
			this.expr = expr;
		}
		
		void visit(Visitor v) {
			v._case(this);
		}
	}
	
	static class ReadStmt extends Tree {
		Tree ident;
		ReadStmt(Tree ident) {
			this.ident = ident;
		}
		
		void visit(Visitor v) {
			v._case(this);
		}
	}
	
	static class WriteStmt extends Tree {
		Tree expr;
		WriteStmt(Tree expr){
			this.expr = expr;
		}
		
		void visit(Visitor v) {
			v._case(this);
		}
	}
	
	static class Expr extends Tree {
		Tree first;
		int op;
		Tree second;
		
		Expr(Tree first,int op, Tree second) {
			this.first = first;
			this.op = op;
			this.second = second;
		}
				
		void visit(Visitor v) {
			v._case(this);
		}
	}
	
	static class ParExpr extends Tree {
		Tree expr;
		ParExpr(Tree expr) {
			this.expr = expr;
		}
		
		void visit(Visitor v) {
			v._case(this);
		}
	}
	
	static class Ident extends Tree {
		String name;
		Ident(String name) {
			this.name = name;
		}
		
		void visit(Visitor v) {
			v._case(this);
		}
	}
	
	static class Literal extends Tree {
		int value;
		
		Literal(int value) {
			this.value = value;
		}
		
		void visit(Visitor v) {
			v._case(this);
		}
	}
	
	static class Operation extends Tree {
		int op;
		Tree left;
		Tree right;
		Operation(int op,Tree left,Tree right) {
			this.op = op;
			this.left = left;
			this.right = right;
		}
		
		void visit(Visitor v) {
			v._case(this);
		}
	}
	
	static class ErrorTree extends Tree {
		ErrorTree() {
		}
		
		void visit(Visitor v) {
			v._case(this);
		}
	}
	/**
	 * Tree visitor used as base class of any concrete visitor.
	 */
	abstract static class Visitor {
		void _case(StmtSeq tree) {
			_case((Tree)tree);
		}
		void _case(IfStmt tree) {
			_case((Tree)tree);
		}
		void _case(RepeatStmt tree) {
			_case((Tree)tree);
		}
		void _case(AssignStmt tree) {
			_case((Tree)tree);
		}
		void _case(ReadStmt tree) {
			_case((Tree)tree);
		}
		void _case(WriteStmt tree) {
			_case((Tree)tree);
		}
		void _case(Expr tree) {
			_case((Tree)tree);
		}
		void _case(ParExpr tree) {
			_case((Tree)tree);
		}
		void _case(Literal tree) {
			_case((Tree)tree);
		}
		void _case(Ident tree) {
			_case((Tree)tree);
		}
		void _case(Operation tree) {
			_case((Tree)tree);
		}
		void _case(ErrorTree tree) {
			_case((Tree)tree);
		}
		void _case(Tree tree) {
			throw new RuntimeException("unexpected tree");
		}
	}
}

⌨️ 快捷键说明

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