Calculator  Step 3
node.cpp
Go to the documentation of this file.
1 #include <stdexcept>
2 
3 #include "node.hpp"
4 #include "node_impl.hpp"
5 
6 std::shared_ptr<node_impl> node::make_binary_operator(node left, char op, node right)
7 {
8  if (op == '+')
9  return std::make_shared<node_add>(left, right);
10  else if (op == '-')
11  return std::make_shared<node_subtract>(left, right);
12  else if (op == '*')
13  return std::make_shared<node_multiply>(left, right);
14  else if (op == '/')
15  return std::make_shared<node_divide>(left, right);
16  else
17  throw std::logic_error{"fatal error in make_binary_opreator: unknown operator: " + std::string(1,op)};
18 }
19 
21 : pimpl_{std::make_shared<node_void>()}
22 {}
23 
24 node::node(double number)
25 : pimpl_{std::make_shared<node_number>(number)}
26 {}
27 
28 node::node(std::string identifier)
29 : pimpl_{std::make_shared<node_identifier>(std::move(identifier))}
30 {}
31 
32 node::node(node identifier, node number)
33 : pimpl_{std::make_shared<node_assign>(identifier, number)}
34 {}
35 
36 node::node(char op, node operand)
37 : pimpl_{std::make_shared<node_negate>(operand)}
38 {}
39 
40 node::node(node left, char op, node right)
41 : pimpl_{make_binary_operator(left, op, right)}
42 {}
43 
44 void node::print(std::ostream& stream, int indent)
45 const
46 {
47  pimpl_->print(stream, indent);
48 }
49 
51 const
52 {
53  return pimpl_->evaluate();
54 }
55 
56 std::string node::to_string()
57 const
58 {
59  return pimpl_->to_string();
60 }
Definition: node.hpp:16
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:72
static std::shared_ptr< node_impl > make_binary_operator(node, char, node)
Factory function to make the binary operator nodes.
Definition: node.cpp:6
void print(std::ostream &stream, int indent=0) const
Definition: node.cpp:44
std::string to_string() const
Definition: node.cpp:56
double evaluate() const
Definition: node.cpp:50
node()
Definition: node.cpp:20