bass icon indicating copy to clipboard operation
bass copied to clipboard

Sub label scopes

Open sasq64 opened this issue 1 year ago • 5 comments

Macros and rept statements introduce a temporary lastLabel and then restores the previous one. This allows for local labels within those scopes only.

But it fails if you try to nest macros/repts.

We should use a stack of labels instead of the single lastLabel, and search up the stack so we can support this.

!macro hey(a) {
   .x = a + 1
   .y = a + 2
   !rept 4 {
      .x = .y * 2 ; Should find above .y (a + 2)
      !rept 4 {
          .z = .x + .y ; Should find .x in parent rept scope and .y in top macro scope.
      }
   }
}

sasq64 avatar May 07 '23 11:05 sasq64

Should we scope labels (hence variable assignments) to a "block" (macro/rept/… - maybe just like with !enum)? It would be more convenient and also less "headache" getting the parsing right don't you think?

Above code slightly would change:

!macro hey(a) {
   x = a + 1  ; as of current implentation "a" would still defined in global scope
   y = a + 2
   !rept 4 {
      x = y * 2 ; Should find above y (a + 2)
      !rept 4 {
          z = x + y ; Should find x in parent rept scope and y in top macro scope.
      }
   }
}

antis81 avatar Jun 10 '23 13:06 antis81

Except then macros could no longer define labels, which may be something you want.

sasq64 avatar Jun 10 '23 13:06 sasq64

Defining (global) labels in macros will break the macro scoping anyway right? Other could you maybe write a small example/test?

antis81 avatar Jun 11 '23 16:06 antis81

No you can do

!macro Test() {
plot:
    nop
    rts
}

    jsr plot
    rts

    Test()

sasq64 avatar Jun 11 '23 17:06 sasq64

Thanks for providing some sample! Shouldn't this example produce a shadowing error?

Writing this slightly different helps with quite some issues:

!macro Test() {
    nop
    rts
}


plot: 
    Test()

main:
    ; do some initialization -- almost feels like function call :)
    ldy #17
    jsr plot
    rts

    ldy #24
    Test()

antis81 avatar Jun 12 '23 15:06 antis81