Program structure

A TryC file is a sequence of top-level statements, executed in order when the program starts, plus the classes and functions they use. Indentation delimits blocks, as in Python.

The smallest program

#include <libstd/stdio>

print("Hello world\n")

#include <libstd/stdio> pulls in the standard I/O module (print is a library function, not a keyword). Top-level statements run inside _start, the entry point the compiler generates; print does not add a newline, so the \n is explicit.

A program with a main

Most programs put their logic in a function and call it at the end of the file. The last top-level bare call of the form name() decides the process exit code: what that function returns becomes the value of $?.

#include <libstd/stdio>

def main() -> int:
    print("Hello from main\n")
    return 7

main()
./hello; echo "exit code: $?"
Hello from main
exit code: 7

You can also terminate explicitly from anywhere with exit(code).

Blocks and indentation

A colon at the end of a line opens a block; the block is the following lines indented deeper than the opening one. Any consistent indentation works (4 spaces are used throughout libstd). A block ends where the indentation returns to the previous level.

def classify(n: int) -> str:
    if n < 0:
        return "negative"
    elif n == 0:
        return "zero"
    else:
        return "positive"

Statements are one line

A statement cannot be split across lines: an expression, a call, an assignment or a condition must fit on the line where it starts. The one exception is the parameter list of a def (see Functions). Adjacent string literals are joined at compile time, which is the usual way to write a long string:

head = (
    "HTTP/1.1 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Connection: keep-alive\r\n\r\n"
)

Comments

// a line comment

/*
* a block comment; the leading '*' on each line is
* a convention used by libstd, not a requirement
*/

A block comment placed right after a def line is the function's docstring: the standard library documents every function that way, and the reference on this site is generated from those blocks.

Names and declaration order

Includes

#include <libstd/string>        // a standard module
#include <./helpers>            // a file next to this one (no extension)

An include is textual and happens in the precompiler, so a module is included at most once per program and its #defines are visible after the include. The full list of modules is in the standard library section.