sweetener


Namesweetener JSON
Version 0.2.0 PyPI version JSON
download
home_pagehttps://github.com/samvv/sweetener
SummaryA base library for writing compilers and interpreters
upload_time2024-02-18 19:46:49
maintainer
docs_urlNone
authorSam Vervaeck
requires_python>=3.12, <4
license
keywords productivity algorithms data-structures development programming
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Sweetener
=========

[![Python package](https://github.com/samvv/sweetener/actions/workflows/python-package.yml/badge.svg)](https://github.com/samvv/sweetener/actions/workflows/python-package.yml) [![Coverage Status](https://coveralls.io/repos/github/samvv/sweetener/badge.svg?branch=main)](https://coveralls.io/github/samvv/sweetener?branch=main) [![PyPI version](https://badge.fury.io/py/sweetener.svg)](https://pypi.org/project/sweetener)

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][1]! 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.

[1]: https://github.com/samvv/sweetener/issues

## Examples

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

```py
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:

<img src="https://raw.githubusercontent.com/samvv/sweetener/main/sample-record.png" />

```py
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:

<img src="https://raw.githubusercontent.com/samvv/sweetener/main/sample-tree.png" />

## License

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


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/samvv/sweetener",
    "name": "sweetener",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.12, <4",
    "maintainer_email": "",
    "keywords": "productivity,algorithms,data-structures,development,programming",
    "author": "Sam Vervaeck",
    "author_email": "samvv@pm.me",
    "download_url": "https://files.pythonhosted.org/packages/26/24/9619d79c90df6c918b44d819636dad8d1feca0ee2b70e4ac4c7af7cfda0d/sweetener-0.2.0.tar.gz",
    "platform": null,
    "description": "Sweetener\n=========\n\n[![Python package](https://github.com/samvv/sweetener/actions/workflows/python-package.yml/badge.svg)](https://github.com/samvv/sweetener/actions/workflows/python-package.yml) [![Coverage Status](https://coveralls.io/repos/github/samvv/sweetener/badge.svg?branch=main)](https://coveralls.io/github/samvv/sweetener?branch=main) [![PyPI version](https://badge.fury.io/py/sweetener.svg)](https://pypi.org/project/sweetener)\n\nSweetener is a Python library for quickly prototyping compilers and\ninterpreters. Extra care has been taken to make the algorithms as flexible as\npossible to fit as many use cases. If you're still missing something, do not\nhesitate to file an [issue][1]! In particular, this library contains the\nfollowing goodies:\n\n - **A typed record type** that allows you to build PODs very quickly.\n - **Base classes for an AST/CST**, including reflection procedures.\n - **A means for writing diagnostics** that can print part of your source code\n   alongside your error messages.\n - **Generic procedures** like `clone`, `equal` and `lte` that work on most\n   types in the standard library and you can specialize for your own types.\n\n[1]: https://github.com/samvv/sweetener/issues\n\n## Examples\n\nHere's an example of using the `Record` type to define a record that holds two\nfields and visualizes tham using GraphViz:\n\n```py\nfrom sweetener import Record, equal, visualize\n\nclass MySimpleRecord(Record):\n    field_1: int\n    field_2: str\n\nr1 = MySimpleRecord('foo', 42)\nr2 = MySimpleRecod(field_1='foo', field_2=42)\n\nassert(equal(r1, r2))\n\nvisualize(r1)\n```\n\nRunning this program will open a new window with the following content:\n\n<img src=\"https://raw.githubusercontent.com/samvv/sweetener/main/sample-record.png\" />\n\n```py\nfrom sweetener import BaseNode, visualize\n\nclass CalcNode(BaseNode):\n    pass\n\nclass Expr(CalcNode):\n    pass\n\nclass Add(Expr):\n    left: Expr\n    right: Expr\n\nclass Sub(Expr):\n    left: Expr\n    right: Expr\n\nclass Var(Expr):\n    name: str\n\nclass Lit(Expr):\n    value: int\n\ndef eval(node: Expr, vars = {}) -> int:\n    if isinstance(node, Add):\n        return eval(node.left, vars) + eval(node.right, vars)\n    if isinstance(node, Sub):\n        return eval(node.left, vars) - eval(node.right, vars)\n    if isinstance(node, Lit):\n        return node.value\n    if isinstance(node, Var):\n        return vars[node.name]\n    raise RuntimeError('Could not evaluate a node: unrecognised node type')\n\nprog = Sub(\n    Add(\n        Lit(1),\n        Lit(2)\n    ),\n    Var('x')\n)\n\nvisualize(prog, format='png')\n\nassert(eval(prog, { 'x': 3 }) == 0)\n```\n\nRunning this example will open a new window with the following content:\n\n<img src=\"https://raw.githubusercontent.com/samvv/sweetener/main/sample-tree.png\" />\n\n## License\n\nSweetener is generously licensed under the MIT license, in the hope that it\ninspires people to build new and cool software.\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A base library for writing compilers and interpreters",
    "version": "0.2.0",
    "project_urls": {
        "Bug Reports": "https://github.com/samvv/sweetener/issues",
        "Homepage": "https://github.com/samvv/sweetener",
        "Source": "https://github.com/samvv/sweetener/"
    },
    "split_keywords": [
        "productivity",
        "algorithms",
        "data-structures",
        "development",
        "programming"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "60572765630fba187220a5f696362b40a864a3d327febaa31374049f216cc67d",
                "md5": "d04c10b93e7a5f368c56ec5463d1fb73",
                "sha256": "cd11e49bdad9488a436363ac548b68cc46a3fcdfad87a616105ab300600051ab"
            },
            "downloads": -1,
            "filename": "sweetener-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d04c10b93e7a5f368c56ec5463d1fb73",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.12, <4",
            "size": 28885,
            "upload_time": "2024-02-18T19:46:47",
            "upload_time_iso_8601": "2024-02-18T19:46:47.989285Z",
            "url": "https://files.pythonhosted.org/packages/60/57/2765630fba187220a5f696362b40a864a3d327febaa31374049f216cc67d/sweetener-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "26249619d79c90df6c918b44d819636dad8d1feca0ee2b70e4ac4c7af7cfda0d",
                "md5": "084f3b3d6e5f5235ec7cdd292919de34",
                "sha256": "ca64f22ed908c19bb32725d7385aeb9304a07706c30b18f497b8d57b48abbdcf"
            },
            "downloads": -1,
            "filename": "sweetener-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "084f3b3d6e5f5235ec7cdd292919de34",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.12, <4",
            "size": 24754,
            "upload_time": "2024-02-18T19:46:49",
            "upload_time_iso_8601": "2024-02-18T19:46:49.571051Z",
            "url": "https://files.pythonhosted.org/packages/26/24/9619d79c90df6c918b44d819636dad8d1feca0ee2b70e4ac4c7af7cfda0d/sweetener-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-18 19:46:49",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "samvv",
    "github_project": "sweetener",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "sweetener"
}
        
Elapsed time: 0.20110s