Skip to content

tea-compiler/teac-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

teac-cpp

A C++ reimplementation of teac targeting AArch64 Darwin (macOS ARM64) assembly. Written in C++23 with Flex/Bison for lexing and parsing.

TeaLang at a Glance

TeaLang is a statically typed, imperative language with C-like syntax and Rust-inspired declarations. Here's a taste:

use std;

fn gcd(a: i32, b: i32) -> i32 {
    if b == 0 {
        return a;
    } else {
        return gcd(b, a - (a / b) * b);
    }
}

fn main() -> i32 {
    let x: i32 = std::getint();
    let y: i32 = std::getint();
    std::putint(gcd(x, y));
    std::putch(10);
    return 0;
}

Key language features:

  • Types: i32, fixed-size arrays ([i32; 16]), void
  • Declarations: let bindings, global variables, fn functions with typed parameters
  • Control flow: if/else, while, break, continue, return
  • Expressions: arithmetic, comparison, logical (&&, ||, !), array indexing
  • Modules: use std; imports the standard library, accessed via std:: prefix
  • Standard library (std): getint, getch, putint, putch, putarray, timer_start, timer_stop

Building

Prerequisites

  • CMake >= 3.16
  • A C++23 compiler (Clang or GCC)
  • Homebrew bison (>= 3.2) and flex on macOS
brew install bison flex

Compile

cmake -B build && cmake --build build

The teac binary is produced at build/teac.

Usage

teac <input.tea> [-d ast|ir|ssa|s] [-o output]
Flag Description
-d ast Dump the Abstract Syntax Tree
-d ir Dump the Intermediate Representation (pre-optimization)
-d ssa Dump the IR after mem2reg (SSA form with phi nodes)
-d s Emit AArch64 assembly (default)
-o file Write output to a file instead of stdout

Compiling and Running a Program

# Compile TeaLang source to assembly
./build/teac program.tea -o program.s

# Assemble and link with the standard library
clang program.s tests/std/std.o -o program -arch arm64

# Run
./program

Testing

Compile-only Tests (CTest)

Verifies that teac can produce assembly for every test case without errors:

cd build && ctest

End-to-End Tests

Compiles each test, links it, runs the binary, and compares stdout (plus the exit code) against the expected .out file:

./scripts/run_tests.sh

Each test lives in its own directory under tests/<name>/:

File Purpose
<name>.tea Source file
<name>.in Stdin input (optional)
<name>.out Expected output (stdout followed by the return value on the last line, per SysY convention)

The test suite includes 29 programs covering algorithms like BFS, DFS, Dijkstra, sorting, binary search, matrix multiplication, convolution, a Brainfuck interpreter, and more.

Project Structure

teac-cpp/
├── include/teac/           # Public headers
│   ├── ast/                #   AST node definitions
│   ├── codegen/            #   Code generation interfaces
│   │   └── aarch64/        #     AArch64-specific codegen
│   ├── common/             #   Shared utilities (Indirect<T>, error types, Overloaded)
│   ├── driver/             #   Compiler driver interface
│   └── ir/                 #   IR types, modules, passes
├── src/                    # Implementation
│   ├── ast/                #   AST display/utilities
│   ├── codegen/            #   Layout, stack frame, AArch64 backend
│   ├── driver/             #   Compiler orchestration, preprocessor
│   ├── ir/                 #   IR generation, CFG, dominance, mem2reg, optimization passes
│   ├── parser/             #   Flex lexer (.l), Bison grammar (.y), ParserDriver
│   └── main.cpp            #   Entry point and CLI argument parsing
├── tests/                  # Test suite
│   ├── std/std.c           #   C runtime library (getint, putint, etc.)
│   └── <name>/             #   Individual test cases
├── scripts/
│   └── run_tests.sh        #   End-to-end test runner
└── CMakeLists.txt

About

A C++ reimplementation of teac targeting AArch64 Darwin (macOS ARM64) assembly.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages