package pp.s1184725.boppi;

import static pp.s1184725.boppi.antlr.BoppiLexer.*;

import pp.iloc.eval.Machine;
import pp.s1184725.boppi.antlr.BoppiLexer;

/**
 * This class provides 4 basic data types:
 * <ul>
 * <li>character (sized {@link Machine#DEFAULT_CHAR_SIZE})</li>
 * <li>integer (sized {@link Machine#INT_SIZE})</li>
 * <li>boolean (sized {@link Machine#INT_SIZE})</li>
 * <li>void (empty/unit type)</li>
 * </ul>
 */
public enum SimpleType implements Type {

	/**
	 * Integer type
	 */
	INT(Machine.INT_SIZE),
	/**
	 * Character type
	 */
	CHAR(Machine.DEFAULT_CHAR_SIZE),
	/**
	 * Boolean type
	 */
	BOOL(Machine.INT_SIZE),
	/**
	 * Void type (not the same as {@link TupleType#UNIT})
	 */
	VOID(0);

	private final int size;

	@Override
	public int getSize() {
		return size;
	}

	private SimpleType(int typeSize) {
		size = typeSize;
	}

	/**
	 * Maps a token lexed by {@link BoppiLexer} to a data type.
	 * 
	 * @param token
	 *            the token to parse
	 * @return the data type of the token, {@link SimpleType#VOID} if
	 *         unrecognized
	 */
	public static Type fromToken(int token) {
		switch (token) {
		case INTTYPE:
			return SimpleType.INT;

		case CHARTYPE:
			return SimpleType.CHAR;

		case BOOLTYPE:
			return SimpleType.BOOL;

		default:
			return SimpleType.VOID;
		}
	}
}
