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

📄 treemaker.java

📁 Tiger编译器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		arrNode.add(prExp(e.size));
		arrNode.add(prExp(e.init));
		TreeNodeUnit tmp = new TreeNodeUnit(this);
		tmp.Render("ArrayExp:"+e.typ.toString(),e.pos , 2, 1);
		tmp.Merge(arrNode);
		arrNode.clear();
		return tmp;
	}

	/* Print Exp class types. Indent d spaces. */
	public TreeNodeUnit prExp(Exp e) {
		TreeNodeUnit ans = null;
		if (e instanceof OpExp)ans= prExp((OpExp)e);
		else if (e instanceof VarExp) ans= prExp((VarExp) e);
		else if (e instanceof NilExp) ans= prExp((NilExp) e);
		else if (e instanceof IntExp) ans= prExp((IntExp) e);
		else if (e instanceof StringExp)ans=  prExp((StringExp) e);
		else if (e instanceof CallExp) ans= prExp((CallExp) e);
		else if (e instanceof RecordExp) ans= prExp((RecordExp) e);
		else if (e instanceof SeqExp) ans= prExp((SeqExp) e);
		else if (e instanceof AssignExp) ans= prExp((AssignExp) e);
		else if (e instanceof IfExp) ans= prExp((IfExp) e);
		else if (e instanceof WhileExp)ans=  prExp((WhileExp) e);
		else if (e instanceof ForExp) ans= prExp((ForExp) e);
		else if (e instanceof BreakExp)ans=  prExp((BreakExp) e);
		else if (e instanceof LetExp)ans=  prExp((LetExp) e);
		else if (e instanceof ArrayExp) ans= prExp((ArrayExp) e);
		else 
		{
			//throw new Error("Print.prExp");
		}
		
			return ans;
		
	}

	TreeNodeUnit prDec(FunctionDec d) {
	
		TreeNodeUnit tmp;
		
		if (d!=null) {
			
			ArrayList<TreeNodeUnit> arrNode= new ArrayList<TreeNodeUnit>();
			arrNode.add(prFieldlist(d.params));
			arrNode.add(prExp(d.body));
			arrNode.add(prDec(d.next));
			tmp = new TreeNodeUnit(this);
			if (d.result!=null)
			{
				tmp.Render("FunctionDec:"+d.name.toString()+" Result:"+d.result.name.toString(),d.pos , 3, 3);
			}
			else
			{
				tmp.Render("FunctionDec:"+d.name.toString(),d.pos , 3, 3);
			
			}
			tmp.Merge(arrNode);
			arrNode.clear();
		}
		else
		{
			tmp=null;
		}
		return tmp;
	}

	TreeNodeUnit prDec(VarDec d) {
		
		ArrayList<TreeNodeUnit> arrNode= new ArrayList<TreeNodeUnit>();
		
		arrNode.add(prExp(d.init));
		TreeNodeUnit tmp = new TreeNodeUnit(this);
		if (d.typ!=null) {
		
			tmp.Render("VarDec:"+d.name.toString()+":"+d.typ.name.toString() + " escape:"+d.escape,d.pos , 1, 3);
		}
		else
		{
			tmp.Render("VarDec:"+d.name.toString() + " escape:"+d.escape,d.pos , 1, 3);
		}
		tmp.Merge(arrNode);
		arrNode.clear();
		return tmp;
	}

	TreeNodeUnit prDec(TypeDec d) {
		TreeNodeUnit tmp;
		if (d!=null) {
			ArrayList<TreeNodeUnit> arrNode= new ArrayList<TreeNodeUnit>();
			arrNode.add(prTy(d.ty));
			
			if (d.next!=null) {
				 arrNode.add( prDec(d.next));
			}
			tmp = new TreeNodeUnit(this);
			tmp.Render("TypeDec:"+d.name.toString(),d.pos , 1, 3);
			
			tmp.Merge(arrNode);
			arrNode.clear();
		}
		else
		{
			tmp = null;
		}
		return tmp;
	}

	TreeNodeUnit prDec(Dec d) {
		TreeNodeUnit tmp=null;
		if (d instanceof FunctionDec) tmp=prDec((FunctionDec) d);
		else if (d instanceof VarDec) tmp=prDec((VarDec) d);
		else if (d instanceof TypeDec) tmp=prDec((TypeDec) d);
		else
			{
			//throw new Error("Print.prDec");
			
			}
		return tmp;
	}

	TreeNodeUnit prTy(NameTy t) {
		
		
		TreeNodeUnit tmp = new TreeNodeUnit(this);
		tmp.Render("NameTy:"+t.name.toString(),t.pos , 0,4);
		
		
		return tmp;
	}

	TreeNodeUnit prTy(RecordTy t) {
		

		
		
		ArrayList<TreeNodeUnit> arrNode= new ArrayList<TreeNodeUnit>();
		arrNode.add(prFieldlist(t.fields));
		TreeNodeUnit tmp = new TreeNodeUnit(this);
		tmp.Render("RecordTy",t.pos , 1,4);
		tmp.Merge(arrNode);
		arrNode.clear();
		return tmp;
	}

	TreeNodeUnit prTy(ArrayTy t) {

		
		TreeNodeUnit tmp = new TreeNodeUnit(this);
		tmp.Render("ArrayTy:"+t.typ.toString(),t.pos , 0,4);
		
		return tmp;
	}

	TreeNodeUnit prTy(Ty t) {
		TreeNodeUnit tmp = null;
		if (t!=null) {

			if (t instanceof NameTy) tmp=prTy((NameTy) t);
			else if (t instanceof RecordTy)tmp= prTy((RecordTy) t);
			else if (t instanceof ArrayTy) tmp=prTy((ArrayTy) t);
			else 
				{
				//throw new Error("Print.prTy");
				}
		}
		
		return tmp;
	}

	TreeNodeUnit prFieldlist(FieldList f) {
		
		
		int pos = -1;
		String title = "Fieldlist(";
		ArrayList<TreeNodeUnit> arrNode= new ArrayList<TreeNodeUnit>();
		if (f!=null) {
			title += "name:"+f.name.toString();
			title += ",type:"+f.typ.toString();
			title += ",escape:"+f.escape;
			arrNode.add(prFieldlist(f.tail));
			pos = f.pos;
		}
		title+=")";
		TreeNodeUnit tmp = new TreeNodeUnit(this);
		tmp.Render(title,pos , 1,5);
		tmp.Merge(arrNode);
		arrNode.clear();
		return tmp;
	}

	TreeNodeUnit prExplist(ExpList e) {
		
		
		
		String title = "ExpList";

		ArrayList<TreeNodeUnit> arrNode= new ArrayList<TreeNodeUnit>();
		
		if (e!=null) {
			
			arrNode.add(prExp(e.head));
			if (e.tail != null) {
				arrNode.add(prExplist(e.tail));
				
			}
		
		}
		TreeNodeUnit tmp = new TreeNodeUnit(this);
		tmp.Render(title,-1, 2,5);
		tmp.Merge(arrNode);
		arrNode.clear();
		return tmp;
	}

	TreeNodeUnit prDecList(DecList v) {

		
		
	
		String title = "DecList(";
		ArrayList<TreeNodeUnit> arrNode= new ArrayList<TreeNodeUnit>();
		if (v!=null) {
			arrNode.add(prDec(v.head));
			arrNode.add(prDecList(v.tail));
			
		}
		title+=")";
		TreeNodeUnit tmp = new TreeNodeUnit(this);
		tmp.Render(title,-1 , 2,5);
		tmp.Merge(arrNode);
		arrNode.clear();
		return tmp;
	}

	TreeNodeUnit prFieldExpList(FieldExpList f) {
		
		
		
		String title = "FieldExpList(";
		ArrayList<TreeNodeUnit> arrNode= new ArrayList<TreeNodeUnit>();
		if (f!=null) {
			title +="name:"+f.name.toString();
			arrNode.add(prExp(f.init));
			arrNode.add(prFieldExpList(f.tail));
			
		}
		title+=")";
		TreeNodeUnit tmp = new TreeNodeUnit(this);
		tmp.Render(title,-1 , 2,5);
		tmp.Merge(arrNode);
		arrNode.clear();
		return tmp;
	}
}


class TreeNodeUnit
{
	public int NodeWidth,NodeHeight;
	
	public GC renderUnit = null;
	public Image memImg = null;
	public TreeMaker m_parent;
	public int nSlot=1;
	public final static int nMinWidth = 60;
	public final static int nMinHeight = 16;
	
	public final static int DEFAULT_NODE_MARGIN = 3;
	public final static int DEFAULT_NODE_H_BLANK = 2;
	public final static int DEFAULT_NODE_V_BLANK = 20;
	
	protected Color coGrStart = null;
	protected Color coGrEnd = null;
	protected Color coFon = null;
	public TreeNodeUnit(TreeMaker parent)
	{
		m_parent = parent;
		
	}
	public void Render(String Text,int pos,int Slot,int Type)
	{
		
		String finalTxt = Text;
		if (pos!=-1) finalTxt = "(" + pos + ")" + finalTxt;
		Point ptTextEnt = m_parent.getTextExtent(finalTxt);
		Point finalSize = new Point(nMinWidth,nMinHeight);
		if (ptTextEnt.x+DEFAULT_NODE_MARGIN*2 > nMinWidth) finalSize.x = ptTextEnt.x+DEFAULT_NODE_MARGIN*2;
		if (ptTextEnt.y+DEFAULT_NODE_MARGIN*2 > nMinHeight) finalSize.y = ptTextEnt.y+DEFAULT_NODE_MARGIN*2;
		
		NodeWidth = finalSize.x;
		NodeHeight = finalSize.y;
		nSlot = Slot;
		CreateEnv(finalSize.x,finalSize.y);
		switch(Type)
		{
		case 1:
			coGrStart = new Color(TreeMaker.main_Disp,33,135,249);
			coGrEnd = new Color(TreeMaker.main_Disp,48,109,220);
			coFon = TreeMaker.main_Disp.getSystemColor(SWT.COLOR_WHITE);
			break;
		case 2:
			coGrStart = new Color(TreeMaker.main_Disp,33,249,104);
			coGrEnd = new Color(TreeMaker.main_Disp,29,163,25);
			coFon = TreeMaker.main_Disp.getSystemColor(SWT.COLOR_BLACK);
			break;
		case 3:
			coGrStart = new Color(TreeMaker.main_Disp,239,178,30);
			coGrEnd = new Color(TreeMaker.main_Disp,171,115,17);
			coFon = TreeMaker.main_Disp.getSystemColor(SWT.COLOR_BLACK);
			break;
		case 4:
			coGrStart = new Color(TreeMaker.main_Disp,253,102,255);
			coGrEnd = new Color(TreeMaker.main_Disp,182,28,184);
			coFon = TreeMaker.main_Disp.getSystemColor(SWT.COLOR_BLACK);
			break;
		case 5:
			coGrStart = new Color(TreeMaker.main_Disp,18,218,197);
			coGrEnd = new Color(TreeMaker.main_Disp,38,157,62);
			coFon = TreeMaker.main_Disp.getSystemColor(SWT.COLOR_BLACK);
			break;
		
		}
		
		renderUnit.setForeground(TreeMaker.main_Disp.getSystemColor(SWT.COLOR_GRAY));
		renderUnit.drawRectangle(0, 0, finalSize.x-1, finalSize.y-1);
		renderUnit.setForeground(coGrStart);
		renderUnit.setBackground(coGrEnd);
		renderUnit.fillGradientRectangle(1, 1, finalSize.x-2, finalSize.y-2, true);
		
		renderUnit.setForeground(coFon);
		renderUnit.drawText(finalTxt, (NodeWidth-ptTextEnt.x)/2, (NodeHeight-ptTextEnt.y)/2, true);
		
	}
	public void Merge(ArrayList<TreeNodeUnit> plist)
	{
		int total_w=0,total_h=0;
		int actual_solt=0;
		if (renderUnit==null)
		{
			return;
		}
		for (int nPos=0;nPos<plist.size();nPos++)
		{
			TreeNodeUnit curent = plist.get(nPos);
			if (curent!=null)
			{
				total_w += curent.NodeWidth + DEFAULT_NODE_H_BLANK;
				if (curent.NodeHeight > total_h) total_h = curent.NodeHeight;
				actual_solt++;
			}
		}
		if (total_w<this.NodeWidth) total_w = this.NodeWidth;
		if (total_h!=0) total_h+=DEFAULT_NODE_V_BLANK;
		
		total_h += this.NodeHeight;
		
		Image tmpImg = new Image(TreeMaker.main_Disp,total_w,total_h);
		GC tmpGC = new GC(tmpImg);
		tmpGC.drawImage(memImg, (total_w-NodeWidth)/2,0);
		
		//tmpGC.setAntialias(SWT.ON);
		
		renderUnit.dispose();
		memImg.dispose();
		memImg = tmpImg;
		renderUnit = tmpGC;
		int startPosX=0;
		float slotBlank = (this.NodeWidth - 4 )/(actual_solt+1);
		
		for (int nPos=0;nPos<plist.size();nPos++)
		{
			TreeNodeUnit curent = plist.get(nPos);
			if (curent!=null)
			{
				renderUnit.drawImage(curent.memImg, startPosX, DEFAULT_NODE_V_BLANK+this.NodeHeight);
				
				curent.Dispose();
				renderUnit.drawLine(2+Math.round(slotBlank*(nPos+1))+(total_w-NodeWidth)/2, this.NodeHeight, startPosX+curent.NodeWidth/2, DEFAULT_NODE_V_BLANK+this.NodeHeight);
				
				startPosX += curent.NodeWidth + DEFAULT_NODE_H_BLANK;
			}
		}
		
		
		this.NodeWidth = total_w;
		this.NodeHeight = total_h;
		
	}
	protected void CreateEnv(int Width,int Height)
	{
		
		if (memImg==null)
		{
			memImg = new Image(TreeMaker.main_Disp,Width,Height);
			renderUnit = new GC(memImg);
			//renderUnit.setAntialias(SWT.ON);
			renderUnit.setTextAntialias(SWT.ON);
			renderUnit.setFont(m_parent.stdFont);
		}
	}
	public void Dispose()
	{
		
		renderUnit.dispose();
		memImg.dispose();
		coGrStart.dispose();
		coGrEnd.dispose();
		renderUnit = null; //force GC
		memImg = null;
		
	}
}

⌨️ 快捷键说明

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