module documentation

The interpreter module implements an extensible lisp-based scripting runtime.

See Also

PContext for the main class to run a script.

Function assertSymbol Assert that the symbol is a list of symbols, and that the list length is within the given arguments: either an exact length, or a minimum lenght and a maximum length.
Function checkScriptTimeout Check for script timeout.
Function evaluateInlineExpressions Replace all inline expressions in a string.
Function executeExpression Recursively execute a symbol as an expression.
Function executeFunction Execute a named function or lambda function.
Function executeSubexpression Execute an expression that is contained in a string. This starts a sub-execution of a new script, but in the same context.
Function executeSymbolWithArguments Execute a symbol with a list of arguments. A symbol can be a function, a lambda function, or another (quoted) symbol.
Function getArgument Verify that an expression is a list and return an argument symbol, while optionally verify the allowed type(s) for that argument.
Function joinExpression Join all symbols in an expression.
Function parseScript Parse and validate a script in a context.
Function resultFromArgument Return the SSymbol result from an argument symbol.
Function runScript Run the script in the PContext instance.
Function valueFromArgument Return the actual value from an argument symbol.
Variable builtinFunctions Dictionary to map the interpreter functions to Python functions.
Function __doProgn Execute a symbol list.
Function _doAll Return True if all of the arguments are True. The arguments can be a list of symbols. The type of all arguments must be Bool.
Function _doAny Return True if any of the arguments is True. The arguments can be a list of symbols. The type of all arguments must be Bool.
Function _doArgv With the argv function one can access the individual arguments of a script.
Function _doAssert Assert a condition. If it fails an exception is raised and script execution interrupted.
Function _doB64Decode Base64-decode a string.
Function _doB64Encode Base64-encode a string.
Function _doBlock Execute a block of expressions.
Function _doBoolean Just set and return a boolean value.
Function _doCar Get the first symbol of a list without changing the list.
Function _doCase The case function implements multiple test-action clauses. It evaluates a symbol and compares the result against multiple action lists based on the evaluation of that symbol.
Function _doCdr The CDR of a list is the rest of the list without the first symbol. The original list is not changed.
Function _doConcatenate Concat symbols together.
Function _doCons The cons function creates a new list out of a first element (the car) and a second element (the cdr).
Function _doDatetime This function returns the the current time (UTC-based) in the specified format.
Function _doDefun This function defines a new function.
Function _doDolist This function executes a code block for each element in a list.
Function _doDotimes This function executes a code block a number of times.
Function _doError End script execution with an error. The optional argument will be assigned as the result of the script (pcontext.result).
Function _doEval Evaluate a list as a function call.
Function _doEvaluateInline Enable or disable inline string evaluation.
Function _doFilter Filter a list based on a condition.
Function _doFset This function defines an alias for a symbol.
Function _doGetJSONAttribute Retrieve an attribute from a JSON structure via a key path.
Function _doHasJSONAttribute Check whether an attribute exists in a JSON structure for a key path.
Function _doIf Check whether an expression evaluates to true and then execute a symbol or list. Otherwise a second, optional symbol or list is executed.
Function _doIn Check whether a symbol is contained in a list, or a string is contained in another string.
Function _doIncDec Increment or decrement a variable by an optional value.
Function _doIndexOf Determine the index of a symbol or string in a list or string.
Function _doIsDefined Check whether a symbol, function, or variable is defined.
Function _doJsonify Escape a string for use in a JSON structure. Newlines and quotes are escaped.
Function _doJsonToString Convert a JSON structure to a string.
Function _doLambda Define a nameless lambda function.
Function _doLength Get the length of a symbol or list.
Function _doLet Perform multipe assignments in sequence (let* function).
Function _doList Create a list out of the arguments
Function _doLog Print a message to the debug or to the error log. Either the internal or a provided log function is used.
Function _doLowerUpper Convert a string to upper or lower case.
Function _doMap Apply a function to each element of a list or multiple lists, accumulating the result.
Function _doMatch Apply a regular expression to a string and return whether it matches.
Function _doMinMax Get the minimum or maximum value of a list of numbers, strings or orher comparables.
Function _doNot Boolean not operation.
Function _doNth Get the nth element from a list, or the nth character from a string. The index is 0-based.
Function _doOperation Process various boolean and mathematical operations.
Function _doParseString Parse a string as executable code and return it for evaluation.
Function _doPrint Print the arguments to the console.
Function _doProgn Evaluate one or multiple symbols in a list. This is the explicite function that many
Function _doQuit End script execution. The optional argument will be assigned as the result of the script in the PContext.result attribute.
Function _doQuote Convert a symbol into a quotable version.
Function _doRandom Generate a random float number in the given range. The default for the range is [0.0, 1.0]. If one argument is given then this indicates a range of [0.0, arg].
Function _doReduce Apply a function to each element of a list or multiple lists, accumulating the result.
Function _doRemoveJSONAttribute Remove an attribute from a JSON structure via its key path.
Function _doReturn Return from a function call or break from while loop.
Function _doReturnFrom Return from a named block.
Function _doReverse Reverse a list or string.
Function _doRound Return a number rounded to optional ndigits precision after the decimal point.
Function _doSetJSONAttribute Set an attribute of a JSON structure via its key path.
Function _doSetq Set a variable. The second symbol is automatically quoted, so it is taken as the variable name.
Function _doSleep Sleep for a number of seconds.
Function _doSlice Return a slice of a string or list.
Function _doStringToJson Convert a string that contains a JSON structure to a JSON dictionary.
Function _doToNumber Convert a string to a number.
Function _doToString Convert any symbol to its string representation.
Function _doToSymbol Convert a string to a symbol.
Function _doUnwindProtect Execute a cleanup form after the main form has been executed or in case of an error.
Function _doURLEncode URL-Encode a string.
Function _doWhile Provide a while loop functionality.
Function _doZip Zip lists together.
Function _utcNow Return the current time, but relative to UTC.
Function _utcTimestamp Return the current time's timestamp, but relative to UTC.
Variable _onErrorFunction Name of the on-error function that is executed in case of an error
def assertSymbol(pcontext: PContext, symbol: SSymbol, length: int = None, minLength: int = None, maxLength: int = None):

Assert that the symbol is a list of symbols, and that the list length is within the given arguments: either an exact length, or a minimum lenght and a maximum length.

All length parameter are optional. If none is given then no length check happens.

Parameters
pcontext:PContextThe PContext object that represents the current script state.
symbol:SSymbolThe SSymbol to check. It must be a list.
length:intOptional exact list length to assert.
minLength:intOptional minimum list length to assert.
maxLength:intOptional maximum list length to assert.
Raises
PInvalidArgumentErrorIn case any assertion fails.
def checkScriptTimeout(pcontext: PContext):

Check for script timeout.

Parameters
pcontext:PContextThe PContext object to check.
Raises
PTimeoutErrorIn case the script timeout is reached.
def evaluateInlineExpressions(pcontext: PContext, symbol: SSymbol) -> PContext:

Replace all inline expressions in a string.

Expressions are replaced recursively.

Parameters
pcontext:PContextUndocumented
symbol:SSymbolThe symbol to execute.
Returns
PContextPContext object that contains as a result the string with all expressions executed.
def executeExpression(pcontext: PContext, symbol: SSymbol) -> PContext:

Recursively execute a symbol as an expression.

Parameters
pcontext:PContextThe PContext object that represents the current script state.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the result.
Raises
PUndefinedErrorIn case a symbol is undefined.
PInvalidArgumentErrorIn case an unexpected symbol is encountered.
def executeFunction(pcontext: PContext, symbol: SSymbol, functionName: str, functionDef: FunctionDefinition | None = None) -> PContext:

Execute a named function or lambda function.

Parameters
pcontext:PContextThe PContext object that represents the current script state.
symbol:SSymbolThe symbol to execute.
functionName:strThe name of the function that is executed. In case of a lambda this name is random.
functionDef:FunctionDefinition | NoneThe executable part of a function or lambda function.
Returns
PContextThe updated PContext object with the function result.
def executeSubexpression(pcontext: PContext, expression: str) -> PContext:

Execute an expression that is contained in a string. This starts a sub-execution of a new script, but in the same context.

Parameters
pcontext:PContextUndocumented
expression:strString with list of symbols to execute.
Returns
PContextPContext object.
Raises
PInvalidArgumentErrorIn case of an error.
def executeSymbolWithArguments(pcontext: PContext, symbol: SSymbol, arguments: SSymbolsList = []) -> PContext:

Execute a symbol with a list of arguments. A symbol can be a function, a lambda function, or another (quoted) symbol.

Parameters
pcontext:PContextThe PContext object that represents the current script state.
symbol:SSymbolThe symbol/function/lambda to execute.
arguments:SSymbolsListList of arguments to pass to the symbol.
Returns
PContextThe updated PContext object with the function result.
def getArgument(pcontext: PContext, symbol: SSymbol, idx: int | None = None, expectedType: SType | tuple[SType, ...] | None = None, doEval: bool | None = True, optional: bool | None = False) -> PContext:

Verify that an expression is a list and return an argument symbol, while optionally verify the allowed type(s) for that argument.

If any of these validations fail, an exception is raised.

This method also assigns a result and error state to self.

Parameters
pcontext:PContextThe PContext object that represents the current script state.
symbol:SSymbolThe symbol that contains an expression.
idx:int | NoneOptional index if the symbol contains a list of symbols.
expectedType:SType | tuple[SType, ...] | Noneone or multiple data types that are allowed for the retrieved argument symbol.
doEval:bool | NoneOptionally recursively evaluate the symbol.
optional:bool | NoneAllow the argument to be None.
Returns
PContextResult PContext object with the result, possible changed variable and other states.
Raises
PInvalidArgumentErrorIn case of an error.
def joinExpression(pcontext: PContext, symbols: SSymbolsList, parentSymbol: SSymbol, sep: str = ' ') -> PContext:

Join all symbols in an expression.

Parameters
pcontext:PContextThe PContext object that represents the current script state.
symbols:SSymbolsListA list of symbols to join.
parentSymbol:SSymbolThe parent symbol that will contain the result.
sep:strAn optional separator for each of the stringified symbols.
Returns
PContextThe updated PContext object with the function result. The PContext.result attribute contains the joint string.
def parseScript(pcontext: PContext) -> bool:

Parse and validate a script in a context.

Parameters
pcontext:PContextThe PContext object that contains the script to parse.
Returns
boolBoolean indicating the success.
def resultFromArgument(pcontext: PContext, symbol: SSymbol, idx: int | None = None, expectedType: SType | tuple[SType, ...] | None = None, doEval: bool | None = True, optional: bool | None = False) -> tuple[PContext, SSymbol]:

Return the SSymbol result from an argument symbol.

Parameters
pcontext:PContextThe PContext object that represents the current script state.
symbol:SSymbolThe symbol that contains an expression.
idx:int | NoneOptional index if the symbol contains a list of symbols.
expectedType:SType | tuple[SType, ...] | Noneone or multiple data types that are allowed for the retrieved argument symbol.
doEval:bool | NoneOptionally recursively evaluate the symbol.
optional:bool | NoneAllow the argument to be optional.
Returns
tuple[PContext, SSymbol]Result tuple of the updated PContext object with the result and the symbol.
def runScript(pcontext: PContext, arguments: list[str] = [], isSubCall: bool | None = False) -> PContext:

Run the script in the PContext instance.

Parameters
pcontext:PContextUndocumented
arguments:list[str]Optional list of string arguments to the script. They are available to the script via the argv function.
isSubCall:bool | NoneOptional indicator whether the script is called from another script.
Returns
PContextPContext object with the result and the termination reason.
def valueFromArgument(pcontext: PContext, symbol: SSymbol, idx: int | None = None, expectedType: SType | tuple[SType, ...] | None = None, doEval: bool | None = True, optional: bool | None = False, default: Any | None = None) -> tuple[PContext, Any]:

Return the actual value from an argument symbol.

Parameters
pcontext:PContextThe PContext object that represents the current script state.
symbol:SSymbolThe symbol that contains an expression.
idx:int | NoneOptional index if the symbol contains a list of symbols.
expectedType:SType | tuple[SType, ...] | Noneone or multiple data types that are allowed for the retrieved argument symbol.
doEval:bool | NoneOptionally recursively evaluate the symbol.
optional:bool | NoneAllow the argument to be optional.
default:Any | NoneOptional default value to return if the argument is not found or is None.
Returns
tuple[PContext, Any]Result tuple of the updated PContext object with the result and the value.
builtinFunctions: PSymbolDict =

Dictionary to map the interpreter functions to Python functions.

def __doProgn(pcontext: PContext, symbol: SSymbol, doEval: bool = True, start: int = 0) -> PContext:

Execute a symbol list.

This function is used internally by the interpreter.

Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
doEval:boolIndicator whether to evaluate the arguments.
start:intThe offset to start the evaluation. This is 1 for the _doProgn function and 0 for the interpreter.
Returns
PContextThe updated PContext object with the function result.
def _doAll(pcontext: PContext, symbol: SSymbol) -> PContext:

Return True if all of the arguments are True. The arguments can be a list of symbols. The type of all arguments must be Bool.

Example

(any true false) -> false
(any true true) -> true
(any '(false false)) -> false
(any '(true true)) -> true
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object. The result is either True or False.
def _doAny(pcontext: PContext, symbol: SSymbol) -> PContext:

Return True if any of the arguments is True. The arguments can be a list of symbols. The type of all arguments must be Bool.

Example

(any true false) -> true
(any false false) -> false
(any '(false false)) -> false
(any '(false true)) -> true
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object. The result is either True or False.
def _doArgv(pcontext: PContext, symbol: SSymbol) -> PContext:

With the argv function one can access the individual arguments of a script.

  • Without an index argument this function returns the whole argument list, including the script name.
  • If the index is 0 then only the script name is returned.
  • Otherwise the nth argument is returned, starting with 1.

Example

(argv) -> The script name and all arguments
(argv 3) -> The third script arguments
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object.
Raises
PInvalidArgumentErrorIn case of an error.
def _doAssert(pcontext: PContext, symbol: SSymbol) -> PContext:

Assert a condition. If it fails an exception is raised and script execution interrupted.

Example

(assert (< x 4))
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object.
Raises
PAssertionFailedIn case the assertion fails.
def _doB64Decode(pcontext: PContext, symbol: SSymbol) -> PContext:

Base64-decode a string.

Example

(base64-decode "SGVsbG8gV29ybGQ=") -> "Hello, World"
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object. The result includes the decoded string.
def _doB64Encode(pcontext: PContext, symbol: SSymbol) -> PContext:

Base64-encode a string.

Example

(base64-encode "Hello, World") -> SGVsbG8gV29ybGQ=
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object. The result includes the encoded string.
def _doBlock(pcontext: PContext, symbol: SSymbol) -> PContext:

Execute a block of expressions.

Example

;; Prints "Hello" and "World". The result is "World".
(block aSymbol
        (print "Hello")
        (print "World"))


;; Prints only "Hello". The result is 42.
(block aSymbol
        (print "Hello")
        (return-from aSymbol 42)
        (print "World"))
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object. The result is the result of the last executed expression in the block, or the result of a matching return-from expression.
def _doBoolean(pcontext: PContext, symbol: SSymbol, value: bool) -> PContext:

Just set and return a boolean value.

Example

(true)
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
value:boolThe actual boolean value.
Returns
PContextThe updated PContext object. The result includes either True or False.
def _doCar(pcontext: PContext, symbol: SSymbol) -> PContext:

Get the first symbol of a list without changing the list.

Example

(car (1 2 3)) -> 1
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object. The result is a list's first symbol or NIL.
def _doCase(pcontext: PContext, symbol: SSymbol) -> PContext:

The case function implements multiple test-action clauses. It evaluates a symbol and compares the result against multiple action lists based on the evaluation of that symbol.

The special symbol otherwise is the default case if no other action list matches the symbol.

Example

(case aSymbol
        ( 1 (print "Result: 1"))
        ( 2 (print "Result: 2"))
        (otherwise (print "Result: something else")))
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object. The result result of the last executed action list, or NIL if nothing was matched and the otherwise symbol is not present.
def _doCdr(pcontext: PContext, symbol: SSymbol) -> PContext:

The CDR of a list is the rest of the list without the first symbol. The original list is not changed.

Example

(cdr '(1 2 3)) -> (2 3)
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object. The result is the rest of the list without the first symbol or NIL.
def _doConcatenate(pcontext: PContext, symbol: SSymbol) -> PContext:

Concat symbols together.

Though this function is mainly for concatenation strings, all types are supported. No space is added between the symbols, but the special symbol sp can be used to add a space character.

Example

(. "Hello" sp "world")
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object. The result is a new string.
def _doCons(pcontext: PContext, symbol: SSymbol) -> PContext:

The cons function creates a new list out of a first element (the car) and a second element (the cdr).

It can be seens as the reverse of a car and a cdr function calls.

Example

(cons "a" "b") -> ("a" "b")
(cons "a" ("b" "c")) -> ("a" "b" "c")
(cons ("a" "b") "c") -> (("a" "b") "c")
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object. The result is a new list.
def _doDatetime(pcontext: PContext, symbol: SSymbol) -> PContext:

This function returns the the current time (UTC-based) in the specified format.

This function has an optional format paramater that is the same as the Python's strftime* function. The default is %Y%m%dT%H%M%S.%f, which evaluates to an ISO8901 timestamp.

Example

(datetime) -> 20220107T221625.771604
(datetime "%H:%M") -> 13:31
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object. The result is a date time string.
def _doDefun(pcontext: PContext, symbol: SSymbol) -> PContext:

This function defines a new function.

A new function may have zero, one or multiple arguments. The result of the last executed expression in the function determines the function's result.

Example

(defun hello (name) (print (. "hello" sp name))) ;; define the function
(hello "Arthur") ;; call the function
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute. The function definition.
Returns
PContextThe updated PContext object.
Raises
PInvalidArgumentErrorIn case of a wrong definition.
def _doDolist(pcontext: PContext, symbol: SSymbol) -> PContext:

This function executes a code block for each element in a list.

The first argument is a list that contains the loop variable symbol and the list to loop over. An optional third argument is the result variable for the loop. The second argument is the code block to execute.

Example

(dolist (i (1 2 3)) (print i))
(dolist (i (1 2 3) result) (setq (+ result i))
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object. The result is the last executed code block or NIL.
def _doDotimes(pcontext: PContext, symbol: SSymbol) -> PContext:

This function executes a code block a number of times.

The first argument is a list that contains the loop counter symbol and the loop limit. An optional third argument is the result variable for the loop. The second argument is the code block to execute.

Example

(dotimes (i 10) (print i))
(dotimes (i 10 result) (setq result i))
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object. The result
def _doError(pcontext: PContext, symbol: SSymbol) -> PContext:

End script execution with an error. The optional argument will be assigned as the result of the script (pcontext.result).

Example

(quit-with-error "Some error")
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object.
Raises
PInvalidArgumentErrorIn case of an invalid argument.
PQuitWithErrorIn case the function quits successfully with an error. This is expected.
def _doEval(pcontext: PContext, symbol: SSymbol) -> PContext:

Evaluate a list as a function call.

Example

(eval '(print "Hello, world"))
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object.
def _doEvaluateInline(pcontext: PContext, symbol: SSymbol) -> PContext:

Enable or disable inline string evaluation.

Example

(evaluate-inline false) ;; Disable inline evaluation
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object.
def _doFilter(pcontext: PContext, symbol: SSymbol) -> PContext:

Filter a list based on a condition.

Example

(filter (lambda (x) (< x 3)) (1 2 3 4 5)) -> (1 2)
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object. The result is a new list.
def _doFset(pcontext: PContext, symbol: SSymbol) -> PContext:

This function defines an alias for a symbol.

An alias is a new symbol that points to an existing symbol. Aliased symbols can be aliases themselves.

The symbols must be quoted.

If the second symbold is not provided, then the alias is removed.

Example

(fset 'aSymbol 'anotherSymbol)
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object.
Raises
PInvalidArgumentErrorIn case of an error.
def _doGetJSONAttribute(pcontext: PContext, symbol: SSymbol) -> PContext:

Retrieve an attribute from a JSON structure via a key path.

Example

(get-json-attribute { "a" : { "b" : "c" }} "a/b" ) -> "c"
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result.
def _doHasJSONAttribute(pcontext: PContext, symbol: SSymbol) -> PContext:

Check whether an attribute exists in a JSON structure for a key path.

Example

(has-json-attribute { "a" : { "b" : "c" }} "a/b" ) -> true
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result.
def _doIf(pcontext: PContext, symbol: SSymbol) -> PContext:

Check whether an expression evaluates to true and then execute a symbol or list. Otherwise a second, optional symbol or list is executed.

Example

(if (< 1 2)
        (print "true")
        (print "false") )
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result of the last operation in the executed list or symbol.
def _doIn(pcontext: PContext, symbol: SSymbol) -> PContext:

Check whether a symbol is contained in a list, or a string is contained in another string.

Example

(in "Hello" "Hello, World") -> true
(in a (b c)) -> false
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result.
def _doIncDec(pcontext: PContext, symbol: SSymbol, isInc: bool | None = True) -> PContext:

Increment or decrement a variable by an optional value.

The default is 1.

Example

(setq a 1)
(inc a 2) -> 3
(dec a) -> 2
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
isInc:bool | NoneIndicate whether to increment or decrement.
Returns
PContextThe updated PContext object with the function result.
Raises
PInvalidArgumentErrorIn case the variable is not defined, or the second argument doesn't evaluate to a number.
def _doIndexOf(pcontext: PContext, symbol: SSymbol) -> PContext:

Determine the index of a symbol or string in a list or string.

If the second argument is a string, then the first argument must also be a string.

Example

(index-of 1 '(1 2 3)) -> 0
(index-of "a" '("b", "c", "d")) -> nil
(index-of "b" "abc") -> 1
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
Raises
PInvalidTypeErrorIn case the first argument is not a string if the second argument is a string.
def _doIsDefined(pcontext: PContext, symbol: SSymbol) -> PContext:

Check whether a symbol, function, or variable is defined.

Example

(setq a 1)
(is-defined a) -> true
(is-defined b) -> false
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result.
def _doJsonify(pcontext: PContext, symbol: SSymbol) -> PContext:

Escape a string for use in a JSON structure. Newlines and quotes are escaped.

Example

(jsonify "Hello,
World") -> "Hello\nWorld"
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the escaped JSON string.
def _doJsonToString(pcontext: PContext, symbol: SSymbol) -> PContext:

Convert a JSON structure to a string.

Example

(json-to-string { "a": 1 }) -> "{ "a": 1 }"
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doLambda(pcontext: PContext, symbol: SSymbol) -> PContext:

Define a nameless lambda function.

Example

((lambda (x) (* x x)) 5) -> 25
(setq y (lambda (x) (* x x)))
((y) 5) -> 25
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the lambda result.
Raises
PInvalidArgumentErrorin case one of the arguments has the wrong type.
def _doLength(pcontext: PContext, symbol: SSymbol) -> PContext:

Get the length of a symbol or list.

Example

(length "Hello, World") -> 11
(length (a b)) -> 2
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doLet(pcontext: PContext, symbol: SSymbol, sequential: bool = True) -> PContext:

Perform multipe assignments in sequence (let* function).

Note

Currently, assignment in parallel (let function) is not supported.

Example

(let* (a 1)
          (a (+ a 1))) -> a = 2
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
sequential:boolIndicator whether sequential (True) or parallel assignments shall be performed.
Returns
PContextThe updated PContext object with the function result and set variables.
Raises
PInvalidArgumentErrorIn case the format of the variable assignment is wrong.
def _doList(pcontext: PContext, symbol: SSymbol) -> PContext:

Create a list out of the arguments

Example

(list 1 2 3) -> (1 2 3)
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doLog(pcontext: PContext, symbol: SSymbol, isError: bool | None = False, exception: Exception | None = None) -> PContext:

Print a message to the debug or to the error log. Either the internal or a provided log function is used.

Example

(log "Hello, World")
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
isError:bool | NoneIndicate whether this message will be logged as an error or a normal log message.
exception:Exception | NoneAn optional exception
Returns
PContextThe updated PContext object with the function result.
def _doLowerUpper(pcontext: PContext, symbol: SSymbol, toLower: bool = True) -> PContext:

Convert a string to upper or lower case.

Example

(upper "Hello, World") -> "HELLO, WORLD"
(lower "Hello, World") -> "hello, world
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
toLower:boolIndicator whether the conversion shall be to lower or upper case.
Returns
PContextThe updated PContext object with the function result.
def _doMap(pcontext: PContext, symbol: SSymbol) -> PContext:

Apply a function to each element of a list or multiple lists, accumulating the result.

Example

(map (lambda (x y) (+ x y)) '(1 2 3) '(4 6 7)) -> (5 8 10)
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doMatch(pcontext: PContext, symbol: SSymbol) -> PContext:

Apply a regular expression to a string and return whether it matches.

Note

The match function must be supplied to the interpreter.

Example

(match "aa" "a?") -> true
(match "aa" "b*") -> false
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doMinMax(pcontext: PContext, symbol: SSymbol, doMax: bool | None = True) -> PContext:

Get the minimum or maximum value of a list of numbers, strings or orher comparables.

Example

(min (1 2 3)) -> 1
(min 1 2) -> 1
(max (1 2 3)) -> 3
(max 1 2) -> 2
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
doMax:bool | NoneIndicator whether to get the maximum (True) or minimum (False) value.
Returns
PContextThe updated PContext object with the function result.
def _doNot(pcontext: PContext, symbol: SSymbol) -> PContext:

Boolean not operation.

Example

(not true) -> false
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doNth(pcontext: PContext, symbol: SSymbol) -> PContext:

Get the nth element from a list, or the nth character from a string. The index is 0-based.

Example

(nth 2 '(1 2 3)) -> 3
(nth 2 "Hello, World") -> "l"
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doOperation(pcontext: PContext, symbol: SSymbol, op: Callable, tp: SType) -> PContext:

Process various boolean and mathematical operations.

The supported operations are:

  • "<": smaller
  • "<=": smaller or equal
  • ">": greater
  • ">=": greater or equal
  • "==": equal
  • "!=": unequal
  • "or": boolean or
  • "|": boolean or
  • "and": boolean and
  • "&": boolean and
  • "!": boolean not
  • "+": Addition
  • "-": Substraction
  • "*": Multiplication
  • "/": Division
  • "//": Division, rounding down and returning an integer number
  • "**": Power
  • "%": Modulo

The mathematical operations may have more then two arguments.

Example

(+ 1 2) -> 3
(+ 1 2 3) -> 6
(< 1 2) -> true
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
op:CallableThe operation to use.
tp:STypeThe expected result's data type.
Returns
PContextThe updated PContext object with the operation result.
Raises
PDivisionByZeroErrorIn case a division by 0 happens.
PInvalidTypeErrorIn case one or more invalid types are provided for the operation.
def _doParseString(pcontext: PContext, symbol: SSymbol) -> PContext:

Parse a string as executable code and return it for evaluation.

Example

(eval (parse-string "(print "hello, world")")) -> prints "hello, world"
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe PContext object with the new symbol as result.
def _doPrint(pcontext: PContext, symbol: SSymbol) -> PContext:

Print the arguments to the console.

The function return the resulting string as a symbol.

Example

(print "Hello, World")
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doProgn(pcontext: PContext, symbol: SSymbol, doEval: bool = True) -> PContext:

Evaluate one or multiple symbols in a list. This is the explicite function that many

Example

(progn (print "Hello, World"))
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
doEval:boolIndicator whether to evaluate the arguments.
Returns
PContextThe updated PContext object with the function result.
Raises
PInvalidArgumentErrorIn case of an invalid argument or parameter.
def _doQuit(pcontext: PContext, symbol: SSymbol) -> PContext:

End script execution. The optional argument will be assigned as the result of the script in the PContext.result attribute.

Example

(quit "some result") -> "some result"
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThis function doesn't return anything, it always raises an exception.
Raises
PQuitRegularAlways raises this exception.
def _doQuote(pcontext: PContext, symbol: SSymbol) -> PContext:

Convert a symbol into a quotable version.

Example

(quote (a b c)) -> '(a b c)
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doRandom(pcontext: PContext, symbol: SSymbol) -> PContext:

Generate a random float number in the given range. The default for the range is [0.0, 1.0]. If one argument is given then this indicates a range of [0.0, arg].

Example

(random 1) -> 0.3
(random 2 3) -> 2.87
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doReduce(pcontext: PContext, symbol: SSymbol) -> PContext:

Apply a function to each element of a list or multiple lists, accumulating the result.

Example

(reduce (lambda (x y) (+ x y)) '(1 2 3 4 5)) -> 15
(reduce '+ '(1 2 3 4 5) 10) -> 25
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doRemoveJSONAttribute(pcontext: PContext, symbol: SSymbol) -> PContext:

Remove an attribute from a JSON structure via its key path.

One may remove multiple attributes at once by providing multiple key paths.

Example

(remove-json-attribute { "a" : { "b" : "c" }} "a/b") -> { "a" : {} }
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result, ie a new JSON symbol.
def _doReturn(pcontext: PContext, symbol: SSymbol) -> PContext:

Return from a function call or break from while loop.

While returning it is possible to pass a return value.

Example

(return "some result") -> "some result"
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doReturnFrom(pcontext: PContext, symbol: SSymbol):

Return from a named block.

Example

(block "aBlock" 1 (return-from "aBlock" 2) 3) -> 2
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
The updated PContext object with the function result.
Raises
PReturnFromAlways raises this exception.
def _doReverse(pcontext: PContext, symbol: SSymbol) -> PContext:

Reverse a list or string.

Example

(reverse '(1 2 3)) -> (3 2 1)
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doRound(pcontext: PContext, symbol: SSymbol) -> PContext:

Return a number rounded to optional ndigits precision after the decimal point.

If ndigits, the second parameter, is omitted, it returns the nearest integer.

Example

(round 1.6) -> 2
(round 1.678 2) -> 1.67
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
Raises
PInvalidArgumentErrorIn case of an invalid argument.
def _doSetJSONAttribute(pcontext: PContext, symbol: SSymbol) -> PContext:

Set an attribute of a JSON structure via its key path.

One may set multiple values at one by providing a list of (key/values).

Example

(set-json-attribute { "a" : { "b" : "c" }} "a/b" "d") -> { "a" : { "b" : "d" }
(set-json-attribute { "a" : { "b" : "c" }} '('("a/b" "d") '("a/c" "e"))) -> { "a" : { "b" : "d", "c" : "e"}
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result, ie a new JSON symbol.
def _doSetq(pcontext: PContext, symbol: SSymbol) -> PContext:

Set a variable. The second symbol is automatically quoted, so it is taken as the variable name.

Example

(setq a "Hello, World") -> "Hello, World"
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result, ie the expression result.
def _doSleep(pcontext: PContext, symbol: SSymbol) -> PContext:

Sleep for a number of seconds.

This function can be interrupted when the script's state is set to any other state than running.

Example

(sleep 2.5)
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result.
Raises
PNotANumberErrorIn case there is a problem with the number conversion.
PInterruptedErrorIn case the sleep was interrupted.
def _doSlice(pcontext: PContext, symbol: SSymbol) -> PContext:

Return a slice of a string or list.

The behaviour is the same as slicing in Python, except that both start and end must be provided.

The first argument is the beginning of the slice, the second is the end (exlcuding) of the slice. The fourth argument is the list or string to slice.

Example

(slice 1 2 '(1 2 3)) -> (2)
(slice 0 -1 "abcde") -> "abcd"
(slice 99 100 '(1 2 3)) -> ()
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doStringToJson(pcontext: PContext, symbol: SSymbol) -> PContext:

Convert a string that contains a JSON structure to a JSON dictionary.

Example

(string-to-json "{ "a": "b"")
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result.
Raises
PInvalidArgumentErrorIn case the JSON input contains an error.
def _doToNumber(pcontext: PContext, symbol: SSymbol) -> PContext:

Convert a string to a number.

Example

(string-to-number "1") -> 1
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result.
Raises
PInvalidArgumentErrorIn case the input cannot be converted.
def _doToString(pcontext: PContext, symbol: SSymbol) -> PContext:

Convert any symbol to its string representation.

Example

(to-string aSymbol) -> "[1, 2, 3]"
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result.
Raises
PInvalidArgumentErrorIn case the input cannot be converted.
def _doToSymbol(pcontext: PContext, symbol: SSymbol) -> PContext:

Convert a string to a symbol.

Example

(to-symbol "aSymbol") -> aSymbol
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result.
Raises
PInvalidArgumentErrorIn case the input cannot be converted.
def _doUnwindProtect(pcontext: PContext, symbol: SSymbol) -> PContext:

Execute a cleanup form after the main form has been executed or in case of an error.

Currently only programmatic flow interrupts are supported to trigger the cleanup form: assert, quit, quit-with-error, return, return-from.

Example

(unwind-protect
        (print "main form")
        (print "cleanup form"))
Parameters
pcontext:PContextCurrent PContext for the script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the function result.
def _doURLEncode(pcontext: PContext, symbol: SSymbol) -> PContext:

URL-Encode a string.

Example

(url-encode "Hello, World") -> "Hello+World"
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result.
def _doWhile(pcontext: PContext, symbol: SSymbol) -> PContext:

Provide a while loop functionality.

Example

(setq i 0)
(while (< i 10)
        ((print i)
                (inc i)))
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result, ie the result of the evaluated expression or a return statement in the loop.
def _doZip(pcontext: PContext, symbol: SSymbol) -> PContext:

Zip lists together.

Example

(zip '(1 2 3) '(4 5 6)) -> ((1 4) (2 5) (3 6))
Parameters
pcontext:PContextPContext object of the running script.
symbol:SSymbolThe symbol to execute.
Returns
PContextThe updated PContext object with the operation result.
def _utcNow() -> datetime:

Return the current time, but relative to UTC.

Returns
datetimeDatetime UTC-based timestamp
def _utcTimestamp() -> float:

Return the current time's timestamp, but relative to UTC.

Returns
floatFloat UTC-based timestamp
_onErrorFunction: str =

Name of the on-error function that is executed in case of an error