Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub label scopes #38

Open
sasq64 opened this issue May 7, 2023 · 5 comments
Open

Sub label scopes #38

sasq64 opened this issue May 7, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@sasq64
Copy link
Owner

sasq64 commented May 7, 2023

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 sasq64 added the enhancement New feature or request label May 7, 2023
@antis81
Copy link
Contributor

antis81 commented Jun 10, 2023

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.
      }
   }
}

@sasq64
Copy link
Owner Author

sasq64 commented Jun 10, 2023

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

@antis81
Copy link
Contributor

antis81 commented Jun 11, 2023

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

@sasq64
Copy link
Owner Author

sasq64 commented Jun 11, 2023

No you can do

!macro Test() {
plot:
    nop
    rts
}

    jsr plot
    rts

    Test()

@antis81
Copy link
Contributor

antis81 commented Jun 12, 2023

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants