gachapy.keylang

The tokenizer, parser, and interpreter for KeyLang, the language used by gachapy to save and load custom rarity to drop rate functions KeyLang is a simple calculator-based language. Each token in the language must be separated by a space. The expressions follow standard order of operations. The following are allowed operations: + : addition - : subtraction * : multiplication / : division ^ : exponent ( … ) : parentheses (for overriding order of operations), where … is any expression any float literal R : rarity, to be substituted upon interpretation time

KeyLang Grammar

Expr -> Term + Expr | Term - Expr | Term Term -> Factor * Term | Factor / Term | Factor Factor -> Base ^ Factor | Base Base -> Const | ( Expr ) Const -> <float literal> | <rarity> Rar -> <rarity>

Example: 2 * ( 1 + R ) / 5 ^ 2 where R = rarity

Author: Jacob Kerr, 2021

class gachapy.keylang.Ast(left=None, right=None, data=None)[source]

The base class of the abstract syntax tree

exception gachapy.keylang.SyntaxError[source]

The exception thrown when a syntax error is found

gachapy.keylang.interpret(ast: Ast, rarity: float) float[source]

Evaluates the KeyLang AST to a value using the rarity

Parameters

astAst

the AST of the expression

rarityfloat

the value for the rarity

Returns

float

the result of the expression

gachapy.keylang.parse(s: str) Ast[source]

Parses the inputed KeyLang expression into a KeyLang abstract syntax tree

Parameters

sstr

the expression to parse

Returns

Ast

the KeyLang AST representative of the expression