Author Topic: [FIXED] QB64 Precompiler Bug ($LET)  (Read 1504 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3185
    • Steve’s QB64 Archive Forum
[FIXED] QB64 Precompiler Bug ($LET)
« on: October 21, 2018, 12:17:19 PM »
Code: QB64: [Select]
  1. $IF X = 2 THEN
  2.     $LET X = 1
  3.     PRINT "We're inside this block like an idiot!"
  4.  
  5. $IF X = 1 THEN
  6.     PRINT "It's 1"
  7.     PRINT "We skipped the definition as we should have."

As we can see from above, $LET is assigning values, even if it's in a block which should be unavailable for us.  Since we don't have a precompiler variable X to start with, there's no way we should actually do the stuff inside that first $IF block (and we don't, except for that $LET)...

This needs a little tweaking.  To be honest, I'm suprised nobody ever mentioned this before.  Maybe I'm the only person who ever uses $IF and $LET??  :P

« Last Edit: October 29, 2018, 11:49:40 PM by odin »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2689
  • LET IT = BE
    • QB64.org
Re: FIXED: QB64 Precompiler Bug ($LET)
« Reply #1 on: October 21, 2018, 01:01:16 PM »
I often use $IF for OS-specific code blocks, so no $LET required. Good catch.
« Last Edit: October 21, 2018, 01:04:39 PM by odin »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3185
    • Steve’s QB64 Archive Forum
Re: FIXED: QB64 Precompiler Bug ($LET)
« Reply #2 on: October 21, 2018, 01:12:56 PM »
The issue is a case of last assignment vs top-down assignment.

QB64 runs things in 2 passes for us -- the precompiler pass and then the translation pass.

$LET X = 1
$IF X = 1 THEN
    PRINT "Hello World"
    $LET X = 2
$END IF

The above colors itself as if it's going to work for us, but it doesn't.  It simply runs and we have a blank screen.

"HOW??", you ask?

Because in the prepass we set X to 1.
That then lets us into the $IF block, which then makes X a value of 2.

In the second pass, when we execute our code, X is 2, therefore we don't go back into the very block that we successfully passed into in the prepass.

For now, the rule should be: 

**DON'T USE $LET MORE THAN  ONCE TO DEFINE A PRECOMPILER VARIABLE, UNTIL I CAN SORT THIS MESS OUT.**
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3185
    • Steve’s QB64 Archive Forum
Re: FIXED: QB64 Precompiler Bug ($LET)
« Reply #3 on: October 21, 2018, 01:41:41 PM »
The fix is below, with one cavet:  It ruins the auto-indenting for the $IF blocks.

No longer do we see:
$IF WIN THEN
     PRINT "Hello World"
$END IF

Now, it all sticks to the left side of the screen and gives us:

$IF WIN THEN
PRINT "Hello World"
$END IF

**********************
**********************

IMHO, it's better to have it work RIGHT, rather than have it work PRETTY.

I'll dig into the indenting and spacing issues later this afternoon or tomorrow, but for now, this seems to correct the flow/assignment issues which we were seeing with the report above.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2689
  • LET IT = BE
    • QB64.org
Re: FIXED: QB64 Precompiler Bug ($LET)
« Reply #4 on: October 21, 2018, 01:44:01 PM »
Did you push it yet or are you pushing with the indenting?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3185
    • Steve’s QB64 Archive Forum
Re: FIXED: QB64 Precompiler Bug ($LET)
« Reply #5 on: October 21, 2018, 01:47:39 PM »
I'll push it once I get the indenting corrected as well.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3185
    • Steve’s QB64 Archive Forum
Re: FIXED: QB64 Precompiler Bug ($LET)
« Reply #6 on: October 21, 2018, 03:01:06 PM »
Fixed, with indenting... I think.  :P

Also expanded the precompiler so that it now knows how to check to see if a PC-variable has been defined or not, which allows for the such as:

Code: QB64: [Select]
  1. $IF X = UNDEFINED THEN
  2.     PRINT "Hello"
  3.     $LET X = 1
  4.  
  5. $IF X = UNDEFINED THEN
  6.     PRINT "World"
  7.     $LET X = 1
  8.  
  9. $IF X = DEFINED THEN
  10.     PRINT "Steve"
  11.     $LET X = 2
  12.  
  13. $IF X = 1 THEN
  14.     PRINT "I love pizza"
  15.     $LET X = 1
  16.  
  17. $IF X = 2 THEN
  18.     PRINT "I love cheese"
  19.     $LET X = 1

I think the above illustrates our fixes fairly well.

When undefined at start, we go inside the first block and print "Hello", then we define X = 1.

Since it's defined, we skip the second block.

And, since it's defined, we execute the third block of code, printing "Steve" and reassigning X = 2.

Since X is 2, we skip the next block completely.  That $LET there does nothing now.

Which means the next block is valid, but -- as you can see -- we no longer bleed over that $LET X = 1 back to the translation stage and ruin the print statements and such above us.

It's prettified.  It seems to fix all the issues without making new ones.  I adds new capabilities for us...

I don't know what more a person could want.  :P

*************************
*************************

Grab it.  Try it.  Test it.

Unless somebody finds a glitch with the process somewhere that I'm currently overlooking, I'll push the changes into the repo in the next couple of days, after some testing has been done.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3185
    • Steve’s QB64 Archive Forum
Re: FIXED: QB64 Precompiler Bug ($LET)
« Reply #7 on: October 21, 2018, 11:04:33 PM »
Went ahead and pushed fix/enhancements into the development build.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!