Interpreter API Documentation
- class Expression[source]
Bases:
objectRepresents an expression in a simple boolean grammar. All concrete expressions must implement the interpret method.
- interpret(ctx)[source]
Interprets the expression under the provided context.
- Parameters:
ctx (
Dict[str,bool]) – A dictionary mapping variable names to boolean values.- Return type:
- Returns:
The boolean result of evaluating the expression.
- Raises:
NotImplementedError – If not implemented in a subclass.
- class Constant(value)[source]
Bases:
ExpressionTerminal expression representing a boolean constant.
- Parameters:
value (bool)
- class Variable(name)[source]
Bases:
ExpressionTerminal expression representing a variable lookup.
- Parameters:
name (str)
- class Not(expr)[source]
Bases:
ExpressionNonterminal expression for logical NOT.
- Parameters:
expr (Expression)
- __init__(expr)[source]
Initializes a NOT expression.
- Parameters:
expr (
Expression) – The operand to negate.
- class And(left, right)[source]
Bases:
ExpressionNonterminal expression for logical AND.
- Parameters:
left (Expression)
right (Expression)
- __init__(left, right)[source]
Initializes an AND expression.
- Parameters:
left (
Expression) – The left operand.right (
Expression) – The right operand.
- class Or(left, right)[source]
Bases:
ExpressionNonterminal expression for logical OR.
- Parameters:
left (Expression)
right (Expression)
- __init__(left, right)[source]
Initializes an OR expression.
- Parameters:
left (
Expression) – The left operand.right (
Expression) – The right operand.
- class Parser(source)[source]
Bases:
objectA minimal recursive-descent parser for a boolean DSL.
- Grammar (EBNF):
Expr := OrExpr OrExpr := AndExpr { ‘|’ AndExpr } AndExpr := NotExpr { ‘&’ NotExpr } NotExpr := ‘!’ NotExpr | Primary Primary := ‘true’ | ‘false’ | Identifier | ‘(’ Expr ‘)’ Identifier := [A-Za-z_][A-Za-z0-9_]*
- Parameters:
source (str)
- __init__(source)[source]
Initializes the parser with the input string.
- Parameters:
source (
str) – The source string to parse.
- parse()[source]
Parses the entire input as an expression.
- Return type:
- Returns:
The root Expression AST node.
- Raises:
ValueError – If there is leftover input or a syntax error.