LiteralExpression.java

package edu.udel.cis.vsl.tass.model.impl.expression.literal;

import edu.udel.cis.vsl.tass.model.IF.ModelFactoryIF;
import edu.udel.cis.vsl.tass.model.IF.expression.ExpressionIF;
import edu.udel.cis.vsl.tass.model.IF.expression.LiteralExpressionIF;
import edu.udel.cis.vsl.tass.model.IF.type.PointerTypeIF;
import edu.udel.cis.vsl.tass.model.IF.type.TypeIF;
import edu.udel.cis.vsl.tass.model.impl.expression.Expression;
import edu.udel.cis.vsl.tass.number.IF.IntegerNumberIF;
import edu.udel.cis.vsl.tass.number.IF.NumberIF;
import edu.udel.cis.vsl.tass.number.IF.RationalNumberIF;

public class LiteralExpression extends Expression implements
		LiteralExpressionIF {

	private final static Boolean TRUE = new Boolean(true);

	private final static Boolean FALSE = new Boolean(false);

	public final static int classHashCode = LiteralExpression.class.hashCode();

	private Object value;

	private int literalId;

	public LiteralExpression(ModelFactoryIF factory, boolean value,
			int literalId) {
		super(factory, ExpressionKind.LITERAL);
		type = factory.booleanType();
		this.value = (value ? TRUE : FALSE);
		this.literalId = literalId;
	}

	public LiteralExpression(ModelFactoryIF factory, char value, int literalId) {
		super(factory, ExpressionKind.LITERAL);
		type = factory.characterType();
		this.value = new Character(value);
		this.literalId = literalId;
	}

	public LiteralExpression(ModelFactoryIF factory, int value, int literalId) {
		super(factory, ExpressionKind.LITERAL);
		type = factory.integerType();
		this.value = factory.numberFactory().integer(value);
		this.literalId = literalId;
	}

	/**
	 * Constructs a LiteralExpression for an array or a record.
	 */
	public LiteralExpression(ModelFactoryIF factory, ExpressionIF[] value,
			TypeIF type, int literalId) {
		super(factory, ExpressionKind.LITERAL);
		assert (type.kind() == TypeIF.TypeKind.ARRAY || type.kind() == TypeIF.TypeKind.RECORD);
		this.type = type;
		this.value = new ExpressionIF[value.length];
		this.literalId = literalId;
		for (int i = 0; i < value.length; i++) {
			((ExpressionIF[]) this.value)[i] = value[i];
		}
	}

	public LiteralExpression(ModelFactoryIF factory, NumberIF value,
			int literalId) {
		super(factory, ExpressionKind.LITERAL);
		this.literalId = literalId;
		if (value instanceof IntegerNumberIF) {
			type = factory.integerType();
		} else if (value instanceof RationalNumberIF) {
			type = factory.rationalType();
		} else {
			throw new IllegalArgumentException("Unknown number type: " + value);
		}
		this.value = value;
	}

	public LiteralExpression(ModelFactoryIF factory, String string,
			int literalId) {
		super(factory, ExpressionKind.LITERAL);
		this.literalId = literalId;
		if ("true".equals(string) || "false".equals(string)) {
			value = ("true".equals(string) ? TRUE : FALSE);
			type = factory.booleanType();
		} else {
			value = factory.numberFactory().number(string);
			if (value instanceof IntegerNumberIF) {
				type = factory.integerType();
			} else if (value instanceof RationalNumberIF) {
				type = factory.rationalType();
			} else {
				throw new IllegalArgumentException("Unknown number type: "
						+ value);
			}
		}
	}

	/** Represents a null pointer value, i.e., NULL. */
	public LiteralExpression(ModelFactoryIF factory, PointerTypeIF pointerType,
			int literalId) {
		super(factory, ExpressionKind.LITERAL);
		this.value = null;
		this.type = pointerType;
		this.literalId = literalId;
	}

	public boolean equals(Object object) {
		if (object instanceof LiteralExpression) {
			return literalId == ((LiteralExpression) object).literalId
					&& value.equals(((LiteralExpression) object).value);
		}
		return false;
	}

	public int hashCode() {
		return classHashCode + literalId + value.hashCode();
	}

	public Object value() {
		return value;
	}

	public String toString() {
		return value.toString();
	}

	public String atomString() {
		if (value instanceof NumberIF) {
			return ((NumberIF) value).atomString();
		}
		return value.toString();
	}

	@Override
	public int literalId() {
		return literalId;
	}
}