Script expressions
Last modified by WikiBot on 2025/03/20 14:15
Script expressions
Script expressions are the expressions used as arguments of script commands and source forms.
A script expression may represent a numerical or symbolic value, an array, a string, an instance of a data object, or a Lisp form. In the following tables, we describe the interpretation of script expressions.
Atomic (non-list) expressions
- Quantity (parameter or variable)
- Either the value of the quantity or the instance of the object, depending on the context.
- Vector or array (except string)
- Every element is evaluated separately
- Symbol
- If the symbol denotes a local or a global variable, it is evaluated to the value of that variable. Otherwise, the symbol is evaluated to itself.
- Any other atom
- The expression is evaluated to itself (i.e., remains unchanged after evaluation).
List expressions
The first element of the list is interpreted as an operation. Other members are treated as arguments.
Here is the list of operations:
- Arithmetical and logical operations:
+ - * / i* i/ = > < >= <= /= == AND OR NOT IF - The arguments are evaluated (except for the operations AND, OR, IF, when only the required number of arguments is evaluated) with type checking. The operation is performed. For * and /, a non-integer result is rounded to 4 significant digits. Operation i* and i/ are multiplication and division of integers. The operation == is used to compare any arguments, while = can compare numbers only.
- :WITH
- The first argument is ((var value) ... ); the second argument is an expression. The operation temporary binds the named local variables to the corresponding values (all values are evaluated before binding), evaluates and returns the expression.
- :FOR
- The first argument is (varname value-list [options]), the second is an expression. The expression is evaluated repeatedly, while the varname is substituted with value from value-list. The result of expression is obtained from evaluated values as indicated by option :collect:
| list | The list of values. This is the default action; |
| filter | The list of non-NIL values; |
| vector | The vector of values; |
| filter-vector | The vector of non-NIL values; |
| sum | The sum of values; |
| product | The product of values; |
| max | The maximal non-NIL value; |
| min | The minimal non-NIL value; |
| range | The list of the minimal and the maximal non-NIL value; |
| first | The first evaluated value (the subsequent values are not evaluated); |
| filter-first | The first evaluated non-NIL value (the subsequent values are not evaluated); |
| last | The last evaluated value; |
| filter-last | The last evaluated non-NIL value; |
| append | The concatenation of all values (that should be lists); |
| append | The union of all values (that should be lists) with duplicated removed; |
See script command :FOR for other options.
- SUM
- This operation is obsolete. Use :SUM instead.
- The first argument is (index min max) or (index max), the second is an expression. The expression is evaluated repeatedly while the index variable is substituted with value from min (or 1) to max, and the sum of the results is calculated. The min and max are also calculated.
- :SUM
- The first argument is (varname value-list [options]), the second is an expression. The expression is evaluated repeatedly while the varname is substituted with value from value-list, and the sum of the results is calculated.
See script command :FOR for options.
- :RANGE
- (:range [from] to) is evaluated to list (from from+1 … to). Default from 1.
(:range from to step) is evaluated to list (from from+step … to).
All arguments are evaluated.
- :FORMAT
- The first argument is a format string, the rest are arguments to the format. All arguments are evaluated.
- CALL
:CALL - The first argument is a function name, the rest are arguments to the function. All arguments are evaluated. Unlike the :CALL script command, this :CALL cannot be omitted.
- :VECTOR
- An 1-dimensional array consisting of the (evaluated) arguments
- :ARRAY
- Builds an n-dimensional array with the given size and formula for members.
- The first argument is a list of (dim_1 ... dim_n), where each of dim_i is either the size of the array in i-th direction, or a list (index size). The index should be a symbol, the size should be an expression with an integer value.
- The second argument is an expression for the member of the array. It may depend on the indices.
- QUOTE
- Returns the first argument with only the subexpressions (:eval arg) evaluated.
- :QUOTE
- Evaluates the first argument and returns (QUOTE result).
- :EVAL
- Evaluates and returns the first argument, even if it is a part of a quoted expression.
- :COND
- Each argument (named clause) is a list of two elements, condition and value. The conditions of each clause are evaluated in turn. If the result is :False or NIL, the clause is ignored. Otherwise, the value of the clause is evaluated as a script expression and returned as the value of the whole expression. The remaining clauses are ignored.
- :CASE
- The first argument is a key; the other arguments are clauses. Each clause is a list of two elements, selector and value. The key is evaluated and the result is compared in turn with selectors until a match is found. The value in the selected clause is evaluated as a script expression and returned as the expression value. Other clauses are ignored.
- An expression matches a selector if either (1) they are identical, (2) the selector is a list and the expression is identical to a member of this list, or (3) the selector is the symbol otherwise.
- The selectors are not evaluated.
- :FORM
- Evaluates and returns the list of arguments as a source form.
- :LIST
- Evaluates and returns the list of arguments as contents of a source form (i.e. as a source form without header)
- :$
- Describes a computed or language-dependent text.
- The list of arguments is evaluated as a single script expression and the result is interpreted as a language-dependent text.
- @ (may be also written as [@ ... ] instead of (@ ... ))
- The list of (evaluated) arguments is interpreted as a path from the base to some object or object’s slot. This object (or the slot value) is returned as the result of the evaluation.
If the first argument after @ is a data object, it is used instead of the base.
See References to data objects and attributes for details.
- @@ or using brackets [... ] instead of ( ... )
- The list of (evaluated) arguments is interpreted as path from the base to some parameter or variable (or an element of an array of ditto). The expression is evaluated to the value of the parameter or variable.
If the first argument after @@ is a data object, it is used instead of base.
See References to data objects and attributes for details.
- ?
- Tries to evaluate arguments without error messages and returns the result of the first successful evaluation, or NIL if all evaluations failed.
- !
- Evaluates the argument without suppressing error messages. (! expr) is equivalent to expr if used outside of (? …) expression.
- any other value (e.g., a number)
- Interpreted as a list of expressions. The members of the list are evaluated.