LiteralNode.java

package edu.udel.cis.vsl.tass.ast.impl.expression;

import java.io.PrintWriter;
import java.util.NoSuchElementException;

import edu.udel.cis.vsl.tass.ast.IF.ASTNodeIF;
import edu.udel.cis.vsl.tass.ast.IF.AbstractSyntaxTreeIF;
import edu.udel.cis.vsl.tass.ast.IF.IdentifierNodeIF;
import edu.udel.cis.vsl.tass.ast.IF.expression.LiteralNodeIF;
import edu.udel.cis.vsl.tass.ast.IF.type.TypeNodeIF;

public class LiteralNode extends PureExpressionNode implements LiteralNodeIF {

	private IdentifierNodeIF identifier;
	
	private TypeNodeIF type;

	public LiteralNode(long id) {
		super(id);
	}
	
    public LiteralNode(long id, IdentifierNodeIF mIdentifier, TypeNodeIF mType) {
		super(id);
		identifier = mIdentifier;
		type = mType;
	}

	@Override
	public IdentifierNodeIF identifier() {
		return identifier;
	}
	
	@Override
	public TypeNodeIF type() {
		return type;
	}
	
	@Override
	public void setIdentifier(IdentifierNodeIF name) {
		identifier = name;
	}
	
	@Override
	public void setType(TypeNodeIF mType) {
		type = mType;
	}

	@Override
	public ASTNodeIF child(int index) throws NoSuchElementException {
		switch (index) {
		case 0:
			return super.child(0);
		case 1:
			return identifier;
		case 2:
			return type;
		default:
			throw new NoSuchElementException("Node " + id()
					+ " does not have a child with index " + index + ".");
		}
	}

	@Override
	public void setChild(int i, ASTNodeIF child) throws NoSuchElementException {
		switch (i) {
		case 0:
			super.setChild(i,child);
			break;
		case 1:
			identifier = (IdentifierNodeIF) child;
			break;
		case 2:
			type = (TypeNodeIF) child;
			break;
		default:
			throw new NoSuchElementException("Node " + id()
					+ " does not have a child with index " + i + ".");
		}
	}

	@Override
	public int numChildren() {
		return 3;
	}
	
	@Override
	protected String nodeType() {
		return "Literal Node";
	}

	@Override
	public void toXml(String prefix, PrintWriter out,AbstractSyntaxTreeIF ast) {
		super.toXml(prefix, out,ast);

		if (identifier != null) {
			out.println(prefix+"<ln:name>");
			identifier.toXml(prefix+"|",out,ast);
			out.println(prefix+"</ln:name>");
		}

		out.println(prefix+"<ln:type>");
		identifier.toXml(prefix+"|",out,ast);
		out.println(prefix+"</ln:type>");
	}	
}