// Mike Maxim
// Absyn for integer expressions. Can being in many forms of ints

package edu.cmu.cs.l3.absyn;

public class ASIntConstant extends ASExpression {

    /** Construct the integer constant
     * @param p The position in the source
     * @param v Value of the int
     */
    public ASIntConstant(int p, int v) {
	super(p); m_value=v;
	m_classname = "ASIntConstant";
    }

    /** Construct an int with a String parameter
     * @param p Position in source
     * @param v String form of the number
     */
    public ASIntConstant(int p, String v) {
	super(p);

	m_classname = "ASIntConstant";

	if (v.length() >= 2 && (v.charAt(v.length()-1) == 'l' || v.charAt(v.length()-1) == 'L')) {
	    m_long = true;
	    v = v.substring(0,v.length()-1);
	}
	if (v.length() >= 2 && (v.charAt(v.length()-1) == 'u' || v.charAt(v.length()-1) == 'U')) {
	    m_unsigned = true;
	    v = v.substring(0,v.length()-1);
	}

	if (v.equals("0")) {
	    m_value = 0;
	    return;
	}
	if (v.startsWith("0b")) {
	    m_value = Integer.parseInt(v.substring(2,v.length()),2);
	}
	else if (v.startsWith("0x")) {
	    m_value = Integer.parseInt(v.substring(2,v.length()),16);
	}
	else if (v.startsWith("0")) {
	    m_value = Integer.parseInt(v.substring(1,v.length()),8);
	}
	else {
	    m_value = Integer.parseInt(v.substring(0,v.length()),10);
	}
    }

    /** Return the value */
    public int getValue() {
	return m_value;
    }

    /** Ask if the int is a long */
    public boolean isLong() {
	return m_long;
    }

    /** Ask if the int is signed or unsigned */
    public boolean isUnsigned() {
	return m_unsigned;
    }

    private int m_value;
    private boolean m_unsigned = false;
    private boolean m_long = false;
}
