Skip to main content

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:

    1. Parse dice roll strings into syntax trees using Lark and a defined roll string grammar.
    1. Transforming the parse tree into an evaluation tree
    1. Recursively call evaluate() from the root of the evaluation tree.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ast_roller-0.0.1.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ast_roller-0.0.1-py3-none-any.whl (19.6 kB view details)

Uploaded Python 3

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

Hashes for ast_roller-0.0.1.tar.gz
Algorithm Hash digest
SHA256 04acaff62bf2a631a9b63bbcf02df8c246ad0db751dcf3a4c5132e25b128acb3
MD5 57a2ad271afef9a61f7db25749dd47d5
BLAKE2b-256 a76faa2651bdba47d932c5661d15e8c6253532001ac24e78ee5ab6f72ddd785a

See more details on using hashes here.

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

Hashes for ast_roller-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a456f933e7725b338da81723aa2615ad55eb048b57f71b1c1c4f29b76f727d27
MD5 3dd5ef0d83db0aa9b97ba3c84ef9003a
BLAKE2b-256 2b6b1586df644429cea5bba2bc2735d444b55c4633e75507ac217d4abd7a086d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page