// $Id: Block.java,v 1.1.1.1 1999/12/05 22:19:50 mpp Exp $

package IR2;

import java.util.*;

public class Block implements Walkable {
  protected SymbolTable symbol_table;
  protected HighInstruction instructions[];

  public Block(Block parent, Vector ilist) {
	this(parent.symbol_table, ilist);
  }
  public Block(SymbolTable current_table, Vector ilist) {
	symbol_table = current_table;
	instructions = new HighInstruction[ilist.size()];
	ilist.copyInto(instructions);
  }

  public HighInstruction[] get_instructions() { return instructions; }

  public LowInstruction destructure(LowInstruction next) {
	if (instructions.length == 0) {
	  return new NopInstruction(next);
	} else {
	  for (int i = instructions.length-1; i >= 0; i--)
		next = instructions[i].destructure(next);
	  return next;
	}
  }

  /* Accessor routine get_symbol_table, used in TempVarDescriptor constructor.
   * Maybe this can be made to go away. */
  public SymbolTable get_symbol_table() { return symbol_table; }
  
  // returns number of locals in the local Symbol Table
  public int num_locals() {
    return symbol_table.size();
  }

  /* Walkable implementation. */
  public String node_name() {
	return "block";
  }
  public Enumeration neighbors() {
	return new ShortEnumeration(instructions);
  }
    public String pretty_print(int indent, boolean recursive) {
	String output = new String();
	
	for (int i = 0; i < indent; i++) output += " ";
	if (recursive) {
	    indent += 2;
	    output += "(" + this.node_name() + "\n";
	    /* stuff - calls to prettyprints of internals of this node*/
	    for (Enumeration e = this.neighbors(); e.hasMoreElements();) {
		Walkable foo = (Walkable)e.nextElement();
		output += foo.pretty_print(indent, true);
	    }
	    for (int i = 0; i < indent; i++) output += " ";
	    output += ") /* "+ this.node_name() + " */\n";
	} else {
	    output += "(" + this.node_name() + ")\n";
	}
	return output;
    }
}
