No boolean in while?

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

No boolean in while?

ngdrummer
got a compiler error that said "boolean value illegal here". Are booleans not allowed for while loop parameters in jack? I mean, it's a small work around, just use an int of 0  and 1 and treat each as a true or false, but still, surprised.
Reply | Threaded
Open this post in threaded view
|

Re: No boolean in while?

cadet1620
Administrator
ngdrummer wrote
got a compiler error that said "boolean value illegal here". Are booleans not allowed for while loop parameters in jack? I mean, it's a small work around, just use an int of 0  and 1 and treat each as a true or false, but still, surprised.
Booleans are allowed in while expressions. Post a sample of the code that is causing the error and I'll see if I can tell why it's happening.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: No boolean in while?

ngdrummer
In reply to this post by ngdrummer
//initialize return variable
                var boolean selected;
               
                let selected = false;
               
line 150 --->while (selected = false)
                {
                        if (Keyboard.keyPressed() = 130)
                        {
                                do cursorUp();
                        }
                }

compiler error says "error line 150: boolean value illegal here"
Reply | Threaded
Open this post in threaded view
|

Re: No boolean in while?

ngdrummer
oh i think i see problem, do i need to write while(~(selected)) instead of selected = false?
Reply | Threaded
Open this post in threaded view
|

Re: No boolean in while?

ngdrummer
yup that fixed it, thanks!
Reply | Threaded
Open this post in threaded view
|

Re: No boolean in while?

cadet1620
Administrator
In reply to this post by ngdrummer
ngdrummer wrote
oh i think i see problem, do i need to write while(~(selected)) instead of selected = false?
Yes, that's correct.

The problem is that using = to compare booleans can be quite problematic, so you should never do it.
boolean is generally defined as zero means false and all non-zero values mean true. The constant true is defined as -1.
boolean b;
let b=7;
if (b) {
    // this code happens
}
if (b=true) {
    // this code doesn't happen!
}
--Mark
Reply | Threaded
Open this post in threaded view
|

Re: No boolean in while?

pablo44
For compiling if-else statements, the book suggests computing ~(condition). So I implemented my compiler by computing the condition, and then adding a 'not' operation. I'm now realizing this is a problem in the context of this thread. If I write the following:

if (boolean) {
  statements1
}
else {
  statements2
}

my compiler will affect a jump to the else clause in any case where boolean is not -1 (all 1s).

For that reason, I am wary of using something like "if (value)" in my jack programs. I'd prefer to use "if boolean_val = true/false", but this is not allowed by the supplied compiler (it is allowed by mine). What is recommended to handle this problem?
Reply | Threaded
Open this post in threaded view
|

Re: No boolean in while?

cadet1620
Administrator
This is a problem with an untyped language like Jack.  The supplied JackCompiler expects a boolean expression for the if and while conditions, so saying something like if(someInt) is an undetected type error.  The expression must evaluate to either true(-1) or false(0).

In fact, it you test it, you'll see that the supplied Jack compiler treats if(42) and while(42) differently!

Java, like Jack also requires the condition of be a boolean, but Java generates a compile error if the condition is the wrong type.

I'm a C/C++ programmer and am used to if/while conditions treating 0 as false and any other value as true, so that's how I wrote my Jack compiler.  My if code starts like this

    if-goto true$1
    goto else$1
    label true$1
    // code for true clause


The extra jump is a trivial expense; it is a single assembly language jump instruction.

--Mark