Calculator  Step 5
Public Member Functions | Static Public Member Functions | Private Member Functions | List of all members
node_impl Class Referenceabstract

#include <node_impl.hpp>

Inheritance diagram for node_impl:
node_binary node_function node_function_call node_identifier node_number node_unary node_void node_add node_divide node_multiply node_subtract node_negate

Public Member Functions

 node_impl ()
 
virtual ~node_impl ()
 
void print (std::ostream &stream, int indent) const
 
double evaluate () const
 
std::string to_string () const
 
identifier_list const & get_parameters () const
 
void save (std::ostream &stream) const
 

Static Public Member Functions

static std::shared_ptr< node_implread_node (std::istream &stream)
 

Private Member Functions

 node_impl (node_impl const &n)
 not implemented More...
 
node_imploperator= (node_impl const &n)
 not implemented More...
 
virtual void print_node (std::ostream &stream, int indent) const =0
 
virtual double evaluate_node () const =0
 
virtual std::string evaluate_string () const
 
virtual identifier_list const & evaluate_parameters () const
 
virtual void save_node (std::ostream &stream) const =0
 

Detailed Description

Base class for all parse tree nodes. The node classes all derive from node_impl. Each derived class overrides print_node(), for debugging and evalute_node() to evaluate the node. A few classes also override evaluate_string(), but most classes can inherit the node_impl::evaluate_string implementation, which prints the result of calling evaluate().

This class primarily manages the reference count.

Definition at line 20 of file node_impl.hpp.

Constructor & Destructor Documentation

node_impl::node_impl ( )

Definition at line 12 of file node_impl.cpp.

13 {}
node_impl::~node_impl ( )
virtual

Definition at line 15 of file node_impl.cpp.

16 {}
node_impl::node_impl ( node_impl const &  n)
private

not implemented

Member Function Documentation

double node_impl::evaluate ( ) const

Definition at line 24 of file node_impl.cpp.

References evaluate_node().

Referenced by evaluate_string().

26 {
27  return evaluate_node();
28 }
virtual double evaluate_node() const =0
virtual double node_impl::evaluate_node ( ) const
privatepure virtual
identifier_list const & node_impl::evaluate_parameters ( ) const
privatevirtual

Reimplemented in node_function.

Definition at line 50 of file node_impl.cpp.

Referenced by get_parameters().

52 {
53  static identifier_list const empty{};
54  return empty;
55 }
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:19
std::string node_impl::evaluate_string ( ) const
privatevirtual

Reimplemented in node_identifier, and node_void.

Definition at line 42 of file node_impl.cpp.

References evaluate().

Referenced by to_string().

44 {
45  std::ostringstream stream{};
46  stream << evaluate();
47  return stream.str();
48 }
double evaluate() const
Definition: node_impl.cpp:24
identifier_list const & node_impl::get_parameters ( ) const

Definition at line 36 of file node_impl.cpp.

References evaluate_parameters().

38 {
39  return evaluate_parameters();
40 }
virtual identifier_list const & evaluate_parameters() const
Definition: node_impl.cpp:50
node_impl& node_impl::operator= ( node_impl const &  n)
private

not implemented

void node_impl::print ( std::ostream &  stream,
int  indent 
) const

Definition at line 18 of file node_impl.cpp.

References print_node().

20 {
21  print_node(stream, indent);
22 }
virtual void print_node(std::ostream &stream, int indent) const =0
virtual void node_impl::print_node ( std::ostream &  stream,
int  indent 
) const
privatepure virtual
std::shared_ptr< node_impl > node_impl::read_node ( std::istream &  stream)
static

Read the node type from a library file, then create the proper kind of node.

Definition at line 64 of file node_impl.cpp.

Referenced by node::node().

65 {
66  std::string type{};
67  if (not (stream >> type))
68  return nullptr;
69  if (type == "void")
70  return std::make_shared<node_void>(stream);
71  if (type == "number")
72  return std::make_shared<node_number>(stream);
73  if (type == "identifier")
74  return std::make_shared<node_identifier>(stream);
75  if (type == "function")
76  return std::make_shared<node_function>(stream);
77  if (type == "call")
78  return std::make_shared<node_function_call>(stream);
79  if (type == "negate")
80  return std::make_shared<node_negate>(stream);
81  if (type == "add")
82  return std::make_shared<node_add>(stream);
83  if (type == "subtract")
84  return std::make_shared<node_subtract>(stream);
85  if (type == "multiply")
86  return std::make_shared<node_multiply>(stream);
87  if (type == "divide")
88  return std::make_shared<node_divide>(stream);
89 
90  throw calc_error{"unknown node type: " + type};
91 }
void node_impl::save ( std::ostream &  stream) const

Definition at line 57 of file node_impl.cpp.

References save_node().

59 {
60  save_node(stream);
61 }
virtual void save_node(std::ostream &stream) const =0
virtual void node_impl::save_node ( std::ostream &  stream) const
privatepure virtual
std::string node_impl::to_string ( ) const

Definition at line 30 of file node_impl.cpp.

References evaluate_string().

32 {
33  return evaluate_string();
34 }
virtual std::string evaluate_string() const
Definition: node_impl.cpp:42

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