Threadon / Examples

Examples

the programs in the repo, explained

Running them

Each example lives in its own folder under examples/. Run one from the project root:

python3 -m compiler.main --run examples/02_structs/main.th

Numbers below are the exact output of each program.

The examples

01_hello — a first program

The minimal shape of a Threadon program: a helper function, a main, a call, arithmetic, print, and a return 0. Demonstrates functions.

def greet(x: Int32) -> Int32
    return x * 10

def main() -> Int32
    x: Int32 = 21
    print(greet(x) + 1)
    return 0
211

02_structs — a struct in action

A Point struct, a function that takes it, and a call. Demonstrates structs.

struct Point:
    x: Int32
    y: Int32

def length_sq(p: Point) -> Int32
    return p.x * p.x + p.y * p.y

def main() -> Int32
    p: Point = Point(x=3, y=4)
    print(length_sq(p))
    return 0
25

03_imports — a module with two import styles

A math.th module with abs, clamp, and mean. The main file imports it both ways, then calls the same functions through both. Demonstrates imports. Note how clamp and abs use the variable-merging trick from the Variables page.

from math import abs, clamp
import math

def main() -> Int32
    a: Int32 = abs(-5)
    b: Int32 = math.clamp(99, 0, 50)
    c: Int32 = math.abs(-9)
    print(a + b + c)
    return 0
64

04_import_structs — importing structs and functions

A geom.th module that exports Point and a few functions. The main file imports the struct directly and via the module name, then uses the functions. Demonstrates importing structs.

from geom import Point, dot, scale, origin
import geom

def main() -> Int32
    p: Point = geom.Point(x=3, y=4)
    q: Point = scale(2, p)
    o: Point = origin()
    print(dot(p, q) + o.x)
    return 0
50

(dot(Point(3,4), Point(6,8)) is 3×6 + 4×8 = 18 + 32 = 50.)

05_lazyimports — lazy from-import

Imports abs lazily from math.th and uses it once. Demonstrates lazyimport / lazyfrom.

lazyfrom math import abs

def main() -> Int32
    print(abs(-7))
    return 0
7

06_input — reading from the user

Asks for a name and prints a greeting. Demonstrates input. It waits for a line of input.

def main() -> Int32
    name: String = input("What is your name? ")
    print("Hello,")
    print(name)
    return 0
What is your name? Joep Hello, Joep

07_conversion — every kind of cast

Exercises all the casts: int to float, float to int, bool to int, string to int and float, string to bool, and number to bool.

print(Float32(7))       # 7.000000
print(Int32(3.7))      # 3
print(Int32(True))       # 1
print(Int32("123"))   # 123
print(Float32("3.5")) # 3.500000
print(Bool("true"))  # 1
print(Bool("0"))     # 0
print(Bool(7))       # 1
7.000000 3 1 123 3.500000 1 0 1

08_pointer — references and struct fields

A tour of the ^ operator and struct field assignment. It prints what each value is before and after a change.

r = 10 r = 15 x = 15 origin.x = 0 origin.y = 0 origin.x = 3 origin.y = 4

09_zero_division — a runtime check

Reads a number and divides by it. With --debug, entering 0 triggers the runtime check. Demonstrates debug mode.

def main() -> Int32
    x: Int8 = Int8(2)/Int8(input("Enter a number"))
    print(x)
    return 0
python3 -m compiler.main --run --debug examples/09_zero_division/main.th
Enter a number: 0
[RUNTIME ERROR] │ Error: Division by zero ├─> Location: (null) └─> Process terminated with exit code 1
NOT ALL EXAMPLES ARE LISTED. WORK COMING SOON