Skip to main content

A dice roller expression language with function support.

Project description

Expression Dice Roller

Install from PyPI: pip install expr_dice_roller

Quick command-line dice roller set up:

from expr_dice_roller import format_expression, evaluate

env = None

while True:
    inp = input("> ")
    try:
        print(format_expression(inp))
        res = evaluate(inp, env, True)
        env = res.environment
        print(res.representation, "=", res.value)
    except ValueError as e:
        print(e)

It is possible to serialize an expression by using format_expression(), which can then be used in evaluate().

Language Specifications

Arithmetic words with the operators +, -, *, /, and ^. Unary operators - and + are supported.

The language closely mirrors RPG Dice Roller's notations, with a few unimplemented or extra parts:

  • Fudge dice is not yet implemented.
  • Critical success / failures are not yet implemented.
  • Grouping rolls are not yet implemented.
  • New features includes variables and functions.

Variables

Variables have a name comprised of any string of characters that are not part of the language itself. There are no built-in variables except for the _ variable when evaluate() is called with assign_last_eval=True.

The _ variable, if provided, will point to the value of the last successful eval.

Functions

Functions are declared like so: f(x, y, z, ...) = expression. They can be immediately called if surrounded by brackets.

Example functions:

  • f(x) = 2 * x - doubles the input.
  • dice(count, sides) = (count)d(sides) - rolls count dice each with sides sides.
  • dice(4, 3) - calls the dice function, effectively rolling a 4d3.
  • (g(x, y)=f(x)-f(y))(2, 3) - immediately invoked function that calls another function.

Extraneous arguments beyond the arity of the function will be voided. Similarly, missing arguments below the arity of the function will be turned to 0.

Functions cannot be arbitrarily chained yet; currently, functions are parsed as special exceptions to certain rules.

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

expr_dice_roller-0.0.2.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

expr_dice_roller-0.0.2-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file expr_dice_roller-0.0.2.tar.gz.

File metadata

  • Download URL: expr_dice_roller-0.0.2.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for expr_dice_roller-0.0.2.tar.gz
Algorithm Hash digest
SHA256 2bb675a25b69e933a54ad3970137046fbed02fb46a8722f989c7a6b2c72cf140
MD5 7f004972e3580afdc81aa300b896ff41
BLAKE2b-256 59089eacb7972bd24d88c49e1164b70d62b7707cca1044724bdba9c80564f14b

See more details on using hashes here.

File details

Details for the file expr_dice_roller-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for expr_dice_roller-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 779dcdda4237eaef3b1f76e122910aa3e50bd4a9f4b02e969c23a1d6d8d0b738
MD5 7a6ae4f9df91546aae1e3e161277f936
BLAKE2b-256 6701539b04099cff40ecd8ffaf3dbf0b8c0695bae5980cbf2333fccfa1f0c9cf

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