Stronger condition test

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

Stronger condition test

pmk21
My implementation of the Jack-->VM compiler had a subtle bug in my WHILE implementation that wasn't detected in either my project #9 or the reference .jack files.  I came across two other project #9 programs that triggered the issue which came down to using "while (~int) {}" as opposed to "while (~bool_cond) {}".  A slight change to the original Jack, "while ( !(true = (~int)) ) {}", yielded the correct result by the VM.

Attached is a test case that may help others build a more robust Jack-->VM compiler.  It should incorrect branching in the WHILE loop and a similar possible inverted branching in the IF condition.

Main.jack
Reply | Threaded
Open this post in threaded view
|

Re: Stronger condition test

ashort
Instead of:

while (~(true = (~x))) {...}

It's much cleaner to write:

while (~(x = 0)) {...}





Reply | Threaded
Open this post in threaded view
|

Re: Stronger condition test

ashort
and if you really meant to implement the C like test while (!xInt) {...}, then in Jack it would be:

while (xInt = 0) {...}

Reply | Threaded
Open this post in threaded view
|

Re: Stronger condition test

pmk21
Here, 'int' represented that variable type.

A previous posted was kind enough to provide a collection of Jack programs.  They are were good against the reference compiler, but tripped up mine.  The test I provided fills a perceived gap in the available test scripts to catch such a difference.

Here is the original code from which the test was derived. My original implementation went into an infinite loop during the while loop checking for a keypress.

https://github.com/DenverCoder1/jack-man/blob/main/Random.jack
Reply | Threaded
Open this post in threaded view
|

Re: Stronger condition test

ashort
The Jack code you linked to:

while(~key) {...}  

assumes the Jack compiler can handle key being an integer, meaning the compiler interprets any non-zero integer as true.   But when I took this course, that was not part of the spec!  Perhaps in the new edition it has changed, I'll let someone else confirm that.   So that Jack code is bad, or at least it was bad.