Supplied compiler doesn't agree what is true between if statement and while statement

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Supplied compiler doesn't agree what is true between if statement and while statement

bupjae
Jack source code:
class Main {
    function void main() {
        if (1) {
            do Output.printString("If statement: 1 is true.");
        } else {
            do Output.printString("If statement: 1 is false.");
        }
        do Output.println();
        while (1) {
            do Output.printString("While statement: 1 is true.");
            return;
        }
        do Output.printString("While statement: 1 is false.");
        return;
    }
}

Expected output:
If statement: 1 is true.
While statement: 1 is true.

Actual output:
If statement: 1 is true.
While statement: 1 is false.

As I don't have *complete* Jack language specification (something like https://docs.oracle.com/javase/specs/jls/se11/html/index.html ), I'm not sure whether it should be compiler bug or not.
I have to debug ~1hr why my Math.jack is not working during Project 12 because of this :(
Reply | Threaded
Open this post in threaded view
|

Re: Supplied compiler doesn't agree what is true between if statement and while statement

cadet1620
Administrator
This is a known JackCompiler issue.

while (expression) statements must use the result of a comparison because they implement while (expression==true) where true in the Jack language is defined to be -1.
Reply | Threaded
Open this post in threaded view
|

Re: Supplied compiler doesn't agree what is true between if statement and while statement

ivant
In reply to this post by bupjae
The problem is that for the while, the Jack compiler generates the following code:
label WHILE_EXP0
push constant 1
not
if-goto WHILE_END0
// code for the body
goto WHILE_EXP0
label WHILE_END0
The problem is, that not is a bitwise negation, not logical one. So, it will replace the 1 at the top of the stack with 1111111111111110, which is different than 0 so the if-goto will not jump. The code generated for if doesn't use the negation, even though the book suggests it's the way to compile it, hence the difference.
bupjae wrote
I have to debug ~1hr why my Math.jack is not working during Project 12 because of this :(
This is not a waste of time! You learned a lot in that 1 hour.