ASTUnaryExpression.java

package edu.udel.cis.vsl.tass.front.minimp.ast.expression;

import edu.udel.cis.vsl.tass.front.minimp.ast.type.ASTPointerType;
import edu.udel.cis.vsl.tass.model.IF.SyntaxException;

public class ASTUnaryExpression extends ASTExpression {
	public enum UnaryOperator {
		PLUS, MINUS, NOT, DEREFERENCE, ADDRESS_OF
	};

	private UnaryOperator exprOperator;
	protected ASTExpressionIF operand;

	public ASTUnaryExpression(UnaryOperator op, ASTExpressionIF operand)
			throws SyntaxException {
		super(operand.getType());
		// Pointer arithmetic
		if (op.equals(UnaryOperator.DEREFERENCE)) {
			// The operand of a dereference operator must have pointer type.
			if (!(operand.getType() instanceof ASTPointerType)) {
				throw new SyntaxException(operand,
						"Type error, the operand of a dereference operator must have pointer type "
								+ "instead of " + operand.getType());
			} else {
				this.exprType = ((ASTPointerType) (operand.getType()))
						.getBaseType();
			}
		} else if (op.equals(UnaryOperator.ADDRESS_OF)) {
			this.exprType = new ASTPointerType(operand.getType());
		}
		exprOperator = op;
		this.operand = operand;
	}

	public UnaryOperator getOperator() {
		return exprOperator;
	}

	public ASTExpressionIF getOperand() {
		return this.operand;
	}

}