Skip to content

About

A tiny typed programming language compiler that emits readable Python

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Agentic Compiler

Agentic Compiler is a tiny typed programming language compiler. It lexes, parses, type-checks, lowers to a three-address intermediate representation, emits readable Python code, and can execute either backend.

This is not an agent-plan artifact generator. It is a real compiler pipeline for a small language.

Features

  • Lexer with comments, strings, integers, booleans, identifiers, and operators
  • Recursive descent parser with Pratt-style expression precedence
  • AST dataclasses for statements and expressions
  • Static checks for variables, mutability, function calls, lists, returns, loops, and operators
  • Three-address IR backend with temporaries, labels, goto, and conditional jumps
  • Three-address IR runner for simple WHILE3ADDR-style programs
  • Python code generator with safe integer division and forward function references
  • CLI: check, compile, run, ir, run-ir, tokens, ast, repl
  • Public API: tokenize, parse_source, check_source, compile_source, run_source, emit_ir_source, run_ir_source
  • Tested with pytest, ruff, strict mypy, package build, and GitHub Actions

Install

pip install -e .[dev]

Quick start

Create main.ag:

fn fact(n: Int) -> Int {
  if n <= 1 {
    return 1
  } else {
    return n * fact(n - 1)
  }
}

print(fact(5))

Run it:

agentic-compile run main.ag
# 120

Compile it to Python:

agentic-compile compile main.ag --out main.py
python main.py

Emit three-address IR:

agentic-compile ir main.ag

Example IR for let x: Int = (1 + 2) * 3 print(x):

t0 = 1 + 2
x = t0 * 3
print x

Language overview

Bindings

let creates an immutable binding. var creates a mutable binding.

let answer: Int = 42
var total: Int = 0
total = total + answer

Types

Supported types:

  • Int
  • Bool
  • String
  • Void for functions that do not return a value
  • List[T], for example List[Int]

Empty lists require an annotation:

let xs: List[Int] = []

Functions

Functions are checked before top-level code is emitted, so forward references work.

print(add(2, 3))

fn add(a: Int, b: Int) -> Int {
  return a + b
}

Control flow

var total: Int = 0
for i in range(0, 6) {
  if i == 4 { break }
  if i % 2 == 0 { continue }
  total = total + i
}

if total == 4 {
  print("four")
} else if total == 1 {
  print("one")
} else {
  print("other")
}

Lists

var xs: List[Int] = [1, 2, 3]
xs[1] = 20
print(xs[1])

Built-ins

  • print(value) as a statement
  • len(list) returns Int
  • str(value) returns String
  • range(stop), range(start, stop), range(start, stop, step) return List[Int]

CLI

agentic-compile check main.ag
agentic-compile compile main.ag --out main.py
agentic-compile run main.ag
agentic-compile ir main.ag
agentic-compile run-ir main.ag
agentic-compile tokens main.ag
agentic-compile ast main.ag
agentic-compile repl

Python API

from agentic_compiler import compile_source, emit_ir_source, run_source

compiled = compile_source('print(1 + 2)')
print(compiled.python_source)

print(emit_ir_source('let x: Int = (1 + 2) * 3 print(x)'))

result = run_source('print(1 + 2)')
assert result.stdout == '3\n'

Development

python -m pytest -q
python -m ruff check .
python -m mypy src/agentic_compiler --strict
python -m build

Status

Alpha, but the pipeline is real: source text becomes tokens, AST, checked semantics, three-address IR, generated Python, and executable output.

About

A tiny typed programming language compiler that emits readable Python

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages