Skip to content

Repository files navigation

Prism

Prism is a universal code translation and transpilation engine designed to separate the storage of code from its presentation.

The core philosophy is simple:

  1. Storage: Code is stored in a canonical, immutable Abstract Syntax Tree (AST) format (S-Expressions).
  2. Presentation: Users view and edit code in their preferred syntax (C-style, Python-style, etc.) via user-defined grammar rules.
  3. Round-trip: The engine ensures a perfect round-trip: Source -> AST -> Source.

Disclaimer

This is a hobby project, I will not actively maintain this. Use this for fun if you like to mess with compilers. I chose python because it's easy to read, quick to iterate, exactly what you want for a hobby project. If I were to actually write a tool like this, I'd want to use something like JAI.

This was my first project completely coded with AI. It was created entirely with AI agents to see how far I could get:

  • I got pretty far, way farther than I would've anticipated to get in a week.
  • I have probably written less than 10 lines of code myself.
  • I have probably read about 80% of the generated code, the quality is not great, not terrible.
  • I had to do a lot of cleaning up, there is a reason for the scripts/clean.py.
  • A lot of things from the specs are not implemented, there are a ton of outrageous claims in there I'm sure.

How I made this:

  • Used Google Antigravity with various models.
  • Started out writing the backend of the specs based on what's available on the unreleased JAI language (I can't wait to work with JAI).
  • Extended the specs to include details about the core philosophy: parsing and projecting.
  • Started with a simple implementation, basic integer math etc, and extended from there.
  • Continuously pointed the AI to reference the specs for implementing, then update the specs from the implementation if something was missing.

The eventual goal (not implemented):

  • Have a project that's completely stored as AST + your personal grammar.
  • When you edit a file, the AST gets projected to your grammar, displaying it in your personalized coding style.
  • When you save the file, it's stored back in AST format, so others can open it in their own personal grammar too.

To make this goal a reality a lot of things still have to be implemented:

  • LSP server to use the grammar for parsing/linting etc.
  • Diff tools: you'll want to see code changes in your own grammar.
  • Formal proof: mapping any grammar to the superset AST should be possible, how can we proof it?

Recommendation to make your grammar:

  • Use an iterative AI tool like antigravity/cursor/etc that can repeatedly make an iteration and test it.
  • Write a bit of code in your code style and add comments explaining the specifics.
  • Ask the AI to make the grammar and use the CLI to validate and parse.

The rest of this repository is completely written AI assisted.

Implemented Features

  • Universal Parser & Projector: Core engine to convert between text and AST based on grammar rules.
  • Structural Hashing: Efficient lookup of syntax rules based on AST structure.
  • Complexity Reduction: Smart handling of juxtaposition (e.g., f x vs f(x)), layout consumption (indentation handling), and implicit templates.
  • Language Support:
    • C-Style: Default fallback grammar.
    • C++: Supported via demos/cpp/cpp.yaml.
    • Python: Supported via demos/crossyntax/pythonstyle.yaml.
    • Haskell: Supported via demos/crossyntax/haskellstyle.yaml.
    • Lisp: Supported via demos/crossyntax/lispstyle.yaml.

Usage

Prerequisites

  • Python 3.12+
  • uv (recommended for dependency management)

Running Demos

The demos/ directory contains examples of different language syntaxes mapping to the same underlying AST logic.

To run the full test suite:

./test.sh

CLI

Prism provides a CLI for parsing, translation, validation, tokenization, compilation, and execution.

Parse Source to AST:

python3 main.py parse path/to/source.ext

Translate Source to Source:

python3 main.py translate path/to/source.ext --to path/to/target_grammar.yaml

Validate Grammar File:

python3 main.py validate path/to/grammar.yaml

Tokenize Source:

python3 main.py tokenize path/to/source.ext --grammar path/to/grammar.yaml

Compile Source to IR:

python3 main.py compile path/to/source.ext --grammar path/to/grammar.yaml

Run Source:

python3 main.py run path/to/source.ext --grammar path/to/grammar.yaml

Defining a Grammar

For instructions on how to create a grammar, please refer to the Cookbook.

Directory Structure

  • src/: Core engine source code (Parser, Projector, Nodes, etc.).
  • specs/: Detailed technical specifications.
  • demos/: Example grammars and source files for various languages.
  • tests/: Comprehensive unit tests.

About

Universal transpiler

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages