Little supports a set of basic types:
null
- represents the absense of a valuenumber
- a double-precision floating point numberboolean
- either true or falsestring
- a reference to an immutable stringfunction
- a little-defined functionclosure
- any function that captures surrounding valuesarray
- 0-indexed array of valuestable
- a table of key-value pairsnative
- reference to a natively defined C functionptr
- userdata pointer set by C api
These are grouped into Value
and Object
types, which are passed by value and reference respectively
null
, number
, boolean
, and string
are the Value
types. String is special in that they are immutable and stored in a global deduplication table, and the actual value passed around is an index into that.
var a
var b = 10
var c = (a or 10) + b
Variables are declared with the var
keyword. Only a single name is permitted per var
statement, with an optional expression following the =
assignment operator.
if a is 100 { ... }
elseif a is 150 { ... }
elseif b is a { ... }
else { ... }
Branching is done with the if
statement, followed by an expression to evaluate and then a mandatory set of braces, containing the body to execute. if
s can be followed by any number of elseif
statements, and optionally a final else
statement,
var a = [ 100, 200, 300 ]
for item in array.each(a) { ... }
for
loops come in only one flavour in little, requiring a single identifier to be the loop variable, and an expression that evaluates into an iterator function. It will be repeatedly called - and it's result stored in the loop variable - until it evaluates to null.
var a = 0
while a < 10 { a = a + 1 }
while
loops continually evaluate their condition and execute their bodies.
while true { break }
break
exits a loop early.
return "any expression!"
return
exits the current execution frame, and returns a single value to the caller.
var a = 10
a = 20
Any identifier followed by =
assignment.
Any top-level statement that doesn't match any of these is instead executed as an expression
Expressions consist of all literals and operators.
null
is both a type and a literal valuenumber
literals are any decimal number strings -123
,0.5
,123.123
etcboolean
literals are eithertrue
orfalse
string
literals are any double-quoted strings -"hello world!"
,"i love apples"
array
literals are a list of values between brackets -[ 1, true, null, "banana" ]
table
literals arekey: value
pairs grouped between braces -{ a: 10 b: 20 c: true }
function
literals are declared with this syntax:var my_fn = fn(a, b) { return a + b }
- They are first-class objects, and can only be stored through assignment
- Can be trivially passed as parameters as well
- Parameter list is mandatory, even if empty
The mathematical operators +
, -
, *
, and /
only work on number
values
The comparison operators <
, <=
, >
, >=
also only work with number
s
The comparison operators is
and isnt
work on all types
The logical operators or
, and
and not
compare values based on their truthiness
, and return their last operand
The index operator [expression]
works on any table
and array
values
The dot operator .
is syntax sugar for indexing table
s - my_table.my_index = 10
Any null
or false
values are considered falsy
, anything else is logically true