|
Code Manifestation -- Strings as Code |
Sheerpower allows strings to contain code. This makes it easy to design and implement your own business-specific scripting languages. The following example uses the eval() function:
The contents of c$ are compiled into Sheerpower bytecode at runtime and then evaluated. The result is stored in 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.
The text is compiled where eval() runs, with no block around it.
Inside a for each, collect cluster or
add cluster block, a field written with no cluster name
(->salary) belongs to that block's cluster in the
program's own lines, but in the text given to eval() it has no
cluster: write the name. Inside for each payroll,
eval('payroll->salary') works and
eval('->salary') raises UNKNOWNVAR, with
->SALARY in _string$.
_integer contains the
low-level exception value. For example, a division by zero with constant
operands eval("5/0") raises EXPRINVALID with _integer set to 3001,
the same value a direct division-by-zero exception would carry. Check
_integer to recover the specific underlying cause.In special cases where greater runtime flexibility is required, Sheerpower supports code manifestation, allowing entire blocks of code to be compiled and executed dynamically at runtime using the execute statement:
The code given to execute is compiled when the statement runs. If it
does not compile, the report prints at once — the same error block a
program's mistake gets, with the execute text as its source line —
and execute raises Expression or statement invalid, which a
when block catches (_string$ says
the code given to EXECUTE did not compile). None of the failed
code runs, and the program goes on:
Output:
Every compile error counts, including the ones found only once the whole
snippet is compiled: a name that is used but never given a value, or a
next with no for.
The code is compiled where execute runs, as a program of its own,
so no block of the program is around it. Inside a for each,
collect cluster or add cluster block, a field
written with no cluster name (->name$) belongs to that
block's cluster in the program's own lines; in the text given to
execute it belongs to a block in that same text, and with none it
does not compile (No implied cluster for ->name$). Write
the cluster's name, or put the whole block in the text:
This runtime compilation capability means Sheerpower can act as both a host language and a code generator. Developers can build domain-specific scripting engines, dynamic rule processors, or business logic layers that evolve without redeployment. It combines the performance of compiled code with the adaptability of scripting — a powerful advantage for systems that need to respond to changing business logic or external data sources in real time.
eval() or
execute is compiled into code that stays in memory.Expression or statement invalid; none of it runs.|
Hide Description
|
|
|
Enter or modify the code below, and then click on RUN |
|
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |