4.3. Scoping

You can limit the scope of your variables and labels by defining a user defined scope. This is done by {..}. Everything between the brackets is defined in a local scope and can't be seen from the outside.

Function1: {
        .var length = 10
        ldx #0
        lda #0
loop:   sta table1,x
        inx
        cpx #length
        bne loop
}

Function2: {
        .var length = 20 // doesn’t collide with the previous ‘length’
        ldx #0
        lda #0
loop:   sta table2,x     // the label doesn’t collide with the previous ‘loop’ 
        inx
        cpx #length
        bne loop
}

Scopes can be nested as many times as you wish as demonstrated by the following program:

.var x = 10
{
    .var x=20
    {
        .print "X in 2nd level scope read from 3rd level scope is " + x 
        .var x=30
        .print "X in 3rd level scope is " + x 
    }
    .print "X in 2nd level scope is " + x 
}
.print "X in first level scope is " + x

The output of this is:

X in 2nd level scope read from 3rd level scope is 20.0
X in 3rd level scope is 30.0
X in 2nd level scope is 20.0
X in first level scope is 10.0