tags:

views:

94

answers:

2

I have a method that generates and error that a int was expected but found boolean but when I switch it to a boolean it says the same error but reverse int and boolean. Here is my code:

private void compileDeclaration(boolean isGlobal) {
         if (equals(theToken, "int")) {
            accept("int");
            String ident = theToken;
            if (!isIdent(theToken)) t.error("expected identifier, got " + theToken);
            else if (isGlobal){
                symTable.allocVar(ident, isGlobal);
            }

            if (!isGlobal) cs.emit(Machine.ALLOC, symTable.stackFrameSize());
            //dprint("declaring int " + ident);
            theToken = t.token();
            accept (";");
        } else if (equals (theToken, "final")) {
            accept("final");
            accept("int");
            String ident = theToken;
            if (!isIdent(theToken)) t.error("expected identifier, got " + theToken);
            theToken = t.token();
            accept("=");
            int numvalue = new Integer(theToken).intValue();
            if (!isNumber(theToken)) t.error("expected number, got " + theToken);
            else if (numvalue = 0) { **//This is where it highlights my error**
                symTable.allocConst(ident, numvalue);
            }

Any help would be most appreciated.

+1  A: 

Most likely you're calling it in two different places, once with an integer and once with a boolean.

Either that or symTable.allocVar() expects an int.

Anon.
+8  A: 

The line

else if (numvalue = 0) { **//This is where it highlights my error**

is missing an equals symbol, i.e.

else if (numvalue == 0) { **//This is where it highlights my error**
Phillip Ngan
And to explain the problem: "numvalue = 0" requires numvalue to be an int (or long) so that 0 can be assigned to it, hence "int expected". However, the if statement wants a boolean expression, and assignments are not boolean expressions, hence the "bool expected" - two different errors, caused by a missing =. If this were C/C++, you would have a lot of fun with that :-)
Michael Stum
I agree that C++ would give you a good run around! Thanks for the explanation
Phillip Ngan