Skip to main content

The Tawla programming language and its tawlac compiler

Project description

Tawla

Tawla is a small programming language with its own compiler, tawlac, built from scratch. It looks a lot like C#: you write classes with fields and methods, you get inheritance and interfaces, and everything is statically typed. Under the hood tawlac turns your code into real machine code using LLVM and runs it on the spot, so there's no separate "compile then run" dance.

It started as a learning project to understand how compilers actually work, and it grew into a genuinely usable little language. Source files end in .twl.

Here's the whole "hello world":

class Main {
    void main() {
        print("Hello, Tawla!");
    }
}
$ tawlac run hello.twl
Hello, Tawla!

Getting it running

You need Python 3.11 or newer. Install it from PyPI with pip:

pip install tawla

That pulls in everything it needs (including LLVM, via llvmlite) and gives you the tawlac command. Works the same on Windows, macOS, and Linux.

Hacking on it from a clone of this repo instead? That works too:

pip install llvmlite
python -m tawla run examples/hello.twl

Either way you get the tawlac command (or python -m tawla) with a few subcommands:

tawlac run app.twl     # compile and run a file
tawlac new myapp       # scaffold a new project (like cargo new)
tawlac init            # scaffold one in the current folder
tawlac run             # run the current project (reads Tawla.toml)
tawlac version         # what version is this
tawlac help            # or: tawlac help run

What the language can do

  • The basics: int, bool, and string; arithmetic and comparisons; if/else, while, and functions (recursion works fine).
  • var: skip the type and let it be inferred — var x = 5;.
  • Classes: fields, methods, constructors, this, and new. Objects live on the heap.
  • Inheritance: class Dog : Animal { ... }, with method overriding and super(...) to call the base constructor.
  • Polymorphism: methods are virtual, so the right one gets picked at runtime based on the actual object type (this is done with vtables).
  • Interfaces: interface Shape { int area(); }, and any class can implement it, even classes that share no common parent.
  • Abstract classes: mark a class abstract and leave some methods without a body for subclasses to fill in.
  • Generics: class Box<T> { ... }, used as Box<int> or Box<string>.
  • Strings: literals with escapes, s.length, ==, and + to join them.
  • Arrays: new int[n], indexing with a[i] (bounds-checked, so you get a clear error instead of a crash), and a.length.
  • Comments: // like this.
  • Imports: split code across files with import "other.twl"; — the path is relative to the file importing it, and the .twl is optional.
  • Garbage collection: you don't free memory by hand. Call collect() when you want a cleanup pass; __live() tells you how many objects are still around.

There's a runnable example for pretty much every feature in examples/ — poke around in there to see how things look in practice.

How it works, roughly

tawlac runs your code through the usual compiler stages:

source.twl -> lexer -> parser -> sema -> codegen -> LLVM -> JIT -> runs
  • the lexer chops the text into tokens
  • the parser builds a tree out of those tokens
  • sema checks the types and catches mistakes before any code is generated
  • codegen turns the checked tree into LLVM instructions
  • LLVM compiles that to machine code and the JIT runs it immediately

Each piece lives in its own file under tawla/, so it's not hard to follow if you want to read along.

Running the tests

./venv/Scripts/python -m pytest

There are around 200 tests. Programs that print output are checked by running tawlac as a separate process and looking at what it prints (this sidesteps a Windows quirk where output from JIT-compiled code is hard to capture in-process).

What's not done yet

It's a real language, but it's still a young one. A few honest gaps:

  • tawlac build (making a standalone .exe from your Tawla program) isn't done — for now everything runs through tawlac run.
  • Generics only cover classes, not standalone functions, and you can't nest them like Box<Box<int>>.
  • Garbage collection has to be triggered with collect(); it doesn't kick in on its own yet.
  • import pulls in other files by relative path, but there's no package system or standard library to speak of yet.

The full design and the step-by-step history of how it was built are in docs/superpowers/specs/2026-05-29-tawla-language-design.md if you're curious.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tawla-0.1.2.tar.gz (43.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tawla-0.1.2-py3-none-any.whl (33.4 kB view details)

Uploaded Python 3

File details

Details for the file tawla-0.1.2.tar.gz.

File metadata

  • Download URL: tawla-0.1.2.tar.gz
  • Upload date:
  • Size: 43.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for tawla-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2cd30cb543674ef5b52a12b008d8fed2a8d098d1e5b615808da8d90fa1b5fac1
MD5 fd712f18f2a7656ea3b7645b1b5ef1a1
BLAKE2b-256 7926443780618578c1e7ce6afb746f6166c90be6c3405e49077310b32242f389

See more details on using hashes here.

File details

Details for the file tawla-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: tawla-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 33.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for tawla-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e13dc625d55a486e41ab7c70c6bcf507335ea7ab4de1197aa2fb49959a97e26e
MD5 fa20a0ba6c72f636c22d84ee7dd4e86b
BLAKE2b-256 439812fb1a02b1c680174c0c682b2c85dfb6a21729ac8060f7ffd59d2e67faed

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page