From c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:08:27 +0200 Subject: add: graphs et rushs --- graphs/cpp/visitor/compute_visitor.cc | 62 +++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 graphs/cpp/visitor/compute_visitor.cc (limited to 'graphs/cpp/visitor/compute_visitor.cc') 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 + +#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_; +} -- cgit v1.2.3