Skip to main content

Molang to Python Translator & interpreter written in pure Python.

Project description

molang

Tests PyPI Python Downloads Status Code style: black Issues Code style: black

Molang to Python Translator & interpreter written in pure Python.

Installation

Install the module with pip:

pip3 install molang

Update existing installation: pip3 install molang --upgrade

Links

Requirements

Name Description
pydantic Data validation using Python type hints

Features

  • Pure-Python Molang parser and AST representation
  • Interpreter: evaluate Molang expressions against a runtime context
  • Compiler / emitter: convert parsed AST back to Molang source
  • DSL convenience objects: query, variable, math, temp, array, etc.
  • Assignment support for dotted variables (e.g. variable.x = 1, temp.a.b = 2)
  • Full math.* function implementations and callable math functions (e.g. math.sqrt(4))
  • Function-call support in expressions and argument evaluation
  • CLI to run Molang files or execute inline Molang code

See the docs for more information.

Example

Parse / Compile / Eval

Parse, compile and evaluate with a context

import molang

expr = "math.sqrt(16) + variable.x"
node = molang.parse(expr)
print(molang.compile(expr))
ctx = molang.create_context()
ctx['variable']['x'] = 9
print(molang.eval(expr, ctx))  # prints 13.0

DSL usage

from molang.dsl import variable, math, query

variable.x = 4
print(variable.x)           # MolangExpr("variable.x = 4;") when used as assignment
print(math.sqrt(variable.x))

expr = query.block_state("minecraft:cardinal_direction") == "north"
print(expr) # MolangExpr("query.block_state('minecraft:cardinal_direction') == 'north'")

Assignment to dotted variables

import molang

ctx = molang.create_context()
molang.eval('variable.health = 20', ctx)
print(ctx['variable']['health'])  # 20.0

Function calls and math helpers

import molang

ctx = molang.create_context()
print(molang.eval('math.lerp(0, 10, 0.5)', ctx))  # 5.0

Pydantic Support

from pydantic import BaseModel
from molang.dsl import MolangExpr, query

class Test(BaseModel):
  condition: MolangExpr

x = Test(condition=query.block_state("facing") == "north")
print(x.model_dump()) # {"condition": "query.block_state('facing') == 'north'"}

Command-line interface

usage: molang [-h] [-c <cmd>] [-V] [<file>]

Run molang files

positional arguments:
  <file>                execute the molang code contained in a file.

options:
  -h, --help            show this help message and exit
  -c <cmd>              execute the molang code in cmd. cmd can be one or more statements separated by newlines, with significant leading whitespace as in normal module code.
  -V, --version         print the molang version number and exit.

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

molang-1.0.1.tar.gz (19.5 kB view details)

Uploaded Source

File details

Details for the file molang-1.0.1.tar.gz.

File metadata

  • Download URL: molang-1.0.1.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for molang-1.0.1.tar.gz
Algorithm Hash digest
SHA256 948ce2cb4298e0fae33e5542e08970537f56017599c7de650e8921cec1565224
MD5 f12c42fdfb81912000d6ce01f2bf00d1
BLAKE2b-256 6a077100907c0c5e7dfdd89122dc00898055259307ded89dcdd954a38e3c4857

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