/**
 * 
 */
package ca.uwaterloo.joos.weeder;

import ca.uwaterloo.joos.ast.ASTNode;
import ca.uwaterloo.joos.ast.expr.primary.LiteralPrimary;
import ca.uwaterloo.joos.ast.visitor.IntegerVisitor;

/**
 * @author wenzhuman
 * 
 */
public class IntegerChecker extends IntegerVisitor {

	/**
	 * @throws Exception
	 * 
	 */
	@Override
	public void willVisit(ASTNode node) {

	}

	@Override
	public void didVisit(ASTNode node) {

	}

	@Override
	protected void visitInteger(LiteralPrimary node) throws Exception {
		String intPositiveThreshold = "2147483647";
		String intNegThreshold = "2147483648";
		if (node.getParent().getClass().getSimpleName().equals("UnaryExpression")) {
			checkIntRange(node.getValue(), intNegThreshold);
		} else {
			checkIntRange(node.getValue(), intPositiveThreshold);
		}

	}

	private void checkIntRange(String intString, String intergerThreshold) throws Exception {

		if (intString.length() == intergerThreshold.length()) {
			for (int i = 0; i < intergerThreshold.length(); i++) {
				if ((int) intString.charAt(i) > (int) intergerThreshold.charAt(i)) {

					throw new Exception("Interger out of Range");
				}
			}
		}
		if (intString.length() > intergerThreshold.length()) {
			throw new Exception("Interger out of Range");
		}
	}
}

@@ -1109,32 +1109,148 @@
 ion(
-%22Interger out of Range%22)
+intString + %22AT%22 + i + %221:Interger out of Range%22);%0A%09%09%09%09%7D%0A%09%09%09%09if ((int) intString.charAt(i) %3C (int) intergerThreshold.charAt(i)) %7B%0A%09%09%09%09%09break
 ;%0A%09%09
@@ -1319,16 +1319,16 @@
 th()) %7B%0A
-
 %09%09%09throw
@@ -1343,16 +1343,18 @@
 eption(%22
+2:
 Interger

fix the bug in integer range