bass
bass copied to clipboard
Sub label scopes
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.
}
}
}
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.
}
}
}
Except then macros could no longer define labels, which may be something you want.
Defining (global) labels in macros will break the macro scoping anyway right? Other could you maybe write a small example/test?
No you can do
!macro Test() {
plot:
nop
rts
}
jsr plot
rts
Test()
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()