Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/samvv/sweetener
A base library for building tools that work with syntax
https://github.com/samvv/sweetener
ast library parse-trees syntax
Last synced: about 1 month ago
JSON representation
A base library for building tools that work with syntax
- Host: GitHub
- URL: https://github.com/samvv/sweetener
- Owner: samvv
- License: mit
- Created: 2020-04-14T17:59:28.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-03-02T16:29:45.000Z (10 months ago)
- Last Synced: 2024-07-06T07:01:53.672Z (6 months ago)
- Topics: ast, library, parse-trees, syntax
- Language: Python
- Homepage: https://pypi.org/project/sweetener
- Size: 138 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
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, visualizeclass MySimpleRecord(Record):
field_1: int
field_2: strr1 = 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:
```py
from sweetener import BaseNode, visualizeclass CalcNode(BaseNode):
passclass Expr(CalcNode):
passclass Add(Expr):
left: Expr
right: Exprclass Sub(Expr):
left: Expr
right: Exprclass Var(Expr):
name: strclass Lit(Expr):
value: intdef 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.