Interpreter Header

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.

Private Members

std::unique_ptr<Expression> left

Left operand.

std::unique_ptr<Expression> right

Right operand.

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).

Private Members

bool value

Stored boolean value.

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 ~Expression() = default
virtual bool interpret(const Context &ctx) const = 0

Evaluate this expression given a ctx of variable bindings.

Parameters:

ctx – The interpretation context.

Returns:

The boolean result of evaluating the expression.

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.

Private Members

std::unique_ptr<Expression> expr

Operand to negate.

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.

Private Members

std::unique_ptr<Expression> left

Left operand.

std::unique_ptr<Expression> right

Right operand.

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:

nVariable name to look up in the context.

inline virtual bool interpret(const Context &ctx) const override

Look up the variable in ctx and return its value.

Throws:

std::out_of_range – if the variable is not bound in ctx.

Private Members

std::string name

Variable identifier.

Inner Classes

Inner Namespaces