Calculator Step 6

- Main Page
- Classes
- Files

- File List
- File Members

node.cpp
Go to the documentation of this file.
1 #include <istream>
2 #include <stdexcept>
3 #include <utility>
4
5 #include "calc_error.hpp"
6 #include "node.hpp"
7 #include "node_impl.hpp"
8
9 std::shared_ptr<node_impl> node::make_binary_operator(node left, char op, node right)
10 {
11 if (op == '+')
12 return std::make_shared<node_add>(left, right);
13 else if (op == '-')
14 return std::make_shared<node_subtract>(left, right);
15 else if (op == '*')
16 return std::make_shared<node_multiply>(left, right);
17 else if (op == '/')
18 return std::make_shared<node_divide>(left, right);
19 else
20 throw calc_error{"fatal error in make_binary_opreator: unknown operator: " + std::string(1,op)};
21 }
22
23 node::node()
24 : pimpl_{std::make_shared<node_void>()}
25 {}
26
27 node::node(std::istream& stream)
28 : pimpl_{node_impl::read_node(stream)}
29 {}
30
31 node::node(int x)
32 : pimpl_{std::make_shared<node_number>(number{x})}
33 {}
34
35 node::node(long x)
36 : pimpl_{std::make_shared<node_number>(number{x})}
37 {}
38
39 node::node(double x)
40 : pimpl_{std::make_shared<node_number>(number{x})}
41 {}
42
43 node::node(number num)
44 : pimpl_{std::make_shared<node_number>(num)}
45 {}
46
47 node::node(std::string identifier)
48 : pimpl_{std::make_shared<node_identifier>(std::move(identifier))}
49 {}
50
51 node::node(char op, node operand)
52 : pimpl_{std::make_shared<node_negate>(operand)}
53 {}
54
55 node::node(node left, char op, node right)
56 : pimpl_{make_binary_operator(left, op, right)}
57 {}
58
59 node::node(identifier_list parameters, node definition)
60 : pimpl_{std::make_shared<node_function>(std::move(parameters), definition)}
61 {}
62
63 node::node(std::string name, node_list arguments)
64 : pimpl_{std::make_shared<node_function_call>(std::move(name), std::move(arguments))}
65 {}
66
67 void node::print(std::ostream& stream, int indent)
68 const
69 {
70 pimpl_->print(stream, indent);
71 }
72
73 number node::evaluate()
74 const
75 {
76 return pimpl_->evaluate();
77 }
78
79 std::string node::to_string()
80 const
81 {
82 return pimpl_->to_string();
83 }
84
85 identifier_list const& node::get_parameters()
86 const
87 {
88 return pimpl_->get_parameters();
89 }
90
91 void node::save(std::ostream& stream)
92 const
93 {
94 pimpl_->save(stream);
95 }
calc_error
Definition: calc_error.hpp:11
number
Definition: number.hpp:16
node
Definition: node.hpp:28
calc_error.hpp
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
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:9
identifier_list
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:21
node_impl::read_node
static std::shared_ptr< node_impl > read_node(std::istream &stream)
Definition: node_impl.cpp:64
node_impl.hpp
node::print
void print(std::ostream &stream, int indent=0) const
Definition: node.cpp:67
node::evaluate
number evaluate() const
Definition: node.cpp:73
node::to_string
std::string to_string() const
Definition: node.cpp:79
node.hpp
node::node
node()
Definition: node.cpp:23
node::get_parameters
identifier_list const & get_parameters() const
Definition: node.cpp:85
node_list
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:15
node::save
void save(std::ostream &stream) const
Definition: node.cpp:91