Calculator Step 6

- 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 (std::istream &stream)
| node (long x)
| node (int x)
| node (double x)
| node (number num)
| node (std::string identifier)
| node (char op, node operand)
| node (node left, char op, node right)
| node (identifier_list parameters, node definition)
| node (std::string name, node_list arguments)
| node (node const &)=default
| node (node &&)=default
node & | operator= (node const &)=default
node & | operator= (node &&)=default
| ~node ()=default
void | print (std::ostream &stream, int indent=0) const
number | evaluate () const
std::string | to_string () const
identifier_list const & | get_parameters () const
void | save (std::ostream &stream) 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 28 of file node.hpp.

## Constructor & Destructor Documentation

node::node | ( |  | ) |

Construct a node of type void.

Definition at line 23 of file node.cpp.

24 : pimpl_{std::make_shared<node_void>()}
25 {}
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

node::node | ( | std::istream & | stream | ) |

Construct a node by reading from a library file.

Definition at line 27 of file node.cpp.

References node_impl::read_node().

28 : pimpl_{node_impl::read_node(stream)}
29 {}
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
node_impl::read_node
static std::shared_ptr< node_impl > read_node(std::istream &stream)
Definition: node_impl.cpp:64

node::node | ( | long | x | ) |

Construct an integer node.

Parameters

  x | The integer

Definition at line 35 of file node.cpp.

36 : pimpl_{std::make_shared<node_number>(number{x})}
37 {}
number
Definition: number.hpp:16
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

node::node | ( | int | x | ) |

Construct an integer node.

Parameters

  x | The integer

Definition at line 31 of file node.cpp.

32 : pimpl_{std::make_shared<node_number>(number{x})}
33 {}
number
Definition: number.hpp:16
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

node::node | ( | double | x | ) |

Construct a floating-point node.

Parameters

  x | The value

Definition at line 39 of file node.cpp.

40 : pimpl_{std::make_shared<node_number>(number{x})}
41 {}
number
Definition: number.hpp:16
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

node::node | ( | number | num | ) |

Construct a numeric node.

Parameters

  num | The number

Definition at line 43 of file node.cpp.

44 : pimpl_{std::make_shared<node_number>(num)}
45 {}
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

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

Construct an identifier node.

Parameters

  identifier | The identifier

Definition at line 47 of file node.cpp.

48 : pimpl_{std::make_shared<node_identifier>(std::move(identifier))}
49 {}
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

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 51 of file node.cpp.

52 : pimpl_{std::make_shared<node_negate>(operand)}
53 {}
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

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 55 of file node.cpp.

References make_binary_operator().

56 : pimpl_{make_binary_operator(left, op, right)}
57 {}
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

node::node | ( | identifier_list | parameters,
|  | node | definition
| ) |  |

Construct a function definition node.

Parameters

  parameters | The list of all the parameter names
  definition | The function definition

Definition at line 59 of file node.cpp.

60 : pimpl_{std::make_shared<node_function>(std::move(parameters), definition)}
61 {}
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

node::node | ( | std::string | name,
|  | node_list | arguments
| ) |  |

Construct a function call node.

Parameters

  name | The function name
  arguments | The function arguments

Definition at line 63 of file node.cpp.

64 : pimpl_{std::make_shared<node_function_call>(std::move(name), std::move(arguments))}
65 {}
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

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

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

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

## Member Function Documentation

number node::evaluate | ( |  | ) | const

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

Definition at line 73 of file node.cpp.

References pimpl_.

Referenced by BOOST_AUTO_TEST_CASE(), node_binary::evaluate_left(), node_identifier::evaluate_node(), node_function::evaluate_node(), node_unary::evaluate_operand(), node_binary::evaluate_right(), and parser::get_statement().

75 {
76 return pimpl_->evaluate();
77 }
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

identifier_list const & node::get_parameters | ( |  | ) | const

Return a list of parameter names. Most node return a reference to a global, static, const, empty list. Only function nodes return a list of parameters.

Definition at line 85 of file node.cpp.

References pimpl_.

Referenced by node_function_call::evaluate_node().

87 {
88 return pimpl_->get_parameters();
89 }
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

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 9 of file node.cpp.

Referenced by node().

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 }
calc_error
Definition: calc_error.hpp:11

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 67 of file node.cpp.

References pimpl_.

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

69 {
70 pimpl_->print(stream, indent);
71 }
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

void node::save | ( | std::ostream & | stream | ) | const

Save the node to a stream.

Parameters

  stream | The output stream

Definition at line 91 of file node.cpp.

References pimpl_.

Referenced by node_function::save_node(), node_negate::save_node(), node_add::save_node(), node_subtract::save_node(), node_multiply::save_node(), and node_divide::save_node().

93 {
94 pimpl_->save(stream);
95 }
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

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 79 of file node.cpp.

References pimpl_.

Referenced by BOOST_AUTO_TEST_CASE().

81 {
82 return pimpl_->to_string();
83 }
node::pimpl_
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

## Member Data Documentation

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

Definition at line 114 of file node.hpp.

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

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

- node.hpp
- node.cpp