Calculator Step 3

- Main Page
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
node Class Reference

`#include <node.hpp>`

## Public Member Functions
| node ()
| node (double number)
| node (std::string identifier)
| node (node identifier, node number)
| node (char op, node operand)
| node (node left, char op, node right)
| ~node ()=default
| node (node const &)=default
| node (node &&)=default
node & | operator= (node const &)=default
node & | operator= (node &&)=default
void | print (std::ostream &stream, int indent=0) const
double | evaluate () const
std::string | to_string () const

## Static Private Member Functions
static std::shared_ptr< node_impl > | make_binary_operator (node, char, node)
| Factory function to make the binary operator nodes. More...

## Private Attributes
std::shared_ptr< node_impl > | pimpl_

## Detailed Description

Wrapper class for all parse-tree nodes. The actual parse tree nodes derive from node_impl. This class uses the pimpl idiom to make it easier to work with node objects.

Definition at line 16 of file node.hpp.

## Constructor & Destructor Documentation

node::node | ( |  | ) |

Construct a node of type void.

Definition at line 20 of file node.cpp.

21 : pimpl_{std::make_shared<node_void>()}
22 {}
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:72

node::node | ( | double | number | ) |

Construct a numeric node.

Parameters

  number | The number

Definition at line 24 of file node.cpp.

25 : pimpl_{std::make_shared<node_number>(number)}
26 {}
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:72

node::node | ( | std::string | identifier | ) |

Construct an identifier node.

Parameters

  identifier | The name of the identifier

Definition at line 28 of file node.cpp.

29 : pimpl_{std::make_shared<node_identifier>(std::move(identifier))}
30 {}
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:72

node::node | ( | node | identifier,
|  | node | number
| ) |  |

Construct an assignment node.

Parameters

  identifier | The name of the variable that is the target of the assignment
  number | the node for the expression to assign to the variable

Definition at line 32 of file node.cpp.

33 : pimpl_{std::make_shared<node_assign>(identifier, number)}
34 {}
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:72

node::node | ( | char | op,
|  | node | operand
| ) |  |

Construct a unary operator node.

Parameters

  op | The operator character, e.g., `'-'`
  operand | The operand node

Definition at line 36 of file node.cpp.

37 : pimpl_{std::make_shared<node_negate>(operand)}
38 {}
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:72

node::node | ( | node | left,
|  | char | op,
|  | node | right
| ) |  |

Construct a binary operator node.

Parameters

  left | The left-hand parse-tree
  op | The operator character, e.g., `'*'`
  right | The right-hand operand

Definition at line 40 of file node.cpp.

References make_binary_operator().

41 : pimpl_{make_binary_operator(left, op, right)}
42 {}
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:72
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

node::~node | ( |  | ) |
| default

node::node | ( | node const & |  | ) |
| default

node::node | ( | node && |  | ) |
| default

## Member Function Documentation

double node::evaluate | ( |  | ) | const

Evaluate a parse tree. Evaluate and tree and return the result.

Definition at line 50 of file node.cpp.

References pimpl_.

Referenced by BOOST_AUTO_TEST_CASE(), node_binary::evaluate_left(), node_unary::evaluate_operand(), node_binary::evaluate_right(), node_assign::evaluate_value(), and parse_loop().

52 {
53 return pimpl_->evaluate();
54 }
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:72

std::shared_ptr< node_impl > node::make_binary_operator | ( | node | left,
|  | char | op,
|  | node | right
| ) |  |
| static private

Factory function to make the binary operator nodes.

Definition at line 6 of file node.cpp.

Referenced by node().

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 }

node& node::operator= | ( | node const & |  | ) |
| default

node& node::operator= | ( | node && |  | ) |
| default

void node::print | ( | std::ostream & | stream,
|  | int | indent = `0`
| ) |  | const

Print a tree. For debugging, print a visual representation of the parse tree. Each level increments the `indent` to make the tree structure evident.

Parameters

  stream | The output stream
  indent | The indentation.

Definition at line 44 of file node.cpp.

References pimpl_.

Referenced by node_assign::print_node(), node_negate::print_node(), node_add::print_node(), node_subtract::print_node(), node_multiply::print_node(), and node_divide::print_node().

46 {
47 pimpl_->print(stream, indent);
48 }
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:72

std::string node::to_string | ( |  | ) | const

Return a string representation. For an identifier node, return the identifier. For all other nodes, return a string representation of the evaluated value.

Definition at line 56 of file node.cpp.

References pimpl_.

Referenced by BOOST_AUTO_TEST_CASE(), and node_assign::get_identifier().

58 {
59 return pimpl_->to_string();
60 }
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:72

## Member Data Documentation

std::shared_ptr<node_impl> node::pimpl_
| private

Definition at line 72 of file node.hpp.

Referenced by evaluate(), print(), and to_string().

The documentation for this class was generated from the following files:

- node.hpp
- node.cpp