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

📄 treeif.java

📁 用Java实现的编译器。把源代码编译成SPARC汇编程序
💻 JAVA
字号:
// $Id: TreeIf.java,v 1.4 1999/09/27 20:01:29 deberg Exp $package java6035.tools.IR;/** * TreeIf represents an if construct. There are three components to this, a * conditional, a true block, and a false block. */public class TreeIf extends TreeNode {    protected TreeBlock true_block;    protected TreeBlock false_block;    protected TreeBlock conditions;    /**     * Constructs a TreeIf object with the arguments given. If either the     * true or false block is null, it is initialized to be empty.      */     public TreeIf(Expression cond, TreeBlock tb, TreeBlock fb)    {	if (cond == null)	    throw new IRException("TreeIf: constructor: cond null");	TreeBlock conditions = new TreeBlock("if_cond");	conditions.appendNode(new SimpleInstr(cond));	init(conditions, tb, fb);    }    /**     * Constructs a TreeIf object with the arguments given.  If either the     * true or false block is null, it is initialized to be empty.      */     public TreeIf(TreeBlock cond, TreeBlock tb, TreeBlock fb)    {	init(cond, tb, fb);    }    private void init(TreeBlock cond, TreeBlock tb, TreeBlock fb)    {	this.conditions = cond;	this.true_block = tb;	this.false_block = fb;	if (this.conditions == null)	    throw new IRException("TreeIf: constructor: cond null");        this.conditions.setParent(this);		if (this.true_block == null)	    this.true_block = new TreeBlock("if_true_blk");        this.true_block.setParent(this);	if (this.false_block == null)	    this.false_block = new TreeBlock("if_false_blk");        this.false_block.setParent(this);    }    protected int tnkind()    {	return TREE_IF;    }    /**     * Returns the true block of this TreeIf object.     */    public TreeBlock getTrueBlock() {        return this.true_block;    }    /**     * Returns the false block of this TreeIf object.     */    public TreeBlock getFalseBlock() {        return this.false_block;    }    /**     * Return the conditional expression for this block. If the block already     * flattened, behavior is not specified. If conditional block passed in is     * null, behavior not specified.     */    public Rhs getCondition()    {	/* The conditional expression is assumed to be the expression on the	 * rhs of the first node of the block. */	if (conditions.size() > 0)	{	    TreeNode tn = conditions.getNodeAt(0);	    if (tn.isSimpleInstr())	    {		return ((SimpleInstr)tn).getRhs();	    }	}	return null;    }    /**     * Returns the conditions of this TreeIf object.  You may find having     * a TreeBlock for your conditional when you have to flatten it.     */    public TreeBlock getConditions() {        return this.conditions;    }    /**     * Walkable: returns the name of the node.     */    public String getNodeName()    {        return "IF";    }    /**     * Walkable: returns the number of neighbors.     */    public int getNeighborCount()    {        return 3;    }    /**     * Walkable: returns the requested neighbor: 0, conditions; 1, true block;     * 2, false block.     */    public Object getNeighbor(int i)    {	if (i == 0)	    return conditions;	else if (i == 1)	    return true_block;	else if (i == 2)	    return false_block;	else 	    return null;    }    public String PPrint(int indent, boolean recursive)    {        String output = new String();        for(int i=0;i<indent;i++) output += " ";        output += "(IF /* conditions, true_branch, false_branch */\n";        indent += 2;        output += conditions.PPrint(indent,true);        if (recursive) {            output += true_block.PPrint(indent,recursive);        } else {            for(int i=0;i<indent;i++) output += " ";            output += true_block.getNodeName() + "\n";        }        if (recursive) {            output += false_block.PPrint(indent,recursive);        } else {            for(int i=0;i<indent;i++) output += " ";            output += false_block.getNodeName() + "\n";        }        indent -= 2;        for(int i=0;i<indent;i++) output += " ";        output += ") /* IF */ \n";        return output;    }}

⌨️ 快捷键说明

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