#Variables and Functions
LIOs support for variables and functions is early, basic, in particular note that error reporting is quite weak.
Some important notes:
Variables are globally scoped and exist for the lifetime of the program - until run.lio is changed or the phatIO device is restarted.
Variables and Functions should be declared at the top level - not within functions, drivers, and conditions.
##(defvar <name> <initialvalue>)
[defvar]
Defines a variable with the given name and initial value. Currently only integers and integer arrays are supported.
For example the following defines a variable a with initial value 3
(defvar a 3)
Any reference to a
in the rest of the script will be replaced by the current value of a, so the following will type “value is 3”:
(keyboard " value is " a)
Strings variables can be defined, the initial value sets the maximum length of the string, assigning a string larger will cause it to be truncated, the following defines a 4 character string and then assigns a longer string, “1234” will be typed.
(defvar s "ABCD")
(= s "123456")
(keyboard s)
If more than one initial integer value is defined, the variable is assumed to be an integer array, for example:
(defvar i 10 11 12)
Defines ‘i’ as three dimensional array with values 10, 11, and 12.
As with strings the array cannot be resized once created.
##(getvar <name> [index])
[getvar]
Get the variable with the given name, normally the variable would be accessed by implicit reference:
(defvar i 10)
(keyboard i)
Rather than
(defvar i 10)
(keyboard (getvar i))
But getvar
must be used when accessing an array value. The following uses getvar to access the values of array val
(“0 1 2 3 4” would be printed)
(defvar i 0)
(defvar val 0 1 2 3 4)
(while (< 1 4)
(keyboard (getvar val i) " ")
(+= i 1)
)
If an index out of bounds of the array is given (in the val
example above less than 0 or greater than 4) 0 will be returned.
When an index for a string variable is requested the integer value of the specified character will be returned, the following would type “99” the ASCII value of ‘c’ (The fmt function can be used to convert that to a character - “(fmt "%c" (getvar i 3))
”).
(defvar i "abcd")
(keyboard (getvar i 3))
##(= <name> [index] <value>)
[setvar]
Sets the named variable to the given value. If there are 3 arguments the second is assumed to be an array index. If no index is specified for an array, the first will be set.
##(defconst <constname> <value>)
[defconst]
The same as defvar but a constant is defined, which cannot be changed.
Currently constants are treated the same as variables but will be saved to persistent flash in a future version.
##(defun <functionname> code...)
[defun]
defun defines a function - the remaining arguments will be stored to flash and can be executed by (functionname)
.
The current defun functionality is basic allowing no arguments
The return value is the last expression evaluated, for example the following would type “2, timestwo 4, 3, timestwo 6”
(defvar i 2)
(defun itimestwo
(keyboard "timestwo ")
(* i 2)
)
(keyboard i ", " (itimestwo) ", " (= i 3) ", " (itimestwo))