node Class Reference

#include <node.hpp>

List of all members.

Public Member Functions

 node ()
 node (double number)
 node (std::string const &identifier)
 node (node identifier, node number)
 node (char op, node operand)
 node (node left, char op, node right)
 node (node const &n)
 ~node ()
nodeoperator= (node const &n)
void print (std::ostream &stream, int indent=0) const
double evaluate () const
std::string to_string () const

Static Private Member Functions

static node_implmake_binary_operator (node, char, node)
 Factory function to make the binary operator nodes.

Private Attributes

node_implpimpl_


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 15 of file node.hpp.


Constructor & Destructor Documentation

node::node (  ) 

Construct a node of type void.

Definition at line 20 of file node.cpp.

00021 : pimpl_(new node_void())
00022 {}

node::node ( double  number  ) 

Construct a numeric node.

Parameters:
number The number

Definition at line 30 of file node.cpp.

00031 : pimpl_(new node_number(number))
00032 {}

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

Construct an identifier node.

Parameters:
identifier The name of the identifier

Definition at line 34 of file node.cpp.

00035 : pimpl_(new node_identifier(identifier))
00036 {}

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

00039 : pimpl_(new node_assign(identifier, number))
00040 {}

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

00043 : pimpl_(new node_negate(operand))
00044 {}

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

00047 : pimpl_(make_binary_operator(left, op, right))
00048 {}

node::node ( node const &  n  ) 

Copy constructor. Increments the reference count.

Parameters:
n The node to copy

Definition at line 24 of file node.cpp.

References node_impl::add_ref(), and pimpl_.

00025 : pimpl_(n.pimpl_)
00026 {
00027   pimpl_->add_ref();
00028 }

node::~node (  ) 

Destructor. Decrements the reference count.

Definition at line 50 of file node.cpp.

References node_impl::del_ref(), and pimpl_.

00051 {
00052   pimpl_->del_ref();
00053 }


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

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

00056 {
00057   n.pimpl_->add_ref();
00058   pimpl_->del_ref();
00059   pimpl_ = n.pimpl_;
00060 
00061   return *this;
00062 }

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 64 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_assign::print_node().

00066 {
00067   pimpl_->print(stream, indent);
00068 }

double node::evaluate (  )  const

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

Definition at line 70 of file node.cpp.

References node_impl::evaluate(), and 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().

00072 {
00073   return pimpl_->evaluate();
00074 }

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

References pimpl_, and node_impl::to_string().

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

00078 {
00079   return pimpl_->to_string();
00080 }

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.

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


Member Data Documentation

node_impl* node::pimpl_ [private]

Definition at line 79 of file node.hpp.

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


The documentation for this class was generated from the following files:
Generated on Sun Nov 30 10:05:07 2008 for Calculator by  doxygen 1.5.3