|
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.
_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.
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. |