Skip to main content

Ape โ€” The Because-I-Said-So Language for deterministic AI programming

Project description

Ape - AI Programming Language

The very first AIP (AI Programming Language) ๐Ÿš€

Ape is an AI-native programming language with a strict core and Controlled Deviation System.

Quickstart

Installation

git clone https://github.com/yourusername/ape.git
cd ape
pip install -r requirements.txt  # if exists, or install pytest

CLI Usage

# Parse Ape source to AST
python -m ape parse examples/calculator_basic.ape

# Build IR (Intermediate Representation)
python -m ape ir examples/calculator_basic.ape

# Validate semantics and strictness
python -m ape validate examples/calculator_basic.ape

# Build Python code
python -m ape build examples/calculator_basic.ape --target=python

Your First Ape Program

Create hello.ape:

entity Greeting:
    message: String

task say_hello:
    inputs:
        - name: String
    outputs:
        - greeting: Greeting
    
    constraints:
        - deterministic
    
    steps:
        - create greeting with message
        - return greeting

Then compile it:

python -m ape validate hello.ape
python -m ape build hello.ape --target=python

Core Philosophy

"What is allowed, is fully allowed.
What is forbidden, is strictly forbidden.
What is not declared, does not exist."

What is Implemented

โœ… 1. Parser & Tokenizer

  • Tokenizer with indentation-aware lexical analysis
  • AST nodes for all Ape constructs
  • Recursive descent parser for Ape grammar v0.3
  • IR Builder transforms AST to Intermediate Representation

Tests: 11 passing

โœ… 2. Semantic Validator

  • Symbol table for tracking all declarations
  • Type checking (entities, enums, builtin types)
  • Duplicate definition detection
  • Unknown type detection
  • Contract validation
  • Policy validation
  • Deviation validation (RFC-0001)

โœ… 3. Strictness Engine

  • Ambiguity detection (maybe, possibly, ?, etc.)
  • Undeclared behavior detection
  • Implicit choice detection (or, choose, etc.)
  • Non-determinism detection (random, etc.)
  • Deviation bounds validation
  • Policy conflict detection

Tests: 19 passing

โœ… 4. Python Code Generator

  • Entity โ†’ Python @dataclass with type hints
  • Enum โ†’ Python constants class
  • Task โ†’ Python function with documentation
  • Flow โ†’ Orchestration function + metadata
  • Policy โ†’ Python dict structures
  • Syntactically valid Python output

Tests: 12 passing

โœ… 5. Runtime Support

  • RunContext for flow orchestration
  • Placeholder for logging, determinism, etc.

Project Structure

Ape/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ apeparser/          # Parser & Tokenizer
โ”‚   โ”‚   โ”œโ”€โ”€ tokenizer.py
โ”‚   โ”‚   โ”œโ”€โ”€ parser.py
โ”‚   โ”‚   โ”œโ”€โ”€ ast_nodes.py
โ”‚   โ”‚   โ””โ”€โ”€ ir_builder.py
โ”‚   โ”œโ”€โ”€ apecompiler/        # Compiler & Validation
โ”‚   โ”‚   โ”œโ”€โ”€ ir_nodes.py
โ”‚   โ”‚   โ”œโ”€โ”€ errors.py
โ”‚   โ”‚   โ”œโ”€โ”€ semantic_validator.py
โ”‚   โ”‚   โ””โ”€โ”€ strictness_engine.py
โ”‚   โ”œโ”€โ”€ apecodegen/         # Code Generation
โ”‚   โ”‚   โ””โ”€โ”€ python_codegen.py
โ”‚   โ””โ”€โ”€ aperuntime/         # Runtime Support
โ”‚       โ””โ”€โ”€ core.py
โ”œโ”€โ”€ tests/                  # Test Suite (49 tests)
โ”‚   โ”œโ”€โ”€ parser/
โ”‚   โ”œโ”€โ”€ compiler/semantic/
โ”‚   โ”œโ”€โ”€ codegen/python/
โ”‚   โ””โ”€โ”€ examples/           # Example program tests
โ”œโ”€โ”€ examples/               # Example Ape programs
โ”‚   โ”œโ”€โ”€ calculator_basic.ape
โ”‚   โ””โ”€โ”€ README.md
โ”œโ”€โ”€ generated/              # Generated Python code
โ”œโ”€โ”€ demo_pipeline.py        # Complete pipeline demo
โ””โ”€โ”€ example_generate.py     # Code generation example

Ape Syntax Voorbeeld

entity User:
    id: Integer
    username: String
    email: String

enum UserRole:
    - admin
    - user
    - guest

task CreateUser:
    inputs:
        username: String
        email: String
        role: UserRole
    outputs:
        user: User
    steps:
        - validate username is not empty
        - validate email format
        - create User instance
        - assign role to user
        - return user
    constraints:
        - username must be unique

flow UserRegistrationFlow:
    steps:
        - receive registration request
        - call CreateUser task
        - send welcome email
        - return success

policy SecurityPolicy:
    rules:
        - all passwords must be hashed
        - user data must be encrypted

Usage

Complete Pipeline

from apeparser import parse_ape_source, IRBuilder
from apecompiler.semantic_validator import SemanticValidator
from apecompiler.strictness_engine import StrictnessEngine
from apecodegen.python_codegen import PythonCodeGenerator
from apecompiler.ir_nodes import ProjectNode

# 1. Parse Ape source
ast = parse_ape_source(source, "example.ape")

# 2. Build IR
builder = IRBuilder()
ir_module = builder.build_module(ast, "example.ape")
project = ProjectNode(name="MyProject", modules=[ir_module])

# 3. Validate
validator = SemanticValidator()
errors = validator.validate_project(project)

# 4. Check strictness
engine = StrictnessEngine()
warnings = engine.enforce(project)

# 5. Generate Python
codegen = PythonCodeGenerator(project)
files = codegen.generate()

# 6. Save
for file in files:
    with open(file.path, 'w') as f:
        f.write(file.content)

Running Demos

# Complete pipeline demo
python demo_pipeline.py

# Generate code
python example_generate.py

# Run tests
python -m pytest tests/ -v

# Test calculator example
python -m pytest tests/examples/test_calculator_basic.py -v

Examples

Calculator Examples

1. Calculator Basic (calculator_basic.ape)

A fully deterministic calculator example that demonstrates:

  • Strict type checking without ambiguity
  • Deterministic constraints
  • No controlled deviation
  • Complete pipeline from Ape โ†’ Python

2. Calculator Smart (calculator_smart.ape)

Demonstrates the Controlled Deviation System (CDS):

  • Deterministic for calculations
  • Creative freedom for human summary
  • Explicit bounds define what can vary
  • Rationale explains why deviation is needed

See examples/calculator_basic.ape, examples/calculator_smart.ape and examples/README.md for details.

Test Results

โœ… Parser tests:           11 passed
โœ… Semantic tests:         19 passed
โœ… Codegen tests:          12 passed
โœ… Example tests:          14 passed (7 basic + 7 smart)
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
   TOTAL:                  56 passed

What Makes Ape Unique

  1. Strict Determinism - No implicit ambiguity allowed
  2. Controlled Deviation (RFC-0001) - Explicit flexibility with bounds
  3. AI-Native - Designed for AI agents to work with
  4. Type Safety - Strict type checking at all levels
  5. Policy Enforcement - Policy rules integrated in the language

Next Steps

Potential extensions:

  • Fully implement deviation system
  • Runtime with logging and tracing
  • Web-based playground
  • VS Code extension
  • More target languages (TypeScript, Rust, etc.)
  • Standard library with common patterns
  • Package manager for Ape modules

Technische Details

  • Taal: Python 3.11+
  • Dependencies: Geen (stdlib only)
  • Test Framework: pytest
  • Code Style: Typed Python met dataclasses

Status: ๐ŸŸข Prototype v0.1 - Parser, Validator & Python Codegen working

Date: December 3, 2025

Author: Veris (AI Lead Designer)

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

ape_lang-0.1.0.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

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

ape_lang-0.1.0-py3-none-any.whl (31.9 kB view details)

Uploaded Python 3

File details

Details for the file ape_lang-0.1.0.tar.gz.

File metadata

  • Download URL: ape_lang-0.1.0.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for ape_lang-0.1.0.tar.gz
Algorithm Hash digest
SHA256 65e1f66607f2342379f99ec447a59d63b697fa44ce791a2f4f32b67195210282
MD5 1eda516c14179ae73c4a497ead3166ee
BLAKE2b-256 7a310b9656923b073be6babc3305a411d6a0d1c565a0975ea01ea7430d177b6f

See more details on using hashes here.

File details

Details for the file ape_lang-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ape_lang-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 31.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for ape_lang-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c3acd55feaf85261f92578f0f53502ae693d8c3d29ee10c14143fa2ad6dcec00
MD5 7e91df59074d7c12d2292598d4730ba9
BLAKE2b-256 e3a3b73e5216855d8ea6db473d383e987643942d1fa02cb8d6c5f9a25d7d9faf

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