Skip to main content

Sweetener

Python package Coverage Status PyPI version

Sweetener is a Python library for quickly prototyping compilers and interpreters. Extra care has been taken to make the algorithms as flexible as possible to fit as many use cases. If you're still missing something, do not hesitate to file an issue! In particular, this library contains the following goodies:

  • A typed record type that allows you to build PODs very quickly.
  • Base classes for an AST/CST, including reflection procedures.
  • A means for writing diagnostics that can print part of your source code alongside your error messages.
  • Generic procedures like clone, equal and lte that work on most types in the standard library and you can specialize for your own types.

Examples

Here's an example of using the Record type to define a record that holds two fields and visualizes tham using GraphViz:

from sweetener import Record, equal, visualize

class MySimpleRecord(Record):
    field_1: int
    field_2: str

r1 = MySimpleRecord('foo', 42)
r2 = MySimpleRecod(field_1='foo', field_2=42)

assert(equal(r1, r2))

visualize(r1)

Running this program will open a new window with the following content:

from sweetener import BaseNode, visualize

class CalcNode(BaseNode):
    pass

class Expr(CalcNode):
    pass

class Add(Expr):
    left: Expr
    right: Expr

class Sub(Expr):
    left: Expr
    right: Expr

class Var(Expr):
    name: str

class Lit(Expr):
    value: int

def eval(node: Expr, vars = {}) -> int:
    if isinstance(node, Add):
        return eval(node.left, vars) + eval(node.right, vars)
    if isinstance(node, Sub):
        return eval(node.left, vars) - eval(node.right, vars)
    if isinstance(node, Lit):
        return node.value
    if isinstance(node, Var):
        return vars[node.name]
    raise RuntimeError('Could not evaluate a node: unrecognised node type')

prog = Sub(
    Add(
        Lit(1),
        Lit(2)
    ),
    Var('x')
)

visualize(prog, format='png')

assert(eval(prog, { 'x': 3 }) == 0)

Running this example will open a new window with the following content:

License

Sweetener is generously licensed under the MIT license, in the hope that it inspires people to build new and cool software.

Metadata

Release files for sweetener 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sweetener 0.2.1
File Size Uploaded Provenance
sweetener-0.2.1.tar.gz 24.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for sweetener 0.2.1
File Interpreter ABI Platform
sweetener-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 53.9 kB

Release files / sweetener-0.2.1.tar.gz

Download URL sweetener-0.2.1.tar.gz
Size 24.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ac23cfd5989b30e2fc8e885843ac68a9053b98cddfd698a1986444192e757a83
BLAKE2b-256 checksum
How to use checksums
5fa60290b4c43c4fcc194997a249bc1f784ef2eadfb81333797f0c674d17e73a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.8

Release files / sweetener-0.2.1-py3-none-any.whl

Download URL sweetener-0.2.1-py3-none-any.whl
Size 29.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e18eaa3fa520d03f235ae27cc333c0b7a6cda703a3b2a6e80c862d203a3e9394
BLAKE2b-256 checksum
How to use checksums
477b5a937cb45716f10d05b44dd856f84f9b6d8a5d122f88c72555c483397a02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.8

Security for sweetener 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page