Calculator  Step 5
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 (double number)
 
 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
 
nodeoperator= (node const &)=default
 
nodeoperator= (node &&)=default
 
 ~node ()=default
 
void print (std::ostream &stream, int indent=0) const
 
double 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_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 26 of file node.hpp.

Constructor & Destructor Documentation

node::node ( )

Construct a node of type void.

Definition at line 22 of file node.cpp.

23 : pimpl_{std::make_shared<node_void>()}
24 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:100
node::node ( std::istream &  stream)

Construct a node by reading from a library file.

Definition at line 26 of file node.cpp.

References node_impl::read_node().

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

Construct a numeric node.

Parameters
numberThe number

Definition at line 30 of file node.cpp.

31 : pimpl_{std::make_shared<node_number>(number)}
32 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:100
node::node ( std::string  identifier)

Construct an identifier node.

Parameters
identifierThe identifier

Definition at line 34 of file node.cpp.

35 : pimpl_{std::make_shared<node_identifier>(identifier)}
36 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:100
node::node ( char  op,
node  operand 
)

Construct a unary operator node.

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

Definition at line 38 of file node.cpp.

39 : pimpl_{std::make_shared<node_negate>(operand)}
40 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:100
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 42 of file node.cpp.

References make_binary_operator().

43 : pimpl_{make_binary_operator(left, op, right)}
44 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:100
static std::shared_ptr< node_impl > make_binary_operator(node, char, node)
Factory function to make the binary operator nodes.
Definition: node.cpp:8
node::node ( identifier_list  parameters,
node  definition 
)

Construct a function definition node.

Parameters
parametersThe list of all the parameter names
definitionThe function definition

Definition at line 46 of file node.cpp.

47 : pimpl_{std::make_shared<node_function>(parameters, definition)}
48 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:100
node::node ( std::string  name,
node_list  arguments 
)

Construct a function call node.

Parameters
nameThe function name
argumentsThe function arguments

Definition at line 50 of file node.cpp.

51 : pimpl_{std::make_shared<node_function_call>(name, arguments)}
52 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:100
node::node ( node const &  )
default
node::node ( node &&  )
default
node::~node ( )
default

Member Function Documentation

double node::evaluate ( ) const

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

Definition at line 60 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().

62 {
63  return pimpl_->evaluate();
64 }
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:100
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 72 of file node.cpp.

References pimpl_.

Referenced by node_function_call::evaluate_node().

74 {
75  return pimpl_->get_parameters();
76 }
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:100
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 8 of file node.cpp.

Referenced by node().

9 {
10  if (op == '+')
11  return std::make_shared<node_add>(left, right);
12  else if (op == '-')
13  return std::make_shared<node_subtract>(left, right);
14  else if (op == '*')
15  return std::make_shared<node_multiply>(left, right);
16  else if (op == '/')
17  return std::make_shared<node_divide>(left, right);
18  else
19  throw calc_error{"fatal error in make_binary_opreator: unknown operator: " + std::string(1,op)};
20 }
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 54 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().

56 {
57  pimpl_->print(stream, indent);
58 }
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:100
void node::save ( std::ostream &  stream) const

Save the node to a stream.

Parameters
streamThe output stream

Definition at line 78 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().

80 {
81  pimpl_->save(stream);
82 }
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:100
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 66 of file node.cpp.

References pimpl_.

Referenced by BOOST_AUTO_TEST_CASE().

68 {
69  return pimpl_->to_string();
70 }
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:100

Member Data Documentation

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

Definition at line 100 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: