Skip to content
Bananattack edited this page Aug 17, 2011 · 5 revisions

The Language Reference - Scoping

Sometimes it is useful to define a private scope for symbol definitions. Often you want to reuse the name of a previously declared symbol, and it can be convenient to not have to prefix names to prevent name duplicates. Perhaps you are making a few labels which you only really care to use in a subroutine. At any rate, there is a scoping block for just that, using begin and end to delimit it.

begin / end creates a new scope for the symbols, allowing access to those newly-defined symbols only from inside its block.

A scope effectively makes symbols "local" to the block -- although, things like the ram counter and rom counter are still advanced within this block. This does not imply any sort of stack in the actual runtime code, and simply creates a private symbol block.

Scopes get access to all locals in their parent scope, but new definitions inside the block stay private to that block, and definitions will shadow old symbols from its parent scope.

Example

Here are some cheesy examples of scoping blocks:

let x = 10
let y = 13
begin
    let x = y // Reusing the name x, and accessing the symbol y which is in a parent scope.
end

// Using begin/end for keeping subroutine-local labels.
def main: begin
    def loop: begin
        goto loop
    end
end
Clone this wiki locally