|
H.1 Routines and Variable Scoping
|
Sheerpower provides a number of ways to organize your code. This becomes very
important as your programs grow larger. Programs of over 500,000 lines of
code have been written in Sheerpower. These, by the way, compile, link, and
run within three seconds of saving a coding change.
A number of types of routines are provided by Sheerpower:
- Global routines -- the default routine type; all variables are global to the program.
- Private routines -- all variables are private to the routine.
- Scoped routines -- just like private routines except all variables are reset upon entry or exit.
- Local routines -- acquire the scoping of the private routine that called them.
Used for the rapid abstraction of large routines into a series of smaller ones.
Routine names must start with a letter, include at least one underscore "_",
and can contain any number of other letters and digits. By default, all
routines are global unless specified otherwise.
When talking about routines, we have to also talk about "variable scoping."
Scoping determines which variables can be used and where. Variables outside
of any routine are called global or main variables. From outside of a
routine, they can be accessed using just their name. They can also be
accessed from within a global routine the same way. However, from within a
private routine, they must be accessed by prefixing their name with MAIN$. We
call this their "fully-qualified name." For example:
x = 45
abc = 999
tax = 9876
routine do_it
print x // this will print 45
print main$x // this will also print 45
end routine
private routine here_we_go
x = 101
print x // this will print 101
print main$x // this will print 45
end routine
my_private
my_regular
private routine my_private
abc=123
local do_it_local
end routine
local routine do_it_local
xyz$ = 'is xyz'
assert abc=123
assert xyz$='is xyz'
end routine
routine my_regular: private abc, tax
abc = 456
tax = 99
local do_it_2
print 'In '; _routine
debug show abc, my_regular$abc, main$abc, tax, my_regular$tax
end routine
local routine do_it_2
my_tax = 45
end routine
The fully qualified name of the "x" in the
here_we_go private routine
is
here_we_go$x. Using this suffix method is how Sheerpower keeps
track of the scope of variables. It also makes it easy for you to keep track
of the scoping of variables.
Sheerpower also supports
include directives and
modules.
Note: Sheerpower supports recursive routines through the use of
memorization techniques. Avoiding traditional recursion gives Sheerpower a significant
performance boost -- no data stack to build or tear down. It also makes hacking
Sheerpower code virtually impossible using traditional hacking methods.