Language Reference

Introduction

This page documents the syntax and semantics of the Tetra language. The page does not provide a formal specification, but an informal one. For a formal syntax specification, see the parser.y file in the Tetra code.

The goal of this page is to provide more detailed information than is given in the tutorial or descriptor pages.


Program Structure

Tetra programs are made of several top-level elements. These can be functions, classes, constants or globals.

Constants consist of the keyword constant, followed by the name of the constant variable, followed by an assignment to a value. Below is an example:


constant E = 2.718281828

Globals are similar, except they use the global keyword, and do not require an assignment. If a global does not contain an assignment, it must be given a type. Below are two examples of globals:


global x = 4.0
global y as real

Comments in Tetra begin with an octothorpe (#) and continue until the end of the line.


Types

All variable types are known to the Tetra interpreter before the program is executed. This means that each newly introduced variables must either be assigned an initial value, or have a type declared. Tetra types can be any of the following:

Note that the above refer to specifying a type in a declaration only.


Functions

Functions in Tetra are created with the def keyword, followed by the name of the function, followed by the parameter list, followed by a colon, and then a new line. After that comes the body of the function which is indented one level.

Below is an example of a function which takes two integers and returns the sum:


def sum(a int, b int) int:
    return a + b

Note that the types follow parameter names, and that the return type is between the closing parenthesis and colon.

If the function returns no value, then the return type is simply omitted.


Indentation

Tetra uses indentation to delimit blocks, as seen in the function example above. The indentation must be some number of spaces - tab characters are illegal in Tetra code. The interpreter does not care how many spaces you use to indent so long as you are consistent.

The interpreter finds the first indented block in your program and uses the amount of spaces you used as the indentation level. You must use this same amount of indentation level each subsequent time you indent, to use more or less will cause a syntax error.


Scope

Unlike some languages, Tetra does not treat each block of code as its own scope. Instead, each function contains one scope, regardless of how many indented blocks it contains. There is also one global scope which contains the constant and global declarations.


Statements

A function body is made up of statements. These can either be a "simple statement" or a "compound statement". The difference is that multiple simple statements can be combined in one line, separated by semi-colons.

The simple statements are:

The compound statements ate:

Copyright © 2018 Tetra | Licensed under the MIT License