Skip to content

Special Variables

Silvio Mayolo edited this page Jan 25, 2018 · 9 revisions

Special Variables

In ShinyLisp, most characters can be used in variable names. Any ASCII character that is not considered "special" or "uppercase" to the language can be used in variable names. If you need to use a traditionally uppercase character in a variable or symbol name, you may do so by preceding it with a backslash. The following are special characters and cannot be used in variable names.

  • 0123456789 - The digits are always parsed as numbers and cannot be used anywhere in variable names. This is to allow chains to include numeric literals.
  • ()[]{} - Parentheses and other brackets are used for special syntactic list-like constructs.
  • :~ - Colon is used to force uppercase status and tilde is used to force the end of a list.
  • '" - Single quotes are used to quote symbols, and double quotes are used to quote strings.
  • \t\n\r - Whitespace characters can never be in variable names, as they are always parsed as whitespace.
  • . - The dot is used for literal cons cell construction and may in the future be used as a decimal point.
  • \ - A backslash is used as a negative sign for numeric literals, since the minus sign is used as a word separator in function names. Backslashes can also precede uppercase characters to treat them as lowercase.

By default, variables in ShinyLisp are local to the function in which they are defined. The = operator will assign variables, defining a new variable in the current scope as necessary. Variable bindings are always resolved dynamically. The variable % and any variable beginning with the # character are declared implicitly global. Unless they are explicitly defined with define, they will always be declared globally, no matter where they are first assigned.

The Printer Variables (#, and #,,)

The variable #, is used when puts (or similar) needs to output a list and is treated as the list delimiter. The default value for this variable is " ", a single space. Less frequently, one might need to modify #,,, which is the dot delimiter. It's default value is " . ", a dot surrounded by spaces, and is used when a dotted list is encountered in puts.

As an example, suppose #, has the value " A " and #,, has the value " B ". Then the expression (puts '(a b c . d)) will print out a A b A c B d.

Implicit Output (%)

In a script file (but not in the REPL), if the script terminates normally, the value of the % variable is implicitly printed at the end of execution, if a variable with that name exists. This is to make it more convenient to write scripts which do some work and then prints the result, as the final print statement can be omitted in this case.

The Loop Continue Value (&&l)

The special loop continue value is used when a loop is about to jump back to its beginning. If &&l exists and is a falsy value, the loop will instead exit and return an appropriate value.

Arguments (!!, x, y, z, ..., r!, rx, ry, rz, ...)

The variable with the name !! always refers to the argument list of the immediately enclosing function. For convenience, each argument is also provided separately. The first three arguments are x, y, then z, followed by xx, yy, and zz. If more than six arguments are needed, the variables xxa, yya, zza xxb, yyb, zzb, xxc, yyc, zzc ... xxz, yyz, zzz are bound to the next arguments. If, inexplicably, you need more than 84 arguments, the pattern continues with xxaa, yyaa, zzaa, xxab, yyab, zzab, ..., but if you are using a variable called xxadz as an argument, it might be appropriate to reconsider your approach. Note that at the top level of the program, the argument variables will be bound to the command line arguments.

Similarly, when a regex match or substitution is performed, the variable r! is bound to the list of subgroup captures, and rx, ry, rz, rxx, ... are bound to the individual captures. Likewise, rr is bound to the full match text.

Global Data Structures (#v)

The variable #v stores the global stack. Functions such as stack-push and stack-pop implicitly operate on this variable. The variable is global by default but can be locally rebound with #v. Stack manipulation functions will respect this local binding, although there is rarely a reason to make one. Additionally, #v is simply a list of the elements on the stack, with higher stack elements at the front of the list, and so the variable can be modified directly if needed.

Clone this wiki locally