blob: 1e4e6dda353b726a9f50f3dc848bfdbd303b5109 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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_;
}
|