Skip to main content

ASTx is an agnostic expression structure for AST.

Project description

ASTx: Abstract Syntax Tree Framework

CI Python Versions Package Version License Discord

ASTx is a versatile and extensible library for representing, manipulating, and analyzing Abstract Syntax Trees (ASTs). It provides a unified interface for working with ASTs in various contexts, such as compilers, interpreters, and transpilers.

ASTx makes it easy to model programming languages, apply transformations, generate code, and build custom tools for static and dynamic analysis.

ASTx doesn't aim to be a lexer or a parser, although it could be used by any programming language or parser in order to provide a high level representation of the AST.

It integrates with IRx, enabling code generation with LLVM. Currently, only a small subset of ASTx nodes is supported, but active development is underway, with full support expected soon.

Note: this project is under active development and it is not ready for production yet.


🚀 Features

  • Language-Agnostic Design: Model and manipulate ASTs for different programming languages.
  • Extensibility: Easily add support for new language features or custom AST nodes.
  • Code Generation: Transform ASTs into target code for various backends or target languages.
  • Rich Node Set: Support for common constructs like variables, expressions, functions, classes, and control flow.
  • Python Integration: Built with Python, making it easy to integrate into Python-based projects.
  • Symbol Table: Support for an initial implementation of Symbol Table.

📦 Installation

Install ASTx from PyPI:

pip install astx

📖 Overview

ASTx is designed around two primary concepts:

  1. Nodes: Each node represents a language construct (e.g., Variable, Function, IfStmt).
  2. Tree: Nodes are organized hierarchically, forming an abstract representation of the program structure.

Additionally, ASTx provides a simple transpiler for converting ASTx nodes to Python code (in text format). This feature is intended solely for educational purposes, demonstrating how a transpiler from ASTx to any other language can be implemented.


✨ Usage

1. Create an AST

import astx

# Define a simple function `add(x, y): return x + y`
args = astx.Arguments(
    astx.Argument(name="x", type_=astx.Int32()),
    astx.Argument(name="y", type_=astx.Int32()),
)
fn_body = astx.Block()
fn_body.append(
    astx.FunctionReturn(
        value=astx.BinaryOp(op_code="+", lhs=astx.Variable("x"), rhs=astx.Variable("y"))
    )
)
add_function = astx.FunctionDef(
    prototype=astx.FunctionPrototype(name="add", args=args, return_type=astx.Int32()),
    body=fn_body,
)

2. Generate Code

Use a transpiler to convert the AST to Python code:

from astx_transpilers.python_string import ASTxPythonTranspiler

# Transpile the AST to Python
transpiler = ASTxPythonTranspiler()
python_code = transpiler.visit(add_function)

print(python_code)

Output:

def add(x: int, y: int) -> int:
    return (x + y)

3. ASTx Visualization Features

ASTx offers multiple ways to visualize the AST structure:

  • YAML
  • JSON
  • Graphical visualization (PNG or ASCII)

In a Jupyter Notebook, the default graphical visualization is PNG, while in a console, the default is ASCII.

You can also print the AST structure in JSON or YAML format. For example:

>>> print(add_function.to_json())
>>> print(add_function.to_yaml())

📚 Documentation

Detailed documentation and examples can be found in the official documentation.


🛠️ Contributing

Contributions are welcome! Please check out our Contributing Guide for more information.


📝 License

ASTx is open-source software licensed under the BSD-3-Clause License. See LICENSE for details.

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

astx_transpilers-0.22.0.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

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

astx_transpilers-0.22.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file astx_transpilers-0.22.0.tar.gz.

File metadata

  • Download URL: astx_transpilers-0.22.0.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.11 Linux/6.11.0-1018-azure

File hashes

Hashes for astx_transpilers-0.22.0.tar.gz
Algorithm Hash digest
SHA256 01ec254d1b8e726292e1ddd26507ed6d6bb658fbe7a72f4bef5481a500d2a576
MD5 4c28f76dbc3a9a729e6137b80354c4e0
BLAKE2b-256 f668653a90e9032858f4d04750ccb063a4f121271c8f3b96bbc508dcf1093bd5

See more details on using hashes here.

File details

Details for the file astx_transpilers-0.22.0-py3-none-any.whl.

File metadata

  • Download URL: astx_transpilers-0.22.0-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.11 Linux/6.11.0-1018-azure

File hashes

Hashes for astx_transpilers-0.22.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cfc32b3e8a693c40338672dc660b46882597edc690ef7ceb70551e6fa5b57359
MD5 0b753b64ac321ec10d1a5663ee6f4bf6
BLAKE2b-256 2d46a4edbeaa55c3ed3dd67359610fe7fbf71f859abb220ed045268857d4bb91

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