{"id":21025251,"url":"https://github.com/samvv/sweetener","last_synced_at":"2025-05-15T09:34:53.607Z","repository":{"id":57472681,"uuid":"255685380","full_name":"samvv/sweetener","owner":"samvv","description":"A base library for building tools that work with syntax","archived":false,"fork":false,"pushed_at":"2024-03-02T16:29:45.000Z","size":141,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-07-06T07:01:53.672Z","etag":null,"topics":["ast","library","parse-trees","syntax"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/sweetener","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samvv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-04-14T17:59:28.000Z","updated_at":"2024-01-27T23:25:05.000Z","dependencies_parsed_at":"2024-02-18T15:29:15.640Z","dependency_job_id":"f090aa53-0425-4e64-8c70-db448866fd60","html_url":"https://github.com/samvv/sweetener","commit_stats":{"total_commits":43,"total_committers":3,"mean_commits":"14.333333333333334","dds":0.4418604651162791,"last_synced_commit":"5145c480e934780a3db6fae0397db9bc5f7ced43"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samvv%2Fsweetener","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samvv%2Fsweetener/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samvv%2Fsweetener/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samvv%2Fsweetener/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samvv","download_url":"https://codeload.github.com/samvv/sweetener/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225342970,"owners_count":17459508,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","library","parse-trees","syntax"],"created_at":"2024-11-19T11:31:54.509Z","updated_at":"2024-11-19T11:31:55.094Z","avatar_url":"https://github.com/samvv.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"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\u003cimg src=\"https://raw.githubusercontent.com/samvv/sweetener/main/sample-record.png\" /\u003e\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 = {}) -\u003e 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\u003cimg src=\"https://raw.githubusercontent.com/samvv/sweetener/main/sample-tree.png\" /\u003e\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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamvv%2Fsweetener","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamvv%2Fsweetener","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamvv%2Fsweetener/lists"}