Skip to content

Syntax reference

Felipe Martínez edited this page May 24, 2020 · 25 revisions

A LS script is executed sequentially from top to bottom. The script is composed only of top-level cases.

Concepts

Slots

Slots represent a machine's input and output, and can be raw or indexed. The number inside an indexer will always be treated as a decimal number.

Truthy values

A value will be considered "true" if it is greater than 0. If you instead want to check for a value equal to 1, use the equality operator (x == 1)


Directives

Directives can be declared at any point of the file, but outside case blocks. They have the format @name value, and the following directives are available (default value is bold):

  • strict (on/off): if on, an exception will be raised when you try to access an undeclared variable.
  • suffix (on/off): if on, decimal numbers won't require the ' suffix.

Comments

Comments can be started with a # anywhere on a line.


Case blocks

Conditional cases

Only executed if the condition evaluates to a truthy value.

when <expression>
    ...
end

Unconditional cases

Will be executed regardless of the machine's state.

any
    ...
end
once
    ...
end
  • any cases will always be executed, regardless of the machine's state
  • once cases will only be executed once (up to machine implementation, usually on first update)

Expressions

Number literals

LogicScripts distinguishes between binary and decimal numbers through the use of a suffix apostrophe ('). All decimal numbers must be suffixed, even if they contain digits other than 0 and 1, to prevent mistakes and confusion (unless the suffix directive is set to on).

1010 # Valid
123' # Valid
42   # Invalid, throws warning

Variables

Variables can be accessed by simply using their name.

Slot accessors

Only input (in) and memory (mem) slots can be used as expressions.

in

Indexers

Take a slice of bits from an expression. The start index is inclusive, while the end index is exclusive. The end index can be omitted, in which case the range will end at the end of the number. Both end and end are expressions.

in[0]
mem[0..]
123'[0..3]

List expression

Accepts multiple expressions in list form and combines them into a single number

(expr1, expr2, expr3, ...)

Function calls

Right now there is no way to define custom functions. Built-in functions:

  • Aggregation:
    • Will combine a number's bits into a single bit
    • AND and
      • and(1010) == 0
    • OR or
      • or(1010) == 1
    • Sum of bits sum
      • sum(110011) == 4
  • Truncate trunc
    • Will remove a number's bits on the left side
    • trunc(00011) == 11
    • trunc(101100, 3') == 101

Operators

All operators operate on numbers. For operator precedence check this file, operators on the top have a lower precedence than the ones at the bottom (e.g. multiplication has a higher precedence than addition).

Binary operators (binary as in two operands)

  • Bitwise operators:
    • AND &
    • OR |
  • Arithmetic operators:
    • Add +
    • Subtract -
    • Multiply *
    • Divide /
  • Comparison operators:
    • Equals ==
    • Greater >
    • Greater or equal >=
    • Lesser <
    • Lesser or equal <=

Unary operators

  • Negation !
    • Will negate all bits of a number

Statements

Contained inside cases or other statements.

Assignment

Assign a value to a slot or a variable. Only output (out) and memory (mem) slots can be assigned to.

out = 1010
mem[1] = 1
myVar = 4'

If statements

Will execute a block of statements if a condition is evaluated to a truthy value.

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

For loops

Execute a block of code a number of times with a counter variable. If the starting number isn't specified, 0 will be implied.

for i from <start> to <end>
    ....
end
for i to <end>
    ...
end

update statement

This statement's functionality will vary between machine implementations, and it will only be available if the machine supports it (by implementing the IUpdatableMachine interface).

In Logic World this statement will cause the component to be updated on the next tick.

update

Clone this wiki locally