Calculator Step 4

- Main Page
- Classes
- Files

- File List
- File Members

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
20 node::node()
21 : pimpl_{std::make_shared<node_void>()}
22 {}
23
24 node::~node()
25 {}
26
27 node::node(double number)
28 : pimpl_{std::make_shared<node_number>(number)}
29 {}
30
31 node::node(std::string identifier)
32 : pimpl_{std::make_shared<node_identifier>(std::move(identifier))}
33 {}
34
35 node::node(char op, node operand)
36 : pimpl_{std::make_shared<node_negate>(operand)}
37 {}
38
39 node::node(node left, char op, node right)
40 : pimpl_{make_binary_operator(left, op, right)}
41 {}
42
43 node::node(identifier_list parameters, node definition)
44 : pimpl_{std::make_shared<node_function>(std::move(parameters), definition)}
45 {}
46
47 node::node(std::string name, node_list const& arguments)
48 : pimpl_{std::make_shared<node_function_call>(std::move(name), arguments)}
49 {}
50
51 void node::print(std::ostream& stream, int indent)
52 const
53 {
54 pimpl_->print(stream, indent);
55 }
56
57 double node::evaluate()
58 const
59 {
60 return pimpl_->evaluate();
61 }
62
63 std::string node::to_string()
64 const
65 {
66 return pimpl_->to_string();
67 }
68
69 identifier_list const& node::get_parameters()
70 const
71 {
72 return pimpl_->get_parameters();
73 }
node::~node
~node()
Definition: node.cpp:24
node
Definition: node.hpp:26
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:92
node::make_binary_operator
static std::shared_ptr< node_impl > make_binary_operator(node, char, node)
Factory function to make the binary operator nodes.
Definition: node.cpp:6
identifier_list
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:19
node_impl.hpp
node::print
void print(std::ostream &stream, int indent=0) const
Definition: node.cpp:51
node::to_string
std::string to_string() const
Definition: node.cpp:63
node.hpp
node::evaluate
double evaluate() const
Definition: node.cpp:57
node::node
node()
Definition: node.cpp:20
node::get_parameters
identifier_list const & get_parameters() const
Definition: node.cpp:69
node_list
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:13