Variables
Variables hold values. You assign them with =:
x = 10
name = "alice"
Variable names must start with a letter and can contain letters, digits, and underscores.
Variables do not need to be declared before use. The first time you assign to a name, the variable is created. Use %name to explicitly declare a variable with value 0:
%counter
write(counter) : 0
counter = 10
write(counter) : 10
If you use a variable that has not been assigned yet, its value defaults to 0:
write(z)
In Python, an undefined variable raises NameError. In lil, you get 0. This is useful for counters and accumulators that start at zero implicitly.
Counting up works naturally:
count = 0
count = count + 1
count = count + 1
write(count)
You can reassign variables to values of any type:
x = 42
x = "now im a string"
x = 99
Assignment always goes variable = expression:
y = x + 50
Assign a number to a variable:
n = 10
write("number:", n)
Store the result of a math expression:
area = 3.14 * 5 * 5
write("area is", area)
Forcing variable types
force locks a variable completely. Once forced, you cannot change it with stringify, intify, toggle, or regular assignment:
x = 42
force x
x = 30 : ERROR: cannot assign to a forced variable
stringify x : ERROR: cannot stringify a forced variable
unforce x : unlock it
x = 30 : works again
Combined assignment and force:
force x = 42
unforce x = 56
unforce unlocks the variable so it can be changed again.
force with read() reads input and forces the variable to a specific type:
age = read("Enter your age: ")
intify age
force age
write("next year you will be", age + 1)
For string input with force:
name = read("Enter your name: ")
force name
write("hello", name)
Input validation without force
Use read() with intify to validate the input type without forcing the variable:
x = read("Enter a number: ")
intify x
x = "now im a string" : allowed (not forced)
y = read("Enter text: ")
y = 42 : allowed (not forced)
intify errors if the value is not a valid number. read() always returns a string.
Live Bindings
A live binding links one variable to another so that reads of the bound variable always reflect the current value of its source:
y = 1
live x = y
y = 42
write(x) : prints 42
After live x = y, every read of x follows the link back to y and returns whatever y currently holds. You do not need to reassign x when y changes.
Assigning directly to x overwrites the binding and clears the live link. For a one-time copy, use plain assignment (x = y).
Changing the undefined default
By default, reading an undefined variable returns 0. Use ?default = value to change this:
?default = "Undefined"
write(someVar) : prints "Undefined"
?default = 0
write(anotherVar) : prints 0
The name after ? is descriptive only and is ignored by the runtime. Only one value is stored globally. Once set, all undefined variables will return that value until changed.