Skip to content
ValorCat edited this page Jul 25, 2019 · 12 revisions

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.

Contents

  1. Statements
  2. Expressions
  3. Data Types

Statements

A statement can be an expression, like a function call, or one of the following statement types.

Assignment

<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.

Print String

"<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

return [expression]

Can be used outside a function to terminate the program.

If

if <expression>
    <...>
else if <expression>
    <...>
else
    <...>
end

While

while <expression>
    <...>
end

For

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.

Function Definition

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

Expressions

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

Operator Descriptions

Arithmetic

  • + - * / ^ - 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

Comparison

  • == != - 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

Logical

  • 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

Miscellaneous

  • . - 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

Inline Assignment

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

Filtering

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)

Data Types

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.

Null

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.