-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This page acts as a one-stop syntax reference for Legend users. The standard library is still under construction, but a similar reference will be made as it draws closer to completion.
A statement can be an expression, like a function call, or one of the following statement types.
<variable> = <expression>
A valid variable name consists of letters, digits, and underscores. The following keywords are reserved and can't be used as variable names: and
, def
, else
, end
, for
, if
, in
, mod
, or
, nor
, not
, return
, to
, where
, and while
.
"<string>"
Print a string directly to the standard output. Use double quotes to denote a print string and single quotes to denote an ordinary string literal.
return [expression]
Can be used outside a function to terminate the program.
if <expression>
<...>
else if <expression>
<...>
else
<...>
end
while <expression>
<...>
end
for <variable> in <expression>
<...>
end
The <expression>
can resolve to any type that defines a unary operation handler named for
, such as lists, strings, ranges, and potentially user-defined types. You can implement a simple counting loop using the to
operator: for i in 0 to 10
.
def <name> ( <parameters...> )
<...>
end
For example, a function that returns the sum of its two arguments might be defined like so:
def sum(a, b)
return a + b
end
An expression consists of a sequence of one or more literals, variables, and operators. Operations are evaluated in the order outlined in the following precedence table, from top to bottom:
Operators | Description |
---|---|
. () []
|
member select, invoke, subscript |
unary - # %
|
negate, measure, percentify |
unary not
|
logical negate |
^ |
exponentiate |
* / // mod
|
multiply, divide, int divide, modulus |
+ -
|
add, subtract |
== != < <= > >=
|
compare |
& ?
|
concatenate, non-null select |
in is is not not in to where
|
non-logical keywords |
and or nor
|
logical keywords |
:= |
inline assign |
-
+
-
*
/
^
- these have the same meaning as in traditional mathematics -
%
- converts a percent to a decimal, e.g.50%
->0.5
-
mod
- see modulo on Wikipedia
-
==
!=
- checks the equality of two values -
<
<=
>
>=
- compares the magnitude of two values -
in
not in
- checks whether the first value is a member of the second, e.g.10 in List(5, 10, 15)
-
is
is not
- checks if a value belongs to a particular type, e.g.x is String
-
and
- returns whether two values are both true -
or
- returns whether either of two values is true -
nor
- returns whether two values are both false -
not
- returns the negation of a value -
?
- returns the first of two values that is non-null, e.g.a ? b
-
.
- retrieves a member of an object, e.g.employee.age
-
()
- invokes a function or instantiates a type, e.g.max(a, b)
-
[]
- subscripts a value, e.g.my_list[3]
-
#
- returns the size of an aggregate object, e.g.#my_string
-
&
- concatenates two values into a string, e.g.'hello, ' & name
-
to
- returns an inclusive range of numbers, e.g.0 to 10
You can use the :=
operator to assign a variable from within an expression, much like how ordinary assignment works in many C-family languages. For example,
if (result := process()) != null
print(result)
end
You can use the where
operator to filter aggregate objects, like lists. Given the expression X where P
, the operation returns a new object containing only the elements of X
for which the expression P
returns true. You can use the wildcard character *
within P
to reference the current element. For example,
seniors = students where *.credits >= 100
print(seniors)
There are currently seven built-in types:
Type | Examples |
---|---|
Boolean |
true and false
|
Function | built-in and user-defined functions |
Integer |
0 , 100 , -30
|
List | List('a', 'b', 'c') |
Range | 100 to 200 |
String | 'hello there' |
Type |
Boolean , Function , etc. |
There is also a special value null
that does not belong to any type. You can use null
to represent the absence of a value.