package pp.s1184725.boppi.type;

import pp.iloc.eval.Machine;

/**
 * The (-&gt;) type. Takes exactly two types as arguments.
 * 
 * @author Frank Wibbelink
 */
public class FunctionType implements ReferenceType {
	private Type argument, result;

	/**
	 * Creates a new function type from the given parameter and return types.
	 * 
	 * @param returnType
	 *            the result type when applying an argument to the function
	 * @param parameter
	 *            the input type for this function type
	 */
	public FunctionType(Type returnType, Type parameter) {
		result = returnType;
		argument = parameter;
	}

	@Override
	public int getSize() {
		return Machine.INT_SIZE;
	}

	/**
	 * Returns the size of the function context.
	 * 
	 * @return the size (in bytes) of the function context
	 */
	public int getReferenceSize() {
		return 3 * Machine.INT_SIZE;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof FunctionType))
			return false;

		FunctionType other = (FunctionType) obj;
		return result.equals(other.result) && argument.equals(other.argument);
	}

	@Override
	public String toString() {
		return argument.toString() + "->" + result.toString();
	}

	/**
	 * Returns the result type of this function.
	 * 
	 * @return the result type
	 */
	public Type getReturn() {
		return result;
	}

	/**
	 * Returns the parameter type of this function.
	 * 
	 * @return the parameter type
	 */
	public Type getParameter() {
		return argument;
	}
}
