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
The base class of the abstract syntax tree |
|
An expression in the KeyLang grammar |
|
A term in the KeyLang grammar |
|
A factor in the KeyLang grammar |
|
A float literal in the KeyLang grammar |
|
A rarity variable in the KeyLang grammar |
Functions
|
Parses the inputed KeyLang expression into a KeyLang abstract syntax |
|
Converts the inputed KeyLang expression into a list of tokens |
|
Parses the inputted tokens into a KeyLang abstract syntax tree |
|
Parses the inputted tokens as an expression |
|
Parses the inputted tokens as a term |
|
Parses the inputted tokens as a factor |
|
Parses the inputted tokens as a base |
|
Parses the inputted tokens as a constant |
|
Evaluates the KeyLang AST to a value using the rarity |
- exception gachapy.keylang.SyntaxError[source]
Bases:
BaseExceptionThe exception thrown when a syntax error is found
- class gachapy.keylang.Ast(left=None, right=None, data=None)[source]
Bases:
objectThe base class of the abstract syntax tree
- class gachapy.keylang._Expr(left=None, right=None, data=None)[source]
Bases:
AstAn expression in the KeyLang grammar
- class gachapy.keylang._Term(left=None, right=None, data=None)[source]
Bases:
AstA term in the KeyLang grammar
- class gachapy.keylang._Factor(left=None, right=None, data=None)[source]
Bases:
AstA factor in the KeyLang grammar
- class gachapy.keylang._Float(left=None, right=None, data=None)[source]
Bases:
AstA float literal in the KeyLang grammar
- class gachapy.keylang._Rar(left=None, right=None, data=None)[source]
Bases:
AstA rarity variable in the KeyLang grammar
- 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
- gachapy.keylang._tokenize(s: str) List[str][source]
Converts the inputed KeyLang expression into a list of tokens
Parameters
- sstr
the KeyLang expression to tokenize
Returns
- 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
Parameters
- tokensList[str]
the list of tokens to parse
Returns
- 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
Parameters
- tokensList[str]
the list of tokens to parse
Returns
- 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
Parameters
- tokensList[str]
the list of tokens to parse
Returns
- 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
Parameters
- tokensList[str]
the list of tokens to parse
Returns
- 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
Parameters
- tokensList[str]
the list of tokens to parse
Returns
- Ast
the abstract syntax tree representative of the base