- Main Page
- Classes
- Files

- Alphabetical List
- Class List
- Class Hierarchy
- Class Members

# node Class Reference

`#include <node.hpp>`

List of all members.

## Public Member Functions
| node ()
| node (std::istream &stream)
| node (long x)
| node (int x)
| node (double x)
| node (number num)
| node (std::string const &identifier)
| node (char op, node operand)
| node (node left, char op, node right)
| node (identifier_list const &parameters, node definition)
| node (std::string const &name, node_list const &arguments)
| node (node const &n)
| ~node ()
node & | operator= (node const &n)
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 node_impl * | make_binary_operator (node, char, node)
| Factory function to make the binary operator nodes.
## Private Attributes
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 27 of file node.hpp.

## Constructor & Destructor Documentation

node::node | ( |  | ) |

Construct a node of type void.

Definition at line 22 of file node.cpp.

    00023 : pimpl_(new node_void())
    00024 {}

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

Construct a node by reading from a library file.

Definition at line 26 of file node.cpp.

    00027 : pimpl_(node_impl::read_node(stream))
    00028 {}

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

Construct an integer node.

Parameters:

  | x | The integer

Definition at line 40 of file node.cpp.

    00041 : pimpl_(new node_number(number(x)))
    00042 {}

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

Construct an integer node.

Parameters:

  | x | The integer

Definition at line 36 of file node.cpp.

    00037 : pimpl_(new node_number(number(x)))
    00038 {}

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

Construct a floating-point node.

Parameters:

  | x | The value

Definition at line 44 of file node.cpp.

    00045 : pimpl_(new node_number(number(x)))
    00046 {}

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

Construct a numeric node.

Parameters:

  | num | The number

Definition at line 48 of file node.cpp.

    00049 : pimpl_(new node_number(num))
    00050 {}

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

Construct an identifier node.

Parameters:

  | identifier | The identifier

Definition at line 52 of file node.cpp.

    00053   : pimpl_(new node_identifier(identifier))
    00054   {}

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

    00057 : pimpl_(new node_negate(operand))
    00058 {}

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

    00061 : pimpl_(make_binary_operator(left, op, right))
    00062 {}

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

Construct a function definition node.

Parameters:

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

Definition at line 64 of file node.cpp.

    00065 : pimpl_(new node_function(parameters, definition))
    00066 {}

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

Construct a function call node.

Parameters:

  | name | The function name
  | arguments | The function arguments

Definition at line 68 of file node.cpp.

    00069 : pimpl_(new node_function_call(name, arguments))
    00070 {}

node::node | ( | node const & | n | ) |

Copy constructor. Increments the reference count.

Parameters:

  | n | The node to copy

Definition at line 30 of file node.cpp.

References node_impl::add_ref(), and pimpl_.

    00031 : pimpl_(n.pimpl_)
    00032 {
    00033   pimpl_->add_ref();
    00034 }

node::~node | ( |  | ) |

Destructor. Decrements the reference count.

Definition at line 72 of file node.cpp.

References node_impl::del_ref(), and pimpl_.

    00073 {
    00074   pimpl_->del_ref();
    00075 }

## Member Function Documentation

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

Copy assignment operator decrements the reference count.

Parameters:

  | n | the source of the assignment

Definition at line 77 of file node.cpp.

References node_impl::add_ref(), node_impl::del_ref(), and pimpl_.

    00078 {
    00079   n.pimpl_->add_ref();
    00080   pimpl_->del_ref();
    00081   pimpl_ = n.pimpl_;
    00082
    00083   return *this;
    00084 }

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

References pimpl_, and node_impl::print().

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

    00088 {
    00089   pimpl_->print(stream, indent);
    00090 }

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

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

Definition at line 92 of file node.cpp.

References node_impl::evaluate(), and pimpl_.

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

    00094 {
    00095   return pimpl_->evaluate();
    00096 }

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

References pimpl_, and node_impl::to_string().

Referenced by BOOST_AUTO_TEST_CASE().

    00100 {
    00101   return pimpl_->to_string();
    00102 }

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

References node_impl::get_parameters(), and pimpl_.

Referenced by node_function_call::evaluate_node(), and get_variable().

    00106 {
    00107   return pimpl_->get_parameters();
    00108 }

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

Save the node to a stream.

Parameters:

  | stream | The output stream

Definition at line 110 of file node.cpp.

References pimpl_, and node_impl::save().

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

    00112 {
    00113   pimpl_->save(stream);
    00114 }

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

    00009 {
    00010   if (op == '+')
    00011     return new node_add(left, right);
    00012   else if (op == '-')
    00013     return new node_subtract(left, right);
    00014   else if (op == '*')
    00015     return new node_multiply(left, right);
    00016   else if (op == '/')
    00017     return new node_divide(left, right);
    00018   else
    00019     throw calc_error("fatal error in make_binary_opreator: unknown operator: " + op);
    00020 }

## Member Data Documentation

node_impl* node::pimpl_`[private]`

Definition at line 120 of file node.hpp.

Referenced by evaluate(), get_parameters(), node(), operator=(), print(), save(), to_string(), and ~node().

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

- node.hpp
- node.cpp