diff options
Diffstat (limited to 'graphs/cpp/visitor')
| -rw-r--r-- | graphs/cpp/visitor/compute_visitor.cc | 62 | ||||
| -rw-r--r-- | graphs/cpp/visitor/compute_visitor.hh | 26 | ||||
| -rw-r--r-- | graphs/cpp/visitor/print_visitor.cc | 58 | ||||
| -rw-r--r-- | graphs/cpp/visitor/print_visitor.hh | 25 |
4 files changed, 171 insertions, 0 deletions
diff --git a/graphs/cpp/visitor/compute_visitor.cc b/graphs/cpp/visitor/compute_visitor.cc new file mode 100644 index 0000000..1e4e6dd --- /dev/null +++ b/graphs/cpp/visitor/compute_visitor.cc @@ -0,0 +1,62 @@ +// +// Created by martial.simon on 2/27/25. +// + +#include "compute_visitor.hh" + +#include <stdexcept> + +#include "add.hh" +#include "div.hh" +#include "leaf.hh" +#include "mul.hh" +#include "sub.hh" +void visitor::ComputeVisitor::visit(const tree::Tree& e) +{ + Visitor::visit(e); + value_ = get_value(); +} +void visitor::ComputeVisitor::visit(const tree::Node& e) +{ + visit(*e.get_lhs()); + visit(*e.get_rhs()); + value_ = get_value(); +} +void visitor::ComputeVisitor::visit(const tree::AddNode& e) +{ + visit(*e.get_lhs()); + int lhs = get_value(); + Visitor::visit(*e.get_rhs()); + value_ = lhs + get_value(); +} +void visitor::ComputeVisitor::visit(const tree::SubNode& e) +{ + visit(*e.get_lhs()); + int lhs = get_value(); + Visitor::visit(*e.get_rhs()); + value_ = lhs - get_value(); +} +void visitor::ComputeVisitor::visit(const tree::MulNode& e) +{ + visit(*e.get_lhs()); + int lhs = get_value(); + Visitor::visit(*e.get_rhs()); + value_ = lhs * get_value(); +} +void visitor::ComputeVisitor::visit(const tree::DivNode& e) +{ + visit(*e.get_lhs()); + int lhs = get_value(); + visit(*e.get_rhs()); + if (get_value() == 0) + throw std::overflow_error{ "Divide by zero exception" }; + value_ = lhs / get_value(); +} +void visitor::ComputeVisitor::visit(const tree::Leaf& e) +{ + value_ = std::stoi(e.get_value()); +} +int visitor::ComputeVisitor::get_value() +{ + return value_; +} diff --git a/graphs/cpp/visitor/compute_visitor.hh b/graphs/cpp/visitor/compute_visitor.hh new file mode 100644 index 0000000..0426f29 --- /dev/null +++ b/graphs/cpp/visitor/compute_visitor.hh @@ -0,0 +1,26 @@ +// +// Created by martial.simon on 2/27/25. +// + +#pragma once + +#include "visitor.hh" + +namespace visitor +{ + class ComputeVisitor : public Visitor + { + public: + void visit(const tree::Tree& e); + void visit(const tree::Node& e); + void visit(const tree::AddNode& e); + void visit(const tree::SubNode& e); + void visit(const tree::MulNode& e); + void visit(const tree::DivNode& e); + void visit(const tree::Leaf& e); + int get_value(); + + private: + int value_ = 0; + }; +} // namespace visitor diff --git a/graphs/cpp/visitor/print_visitor.cc b/graphs/cpp/visitor/print_visitor.cc new file mode 100644 index 0000000..bcfd565 --- /dev/null +++ b/graphs/cpp/visitor/print_visitor.cc @@ -0,0 +1,58 @@ +// +// Created by martial.simon on 2/27/25. +// + +#include "print_visitor.hh" + +#include <iostream> + +#include "add.hh" +#include "div.hh" +#include "leaf.hh" +#include "mul.hh" +#include "sub.hh" +void visitor::PrintVisitor::visit(const tree::Tree& e) +{ + Visitor::visit(e); +} +void visitor::PrintVisitor::visit(const tree::Node& e) +{ + visit(*e.get_lhs()); + visit(*e.get_rhs()); +} +void visitor::PrintVisitor::visit(const tree::AddNode& e) +{ + std::cout << "("; + visit(*e.get_lhs()); + std::cout << " + "; + visit(*e.get_rhs()); + std::cout << ")"; +} +void visitor::PrintVisitor::visit(const tree::SubNode& e) +{ + std::cout << "("; + visit(*e.get_lhs()); + std::cout << " - "; + visit(*e.get_rhs()); + std::cout << ")"; +} +void visitor::PrintVisitor::visit(const tree::MulNode& e) +{ + std::cout << "("; + visit(*e.get_lhs()); + std::cout << " * "; + visit(*e.get_rhs()); + std::cout << ")"; +} +void visitor::PrintVisitor::visit(const tree::DivNode& e) +{ + std::cout << "("; + visit(*e.get_lhs()); + std::cout << " / "; + visit(*e.get_rhs()); + std::cout << ")"; +} +void visitor::PrintVisitor::visit(const tree::Leaf& e) +{ + std::cout << e.get_value(); +}
\ No newline at end of file diff --git a/graphs/cpp/visitor/print_visitor.hh b/graphs/cpp/visitor/print_visitor.hh new file mode 100644 index 0000000..aa0ea4c --- /dev/null +++ b/graphs/cpp/visitor/print_visitor.hh @@ -0,0 +1,25 @@ +// +// Created by martial.simon on 2/27/25. +// + +#pragma once + +#include "visitor.hh" + +namespace visitor +{ + class PrintVisitor : public Visitor + { + public: + void visit(const tree::Tree& e); + void visit(const tree::Node& e); + void visit(const tree::AddNode& e); + void visit(const tree::SubNode& e); + void visit(const tree::MulNode& e); + void visit(const tree::DivNode& e); + void visit(const tree::Leaf& e); + + private: + int value_ = 0; + }; +} // namespace visitor |
