interpreter_pattern
-
namespace interpreter_pattern
Namespace for the Interpreter pattern.
Typedefs
-
using Context = std::unordered_map<std::string, bool>
Context type used to provide variable bindings for interpretation. Maps variable names to boolean values.
-
class And : public interpreter_pattern::Expression
- #include <interpreter.hpp>
Nonterminal expression for logical AND.
Public Functions
-
inline And(std::unique_ptr<Expression> l, std::unique_ptr<Expression> r)
Construct an AND expression.
- Parameters:
l – Left operand.
r – Right operand.
-
inline virtual bool interpret(const Context &ctx) const override
Evaluate as logical conjunction of both operands.
-
inline And(std::unique_ptr<Expression> l, std::unique_ptr<Expression> r)
-
class Constant : public interpreter_pattern::Expression
- #include <interpreter.hpp>
Terminal expression representing a boolean constant.
Public Functions
-
inline explicit Constant(bool v)
Construct a constant.
- Parameters:
v – The boolean literal value.
-
inline virtual bool interpret(const Context&) const override
Return the stored literal value (context is ignored).
-
inline explicit Constant(bool v)
-
class Expression
- #include <interpreter.hpp>
Abstract expression in the grammar.
Subclassed by interpreter_pattern::And, interpreter_pattern::Constant, interpreter_pattern::Not, interpreter_pattern::Or, interpreter_pattern::Variable
Public Functions
-
virtual bool interpret(const Context &ctx) const = 0
Evaluate this expression given a
ctxof variable bindings.- Parameters:
ctx – The interpretation context.
- Returns:
The boolean result of evaluating the expression.
-
virtual bool interpret(const Context &ctx) const = 0
-
class Not : public interpreter_pattern::Expression
- #include <interpreter.hpp>
Nonterminal expression for logical NOT.
Public Functions
-
inline explicit Not(std::unique_ptr<Expression> e)
Construct a NOT expression.
- Parameters:
e – Operand expression.
-
inline virtual bool interpret(const Context &ctx) const override
Evaluate as logical negation of the operand.
-
inline explicit Not(std::unique_ptr<Expression> e)
-
class Or : public interpreter_pattern::Expression
- #include <interpreter.hpp>
Nonterminal expression for logical OR.
Public Functions
-
inline Or(std::unique_ptr<Expression> l, std::unique_ptr<Expression> r)
Construct an OR expression.
- Parameters:
l – Left operand.
r – Right operand.
-
inline virtual bool interpret(const Context &ctx) const override
Evaluate as logical disjunction of both operands.
-
inline Or(std::unique_ptr<Expression> l, std::unique_ptr<Expression> r)
-
class Variable : public interpreter_pattern::Expression
- #include <interpreter.hpp>
Terminal expression representing a variable lookup.
Public Functions
-
inline explicit Variable(std::string n)
Construct a variable reference.
- Parameters:
n – Variable name to look up in the context.
-
inline virtual bool interpret(const Context &ctx) const override
Look up the variable in
ctxand return its value.- Throws:
std::out_of_range – if the variable is not bound in
ctx.
-
inline explicit Variable(std::string n)
-
using Context = std::unordered_map<std::string, bool>