Skip to content

Instantly share code, notes, and snippets.

@morgangallant
Last active August 19, 2019 20:31
Show Gist options
  • Save morgangallant/e5f37f40e97f36149eaad04b419a48c9 to your computer and use it in GitHub Desktop.
Save morgangallant/e5f37f40e97f36149eaad04b419a48c9 to your computer and use it in GitHub Desktop.
#include <functional>
#include <initializer_list>
#include <optional>
#include <string>
#include <vector>
// A single node of our parse tree.
class Node {
public:
// Construct a node using a list of triggers.
explicit Node(std::initializer_list<std::string> triggers)
: triggers_(triggers) {}
// Destruct owned memory.
~Node() {
for (auto node_ptr : nodes_) {
delete node_ptr;
}
}
// Append a node to this node.
void add_node(Node *node) { nodes_.push_back(std::move(node)); }
// Give this node an action.
void set_action(std::function<void()> func) { action_ = std::optional(func); }
private:
// Trigger words for this node.
std::vector<std::string> triggers_;
// Sub-nodes.
std::vector<Node *> nodes_;
// Optional action. A leaf node will have a non-empty action.
std::optional<std::function<void()>> action_ = std::nullopt;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment