Expressive AST-based dice roller
Project description
ast_roller
An expressive, abstract syntax tree-based dice roller.
Installation
TBD
Usage
As a library:
>>> from ast_roller.grammar import parser, transformer
>>> roll_string = '2 2d20 kh1 + 8'
>>> eval_root = transformer.transform(parser.parse(roll_string))
>>> result_root = eval_root.evaluate()
>>> print(result_root.pretty_print())
List Expansion: 2 (2d20 kh1 + 8)
Count: 2 => 2
Expression: (2d20 kh1 + 8)
Results: [21, 26]
0:
(2d20 kh1 + 8) => ([13, 8] + 8) => 13 + 8 = 21
1:
(2d20 kh1 + 8) => ([6, 18] + 8) => 18 + 8 = 26
As a command-line tool:
TBD
Design
This package processes dice rolls in tree main steps:
-
- Parse dice roll strings into syntax trees using Lark and a defined roll string grammar.
-
- Transforming the parse tree into an evaluation tree
-
- Recursively call
evaluate()from the root of the evaluation tree.
- Recursively call
This logic is spread across three files:
grammar.py: Contains the dice roll grammar and logic for transforming parse trees into eval trees.evaluators.py: Handles evaluation logic.results.py: Handles
Contributing
Contributions are welcome and should be pretty manageable. New roll features can be included by adding a definition for the new rule to the grammar and adding or modifying corresponding EvaluatorNode and ResultNode definitions to implement the actual logic.
Code changes must be accompanied by new or reconciled spec code in src/tests.
I feel lukewarm at best about the current output formats so if anyone has better ideas, I'd love an issue suggesting the changes.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ast_roller-0.0.1.tar.gz.
File metadata
- Download URL: ast_roller-0.0.1.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04acaff62bf2a631a9b63bbcf02df8c246ad0db751dcf3a4c5132e25b128acb3
|
|
| MD5 |
57a2ad271afef9a61f7db25749dd47d5
|
|
| BLAKE2b-256 |
a76faa2651bdba47d932c5661d15e8c6253532001ac24e78ee5ab6f72ddd785a
|
File details
Details for the file ast_roller-0.0.1-py3-none-any.whl.
File metadata
- Download URL: ast_roller-0.0.1-py3-none-any.whl
- Upload date:
- Size: 19.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a456f933e7725b338da81723aa2615ad55eb048b57f71b1c1c4f29b76f727d27
|
|
| MD5 |
3dd5ef0d83db0aa9b97ba3c84ef9003a
|
|
| BLAKE2b-256 |
2b6b1586df644429cea5bba2bc2735d444b55c4633e75507ac217d4abd7a086d
|