debug

assert, trace


assert

Syntax

assert(?testResult|>testMe, ['description'])

Description

Asserts are used to check for programming errors in run-time. Until you run "debug.pika", asserts are disabled which means this function will do absolutely nothing (it is defined as an empty function in "stdlib.pika"). Running "debug.pika" will enable asserts by redefining this function. When enabled, it either checks the boolean ?testResult or executes >testMe and checks the result.

Two things differ depending on the choice of passing a boolean or a function / lambda expression. If you pass a boolean, e.g. assert(myfunc() == 3), and assertions are disabled, the argument will still be evaluated, i.e. myfunc() will still be called. Furthermore, if 'description' is omitted, the exception on an assertion failure will simply contain 'true' or 'false'.

If you pass a function / lambda expression, e.g. assert(>myfunc() == 3), the argument will only be evaluated if assertions are enabled and the exception will contain the full expression. In most cases you will want to use this variant.

Examples

assert(>(0 <= $0 <= 100))
assert(alwaysCallMe(), 'alwaysCallMe() failed miserably')

<trace>

Syntax

trace([>tracer], [+level = 2])

Description

Sets or resets the tracer function. The tracer function is called at various points in the PikaScript interpreter code. For example, you can use it for tracing caught exceptions, function calls and even implement debuggers and profilers.

+level ranges from 0 (no tracing) to 21 (maximum tracing) and determines which events that will trigger a callback. The default trace level of 2 calls the trace function on each function entry and exit. Other useful levels are 1 for tracing caught errors and 3 to trace every interpreted statement. (Please see "PikaScriptImpl.h" for a complete listing of all levels.)

The >tracer function will be called with the following arguments:

$0 = the source code being executed
$1 = the offset into the source code
$2 = whether $3 is an lvalue or an rvalue (lvalue = identifier, rvalue = actual value)
$3 = the result from the last operation
$4 = trace level
$5 = false for "entry", true for "exit" (e.g. function call entry / exit).

Call trace() without arguments to stop all tracing.

Examples

trace()
trace(function { print((if ($5) '} ' else '{ ') # if (exists(@^$callee)) ^$callee else '????') })