Warning: This document is incomplete. It has not been proofread (style, correct links, mistranslation, etc.). Also, Erg's syntax may be change destructively during version 0.*, and the documentation may not have been updated accordingly. Please be aware of this beforehand. If you find any errors in this document, please report then to here form or GitHub repo. We would appreciate your suggestions.

Erg Book 日本語訳

Erg Book 繁體中文翻譯

Erg Book 简体中文翻译

This document describes the basic syntax of Erg. If you already have experience with languages such as Python, please refer to the quick tour for an overview. There is also standard API docs, type definition and internal docs for contributors. If you need a detailed explanation of the syntax or Erg's internal architecture, please refer to those documents.

Hello, World!

First, let's do "Hello World".

print!("Hello, World!")

This is almost identical to Python and other languages in the same family. The most striking feature is the !, the meaning of which will be explained later. In Erg, parentheses () can be omitted unless there is some confusion in interpretation. The omission of parentheses is similar to Ruby, but it is not possible to omit parentheses that can be interpreted in more than one way.

print! "Hello, World!" # OK
print! "Hello,", "World!" # OK
print!() # OK
print! # OK, but this does not mean to call, simply to get `print!` as a callable object

print! f x # OK, interpreted as `print!(f(x))`
print!(f(x, y)) # OK
print! f(x, y) # OK
print! f(x, g y) # OK
print! f x, y # NG, can be taken to mean either `print!(f(x), y)` or `print!(f(x, y))`
print!(f x, y) # NG, can be taken to mean either `print!(f(x), y)` or `print!(f(x, y))`
print! f(x, g y, z) # NG, can be taken to mean either `print!(x, g(y), z)` or `print!(x, g(y, z))`


Erg code is called a script. Scripts can be saved and executed in file format (.er).

REPL/File Execution

To start REPL, simply type:

> erg

> mark is a prompt, just type erg. Then the REPL should start.

> erg
Starting the REPL server...
Connecting to the REPL server...
Erg interpreter 0.2.4 (tags/?:, 2022/08/17  0:55:12.95) on x86_64/windows

Or you can compile from a file.

> 'print! "hello, world!"' >>

> erg
hello, world!


The code after # is ignored as a comment. Use this to explain the intent of the code or to temporarily disable the code.

# Comment
# `#` and after are ignored until a new line is inserted
Multi-line comment
Everything from `#[` to `]#` is a comment.

Documentation Comments

'''...''' is a documentation comment. Note that unlike Python, it is defined outside of any class or function.

PI is a constant that is the ratio of the circumference of a circle to its diameter.
PI = 3.141592653589793
This function returns twice the given number.
twice x = x * 2

print! twice.__doc__
# This function returns twice the given number.

Documentation comments for the entire class
C = Class {x = Int}
    Method documentation comments
    .method self = ...

You can specify the language of the document by writing the language code immediately after the '''. The Erg Language Server will then display documents in the Markdown format for each language version (The default language is English). See here for registered language codes.

Answer to the Ultimate Question of Life, the Universe, and Everything.

Also, if you specify erg, it will be displayed as Erg's sample code.

the identity function, does nothing but returns the argument
assert id(1) == 1
assert id("a") == "a"
id x = x

Expressions, separators

A script is a series of expressions. An expression is something that can be calculated or evaluated, and in Erg almost everything is an expression. Each expression is separated by a separator - either a new line or a semicolon ;-. Erg scripts are basically evaluated from left to right, top to bottom.

n = 1 # assignment expression
f x, y = x + y # function definition
f(1, 2) # function-call expression
1 + 1 # operator-call expression
f(1, 2); 1 + 1

As shown below, there is a syntax called instant block that takes the last expression evaluated in the block as the value of the variable. This differs from a function with no arguments, which does not add (). Note that instant blocks are evaluated only once on the fly.

i =
    x = 1
    x + 1
assert i == 2

This cannot be accomplished with a semicolon (;).

i = (x = 1; x + 1) # SyntaxError: cannot use `;` in parentheses


Erg, like Python, uses indentation to represent blocks. There are three operators (special forms) that trigger the start of a block: =, ->, and => (In addition, : and |, although not operators, also produce indentation). The meanings of each are described later.

f x, y =
    x + y

for! 0..9, i =>

for! 0..9, i =>
    print! i; print! i

ans = match x:
    0 -> "zero"
    _: 0..9 -> "1 dight"
    _: 10..99 -> "2 dights"
    _ -> "unknown"

If a line is too long, it can be broken using \.

# this does not means `x + y + z`, but means `x; +y; +z`
+ y
+ z

# this means `x + y + z`
x \
+ y \
+ z