// // Created by martial.simon on 2/27/25. // #include "compute_visitor.hh" #include #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_; }