Calculator  Step 4
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 (char op, node operand)
 
 node (node left, char op, node right)
 
 node (identifier_list parameters, node definition)
 
 node (std::string name, node_list const &arguments)
 
 node (node const &)=default
 
 node (node &&)=default
 
nodeoperator= (node const &)=default
 
nodeoperator= (node &&)=default
 
 ~node ()
 
void print (std::ostream &stream, int indent=0) const
 
double evaluate () const
 
std::string to_string () const
 
identifier_list const & get_parameters () 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 20 of file node.cpp.

21 : pimpl_{std::make_shared<node_void>()}
22 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:92
node::node ( double  number)

Construct a numeric node.

Parameters
numberThe number

Definition at line 27 of file node.cpp.

28 : pimpl_{std::make_shared<node_number>(number)}
29 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:92
node::node ( std::string  identifier)

Construct an identifier node.

Parameters
identifierThe identifier

Definition at line 31 of file node.cpp.

32 : pimpl_{std::make_shared<node_identifier>(std::move(identifier))}
33 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:92
node::node ( char  op,
node  operand 
)

Construct a unary operator node.

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

Definition at line 35 of file node.cpp.

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

References make_binary_operator().

40 : pimpl_{make_binary_operator(left, op, right)}
41 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:92
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 ( identifier_list  parameters,
node  definition 
)

Construct a function definition node.

Parameters
parametersThe list of all the parameter names
definitionThe function definition

Definition at line 43 of file node.cpp.

44 : pimpl_{std::make_shared<node_function>(std::move(parameters), definition)}
45 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:92
node::node ( std::string  name,
node_list const &  arguments 
)

Construct a function call node.

Parameters
nameThe function name
argumentsThe function arguments

Definition at line 47 of file node.cpp.

48 : pimpl_{std::make_shared<node_function_call>(std::move(name), arguments)}
49 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:92
node::node ( node const &  )
default
node::node ( node &&  )
default
node::~node ( )

Definition at line 24 of file node.cpp.

25 {}

Member Function Documentation

double node::evaluate ( ) const

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

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

59 {
60  return pimpl_->evaluate();
61 }
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:92
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 69 of file node.cpp.

References pimpl_.

Referenced by node_function_call::evaluate_node().

71 {
72  return pimpl_->get_parameters();
73 }
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:92
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 51 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().

53 {
54  pimpl_->print(stream, indent);
55 }
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:92
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 63 of file node.cpp.

References pimpl_.

Referenced by BOOST_AUTO_TEST_CASE().

65 {
66  return pimpl_->to_string();
67 }
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:92

Member Data Documentation

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

Definition at line 92 of file node.hpp.

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


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