Calculator  Step 6
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 (long x)
 
 node (int x)
 
 node (double x)
 
 node (number num)
 
 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
 
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 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 28 of file node.hpp.

Constructor & Destructor Documentation

node::node ( )

Construct a node of type void.

Definition at line 23 of file node.cpp.

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

Construct a node by reading from a library file.

Definition at line 27 of file node.cpp.

References node_impl::read_node().

29 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
static std::shared_ptr< node_impl > read_node(std::istream &stream)
Definition: node_impl.cpp:64
node::node ( long  x)

Construct an integer node.

Parameters
xThe integer

Definition at line 35 of file node.cpp.

36 : pimpl_{std::make_shared<node_number>(number{x})}
37 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
node::node ( int  x)

Construct an integer node.

Parameters
xThe integer

Definition at line 31 of file node.cpp.

32 : pimpl_{std::make_shared<node_number>(number{x})}
33 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
node::node ( double  x)

Construct a floating-point node.

Parameters
xThe value

Definition at line 39 of file node.cpp.

40 : pimpl_{std::make_shared<node_number>(number{x})}
41 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
node::node ( number  num)

Construct a numeric node.

Parameters
numThe number

Definition at line 43 of file node.cpp.

44 : pimpl_{std::make_shared<node_number>(num)}
45 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
node::node ( std::string  identifier)

Construct an identifier node.

Parameters
identifierThe identifier

Definition at line 47 of file node.cpp.

48  : pimpl_{std::make_shared<node_identifier>(std::move(identifier))}
49  {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
node::node ( char  op,
node  operand 
)

Construct a unary operator node.

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

Definition at line 51 of file node.cpp.

52 : pimpl_{std::make_shared<node_negate>(operand)}
53 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
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 55 of file node.cpp.

References make_binary_operator().

56 : pimpl_{make_binary_operator(left, op, right)}
57 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
static std::shared_ptr< node_impl > make_binary_operator(node, char, node)
Factory function to make the binary operator nodes.
Definition: node.cpp:9
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 59 of file node.cpp.

60 : pimpl_{std::make_shared<node_function>(std::move(parameters), definition)}
61 {}
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
node::node ( std::string  name,
node_list  arguments 
)

Construct a function call node.

Parameters
nameThe function name
argumentsThe function arguments

Definition at line 63 of file node.cpp.

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

Member Function Documentation

number node::evaluate ( ) const

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

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

75 {
76  return pimpl_->evaluate();
77 }
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
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 85 of file node.cpp.

References pimpl_.

Referenced by node_function_call::evaluate_node().

87 {
88  return pimpl_->get_parameters();
89 }
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
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 9 of file node.cpp.

Referenced by node().

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

69 {
70  pimpl_->print(stream, indent);
71 }
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
void node::save ( std::ostream &  stream) const

Save the node to a stream.

Parameters
streamThe output stream

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

93 {
94  pimpl_->save(stream);
95 }
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114
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 79 of file node.cpp.

References pimpl_.

Referenced by BOOST_AUTO_TEST_CASE().

81 {
82  return pimpl_->to_string();
83 }
std::shared_ptr< node_impl > pimpl_
Definition: node.hpp:114

Member Data Documentation

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

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