|
J.2 Strings as Code
|
sheerpower allows strings to contain code. This makes it easy to design and
implement your own business-specific scripting languages. This simple example
uses the
eval() function:
a = 45
b = 10
c$ = 'a*b + pi'
declare dynamic d
d = eval(c$)
print d
The content of
c$ is compiled into sheerpower byte code at runtime and
then evaluated. The result is stored into
D. Because the
eval()
function can return any data type, depending on the string that was passed
into it,
D must be declared as a dynamic variable.
In special cases where a lot of code flexibility is required, sheerpower
supports "code manifestation" where entire blocks of code are compiled and
executed at runtime. This is done using the
execute statement:
suffix$ = '_temp'
code$ = 'for i'+suffix$ + '= 1 to 10\' +
'print i'+suffix$ + '\next i'+suffix$
execute code$
execute 'print i_temp'
Note: The use of execute can make your code harder to maintain because
the actual code does not exist until runtime. It should only be used in
special cases where it is the best solution.