BRiX
Advanced Computing Environment
Hosted by SourceForge
brix-os project page

Previous: Garbage Collector ----- Up: Contents ----- Next: Key::Value

Scoping

show notes
Features that need to be added to the page or ideas that haven't been thought out.
  • document how to set and use thread symbols (environment page)

All symbols are declared in a single nested namespace. The root scope is at the top of this namespace and symbols declared here persist for the duration of the module. Constant values, operators, functions, extensions and types are declared here. Module namespaces and symbols exported to root from modules are imported here.

Subscopes allow the compiler to track declared symbols and release them when the scope ends. They may also be used as a barrier preventing accesses to parent scope symbols.

Local Scopes
Local scopes are created for function, extension and types bodies and provide a means to release symbols declared here. All root symbols can be accessed and overriden within the scope. Parent symbols are not visible and may be redeclared within the scope. The $ prefix operator gives access to the immediate parent's symbols when used in child functions.

Transparent Scopes
Used to track and release symbols declared inside extension bodies. Can see the root scope and all parent scopes to the nearest local or isolated scope. Evaluation of function parameter lists uses a transparent scope to override the = and := operators.

Isolated Scopes
Similar to a local scope but also prevents access to the root scope. Used by specialty extensions to provide a clean slate free of any symbols. Enumeration parameters are evaluated in an isolated scope.


The various scopes are hidden away in extensions and only required by programmers developing new extensions. Programmers are however encouraged to make use of Tetra's extensions that manage explicit scopes to increase readability and stability of code and aid the garbage collector.

The UNSET extension undeclares a symbol declared in the current scope. Undeclaring a variable early can prevent unintentional access later on in large blocks of code.

UNSET (name)
  • name -- one or more variable names
	a := 1
	...
	unset(a)
	...
	b := a // undefined 'a'

The LET extension cretes an explicit scope to control the lifetime of variables. UNSET is a poor way to explicitly scope variables and should only be used in the few cases where LET scoping won't work. The = operator is overridden to act as the := declaration operator and each expression must result in a declaration while evaluatiing the declarations.

LET declarations -> body
  • declarations -- unwrapped list of variable declarations
  • -> -- required syntax
  • body -- single or block expression
	let i = 1 -> while(i < 10) echo i
	let x = 1, y:uint = 1 -> {
	  ...
	}

Thread Scope
Each thread has a stack of symbol:object pairs that is seen from all functions dispatched within the thread. Symbols are overridden by pushing a new pair to the stack and popping it when the function exits. Functions that use these symbols require a default value in case the symbol doesn't exist on the stack.

Previous: Garbage Collector ----- Up: Contents ----- Next: Key::Value