ASTSelfChangeExpression.java

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

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

public class ASTSelfChangeExpression extends ASTExpression {
	public enum SelfChangeOperator {
		INCREMENT, DECREMENT
	};

	private ASTLhsExpressionIF body;
	private SelfChangeOperator operator;
	private boolean isSuffix;

	public ASTSelfChangeExpression(SelfChangeOperator operator,
			ASTLhsExpressionIF body, boolean isSuffix) throws SyntaxException {
		super(body.getType());

		// Check whether the type of the operand is legal
		if (!(body.getType() instanceof ASTIntegerType)
				&& !(body.getType() instanceof ASTPointerType)) {
			throw new SyntaxException(
					"Only integer or pointer type are allowed in increment/decrement expressions.");
		}

		this.operator = operator;
		this.body = body;
		this.isSuffix = isSuffix;
	}

	public ASTExpressionIF getBody() {
		return this.body;
	}

	public boolean isSuffix() {
		return this.isSuffix;
	}

	public SelfChangeOperator getOperator() {
		return this.operator;
	}

	public String toString() {
		String result = body.toString();
		String op;
		if (this.operator == SelfChangeOperator.INCREMENT) {
			op = "++";
		} else {
			op = "--";
		}

		if (this.isSuffix) {
			return result + op;
		} else {
			return op + result;
		}
	}
}