Calculator  Step 3
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
 
nodeoperator= (node const &)=default
 
nodeoperator= (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_implmake_binary_operator (node, char, node)
 Factory function to make the binary operator nodes. More...
 

Private Attributes

std::shared_ptr< 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 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 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:72
node::node ( double  number)

Construct a numeric node.

Parameters
numberThe number

Definition at line 24 of file node.cpp.

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

Construct an identifier node.

Parameters
identifierThe name of the identifier

Definition at line 28 of file node.cpp.

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

Construct an assignment node.

Parameters
identifierThe name of the variable that is the target of the assignment
numberthe 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 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:72
node::node ( char  op,
node  operand 
)

Construct a unary operator node.

Parameters
opThe operator character, e.g., '-'
operandThe operand node

Definition at line 36 of file node.cpp.

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

Construct a binary operator node.

Parameters
leftThe left-hand parse-tree
opThe operator character, e.g., '*'
rightThe right-hand operand

Definition at line 40 of file node.cpp.

References make_binary_operator().

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

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
streamThe output stream
indentThe 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 }
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 }
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: