Guide to NTSL2

Overview
NTSL2+ is the programming language used aboard the station for modular computers and telecommunications, you can design a lot of interesting things with it, this guide will act as a starting place and reference for enterprising programmers aboard the Aurora.

String Library
String Library

Syntax

 * Variables must start with a letter, but can otherwise contain any alphanumeric character.

Variables can be assigned to with name = value.

Local variables can be made with var name = value or local name = value.


 * Tables are values that can be indexed and can contain multiple values. They are also sometimes referred to as lists. They are constructed in the following format:

The final value can have a comma after it, but it is not mandatory. Tables can be indexed as name[index].


 * Functions can be made in the following format (the default value parameters are optional and can be left out):


 * Arguments can have any name a variable can take, and can be assigned default values.


 * Varargs are also usable:


 * The various arguments provided to the function will be combined into a list and stored in the local variable args, or whatever name is provided. Brackets for arguments are optional if you have an anonymous function with one argument:


 * They can even be constructed in lambda calculus style (the default value parameters are optional and can be left out):

In the function style, function can be abbreviated to func. Varargs can also be used in the lambda calculus style. Single variable lambda calculus functions can be further simplified into


 * Member functions of an object can be called in the form object.function(object, other_arguments) or object::function(other_arguments). These function identically.


 * Strings can be constructed in three ways: "content", ‘content’, or `content`.


 * These are functionally identical except for in the last case, where expressions in square brackets will be evaluated and inserted into the string. This means that `con[expression]tent` is equivalent to instead performing two concatenation operations and one tostring call, "con" .. tostring(expression) .. "tent"

Program Flow/Structure
IF/ELSE conditionals are part of the main program flow:
 * IF/ELSE:

WHEN conditionals are similar to IF conditionals, but are asynchronous and occur outside of the main program’s flow. They also expose their arguments to the scope of its block. A typical case is the topic variable which is set in term.topic events.
 * WHEN:

WITH block exposes a list/table to the scope of its block, typically used for making library referencing easier:
 * WITH:

An example is used in Telecomms scripts to save the constant references to tcomm:

FOR loops have two alternate forms. List Iteration FOR Loop:
 * FOR:

In this form, it iterates over the contents of list, setting var equal to them one at a time. list can instead be an iterator function as well, such as string.gmatch OR Variable Iteration FOR Loop:

Where i is any variable, initial is the initial value, condition is any condition (presumably involving said variable, but not necessarily), step is some form of increment/decrement such as i++, i--, ++i, --i, etc. This runs the for loop until condition is false, executing step after every iteration.


 * WHILE loop:

Evaluates condition, runs whichever code block's label corresponds to that condition or, if none is found, the block labelled default. If a break or return isn’t reached, the program will fall through to the next condition.
 * SWITCH block:

Evaluates expression, if expression is true returns on_true, otherwise returns on_false.
 * Ternary:

A very powerful, very dangerous function. Produces a function from NTSL2+ code contained in a string. Format is eval expression.
 * Eval:

Takes the form of either @operator or @expression. In the case of an operator, returns a function that behaves like that operator (With priority to binary operators). So @+ is equivalent to (a,b)=>a+b, though runs slightly faster. When an expression is used, this functions as a niladic operator, so is equivalent to =>expression.
 * The Deoperator:

+, -, *, /, ^, %, and/&&, or/||, <, >, <=, >=, ==, and != all operate as expected.

Concatenation is performed by '''expr1 .. expr2'''.

The length of the text representation of a string, or the number of elements in a list, can be found with #expression.

Logical negation is performed with !expression, binary negation is performed with ~expression, decimal unary negation is performed with -expression.

Integer division can be performed with expression1 // expression2.

Modulo can be performed with expression1 % expression2.

Bitwise operators are also available, such as |, &, <<, >>, &, and expression1 ~ expression2 (xor, do not confuse with unary base 2 negation)