Interpreter API Documentation

class Expression[source]

Bases: object

Represents 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:

bool

Returns:

The boolean result of evaluating the expression.

Raises:

NotImplementedError – If not implemented in a subclass.

class Constant(value)[source]

Bases: Expression

Terminal expression representing a boolean constant.

Parameters:

value (bool)

__init__(value)[source]

Initializes a constant expression.

Parameters:

value (bool) – The boolean literal value.

interpret(ctx)[source]

Returns the stored literal value (context is ignored).

Parameters:

ctx (Dict[str, bool]) – The interpretation context (unused).

Return type:

bool

Returns:

The stored boolean literal.

class Variable(name)[source]

Bases: Expression

Terminal expression representing a variable lookup.

Parameters:

name (str)

__init__(name)[source]

Initializes a variable expression.

Parameters:

name (str) – The variable name to look up in the context.

interpret(ctx)[source]

Looks up the variable value in the context.

Parameters:

ctx (Dict[str, bool]) – The interpretation context.

Return type:

bool

Returns:

The boolean value bound to the variable.

Raises:

KeyError – If the variable is not present in the context.

class Not(expr)[source]

Bases: Expression

Nonterminal expression for logical NOT.

Parameters:

expr (Expression)

__init__(expr)[source]

Initializes a NOT expression.

Parameters:

expr (Expression) – The operand to negate.

interpret(ctx)[source]

Evaluates logical negation of the operand.

Parameters:

ctx (Dict[str, bool]) – The interpretation context.

Return type:

bool

Returns:

The negated boolean value.

class And(left, right)[source]

Bases: Expression

Nonterminal expression for logical AND.

Parameters:
__init__(left, right)[source]

Initializes an AND expression.

Parameters:
interpret(ctx)[source]

Evaluates logical conjunction of both operands.

Parameters:

ctx (Dict[str, bool]) – The interpretation context.

Return type:

bool

Returns:

The result of left AND right.

class Or(left, right)[source]

Bases: Expression

Nonterminal expression for logical OR.

Parameters:
__init__(left, right)[source]

Initializes an OR expression.

Parameters:
interpret(ctx)[source]

Evaluates logical disjunction of both operands.

Parameters:

ctx (Dict[str, bool]) – The interpretation context.

Return type:

bool

Returns:

The result of left OR right.

class Parser(source)[source]

Bases: object

A 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:

Expression

Returns:

The root Expression AST node.

Raises:

ValueError – If there is leftover input or a syntax error.