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

Module Contents

Classes

Ast

The base class of the abstract syntax tree

_Expr

An expression in the KeyLang grammar

_Term

A term in the KeyLang grammar

_Factor

A factor in the KeyLang grammar

_Float

A float literal in the KeyLang grammar

_Rar

A rarity variable in the KeyLang grammar

Functions

parse(s: str) → Ast

Parses the inputed KeyLang expression into a KeyLang abstract syntax

_tokenize(s: str) → List[str]

Converts the inputed KeyLang expression into a list of tokens

_parse_tokens(tokens: List[str]) → Ast

Parses the inputted tokens into a KeyLang abstract syntax tree

_parse_expr(tokens: List[str]) → Ast

Parses the inputted tokens as an expression

_parse_term(tokens: List[str]) → Ast

Parses the inputted tokens as a term

_parse_factor(tokens: List[str]) → Ast

Parses the inputted tokens as a factor

_parse_base(tokens: List[str]) → Ast

Parses the inputted tokens as a base

_parse_const(tokens: str) → Ast

Parses the inputted tokens as a constant

interpret(ast: Ast, rarity: float) → float

Evaluates the KeyLang AST to a value using the rarity

exception gachapy.keylang.SyntaxError[source]

Bases: BaseException

The exception thrown when a syntax error is found

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

Bases: object

The base class of the abstract syntax tree

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

Bases: Ast

An expression in the KeyLang grammar

__str__(self) str[source]

Return str(self).

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

Bases: Ast

A term in the KeyLang grammar

__str__(self) str[source]

Return str(self).

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

Bases: Ast

A factor in the KeyLang grammar

__str__(self) str[source]

Return str(self).

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

Bases: Ast

A float literal in the KeyLang grammar

__str__(self) str[source]

Return str(self).

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

Bases: Ast

A rarity variable in the KeyLang grammar

__str__(self) str[source]

Return str(self).

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

Parses the inputed KeyLang expression into a KeyLang abstract syntax tree

sstr

the expression to parse

Ast

the KeyLang AST representative of the expression

gachapy.keylang._tokenize(s: str) List[str][source]

Converts the inputed KeyLang expression into a list of tokens

sstr

the KeyLang expression to tokenize

List[str]

the list of tokens

gachapy.keylang._parse_tokens(tokens: List[str]) Ast[source]

Parses the inputted tokens into a KeyLang abstract syntax tree

tokensList[str]

the list of tokens to parse

Ast

the abstract syntax tree representative of the tokens

gachapy.keylang._parse_expr(tokens: List[str]) Ast[source]

Parses the inputted tokens as an expression

tokensList[str]

the list of tokens to parse

Ast

the abstract syntax tree representative of the expression

gachapy.keylang._parse_term(tokens: List[str]) Ast[source]

Parses the inputted tokens as a term

tokensList[str]

the list of tokens to parse

Ast

the abstract syntax tree representative of the term

gachapy.keylang._parse_factor(tokens: List[str]) Ast[source]

Parses the inputted tokens as a factor

tokensList[str]

the list of tokens to parse

Ast

the abstract syntax tree representative of the factor

gachapy.keylang._parse_base(tokens: List[str]) Ast[source]

Parses the inputted tokens as a base

tokensList[str]

the list of tokens to parse

Ast

the abstract syntax tree representative of the base

gachapy.keylang._parse_const(tokens: str) Ast[source]

Parses the inputted tokens as a constant

tokensList[str]

the list of tokens to parse

Ast

the abstract syntax tree representative of the constant

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

Evaluates the KeyLang AST to a value using the rarity

astAst

the AST of the expression

rarityfloat

the value for the rarity

float

the result of the expression