ArrayLambdaExpression.java

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

import edu.udel.cis.vsl.tass.model.IF.ModelFactoryIF;
import edu.udel.cis.vsl.tass.model.IF.SyntaxException;
import edu.udel.cis.vsl.tass.model.IF.expression.LHSExpressionIF;
import edu.udel.cis.vsl.tass.model.IF.type.FunctionTypeIF;
import edu.udel.cis.vsl.tass.model.IF.type.TypeIF;
import edu.udel.cis.vsl.tass.model.IF.type.TypeIF.TypeKind;

public class ArrayLambdaExpression extends BinaryExpression implements
		LHSExpressionIF {

	public ArrayLambdaExpression(ModelFactoryIF factory, Expression extent,
			Expression function) throws SyntaxException {
		super(factory, ExpressionKind.ARRAY_LAMBDA, extent, function);

		if (extent.type().kind() != TypeKind.INTEGER) {
			throw new SyntaxException(extent,
					"extent argument to lambda expression must have integer type");
		}

		TypeIF typeOfFunction = function.type();

		if (typeOfFunction.kind() != TypeKind.FUNCTION) {
			throw new SyntaxException(function,
					"argument to array lambda must have function type");
		}

		FunctionTypeIF functionType = (FunctionTypeIF) typeOfFunction;
		TypeIF[] inputTypes = functionType.inputTypes();

		if (inputTypes.length != 1) {
			throw new SyntaxException(function,
					"argument to array lambda must be function of one variable");
		}

		TypeIF domainType = inputTypes[0];

		if (domainType.kind() != TypeKind.INTEGER) {
			throw new SyntaxException(function,
					"argument to array lambda must be function of int");
		}

		TypeIF rangeType = functionType.outputType();

		type = factory.arrayType(rangeType);
	}

	public String toString() {
		return "Array<" + this.left() + "," + this.right + ">";
	}

	public String atomString() {
		return toString();
	}
}