Calculator  Step 6
Public Member Functions | Private Member Functions | Private Attributes | List of all members
node_function Class Reference

#include <node_impl.hpp>

Inheritance diagram for node_function:
node_impl

Public Member Functions

 node_function (identifier_list parameters, node definition)
 
 node_function (std::istream &stream)
 
identifier_list const & parameters () const
 
node definition () const
 
- Public Member Functions inherited from node_impl
 node_impl ()
 
virtual ~node_impl ()
 
 node_impl (node_impl &&)=default
 
node_imploperator= (node_impl &&)=default
 
void print (std::ostream &stream, int indent) const
 
number evaluate () const
 
std::string to_string () const
 
identifier_list const & get_parameters () const
 
void save (std::ostream &stream) const
 

Private Member Functions

virtual void print_node (std::ostream &stream, int indent) const override
 
virtual number evaluate_node () const override
 
virtual identifier_list const & evaluate_parameters () const override
 
virtual void save_node (std::ostream &stream) const override
 

Private Attributes

identifier_list parameters_
 
node definition_
 

Additional Inherited Members

- Static Public Member Functions inherited from node_impl
static std::shared_ptr< node_implread_node (std::istream &stream)
 

Detailed Description

Function definition. A function has parameters and a definition.

Definition at line 101 of file node_impl.hpp.

Constructor & Destructor Documentation

node_function::node_function ( identifier_list  parameters,
node  definition 
)

Definition at line 202 of file node_impl.cpp.

203 : node_impl{}, parameters_{std::move(parameters)}, definition_{definition}
204 {}
identifier_list parameters_
Definition: node_impl.hpp:113
identifier_list const & parameters() const
Definition: node_impl.cpp:222
node_function::node_function ( std::istream &  stream)

Definition at line 206 of file node_impl.cpp.

207 : node_impl{}
208 {
209  std::size_t size;{}
210  if (not (stream >> size))
211  throw calc_error{"malformed library file, cannot read function"};
212  parameters_.reserve(size);
213  while (size-- != 0) {
214  std::string parameter{};
215  if (not (stream >> parameter))
216  throw calc_error{"malformed library file, cannot read function parameter"};
217  parameters_.emplace_back(std::move(parameter));
218  }
219  definition_ = node{stream};
220 }
Definition: node.hpp:28
identifier_list parameters_
Definition: node_impl.hpp:113

Member Function Documentation

node node_function::definition ( ) const

Definition at line 228 of file node_impl.cpp.

References definition_.

Referenced by evaluate_node(), print_node(), and save_node().

230 {
231  return definition_;
232 }
number node_function::evaluate_node ( ) const
overrideprivatevirtual

Implements node_impl.

Definition at line 254 of file node_impl.cpp.

References definition(), and node::evaluate().

256 {
257  return definition().evaluate();
258 }
node definition() const
Definition: node_impl.cpp:228
number evaluate() const
Definition: node.cpp:73
identifier_list const & node_function::evaluate_parameters ( ) const
overrideprivatevirtual

Reimplemented from node_impl.

Definition at line 260 of file node_impl.cpp.

References parameters().

262 {
263  return parameters();
264 }
identifier_list const & parameters() const
Definition: node_impl.cpp:222
identifier_list const & node_function::parameters ( ) const

Definition at line 222 of file node_impl.cpp.

References parameters_.

Referenced by evaluate_parameters(), print_node(), and save_node().

224 {
225  return parameters_;
226 }
identifier_list parameters_
Definition: node_impl.hpp:113
void node_function::print_node ( std::ostream &  stream,
int  indent 
) const
overrideprivatevirtual

Implements node_impl.

Definition at line 245 of file node_impl.cpp.

References definition(), parameters(), node::print(), and print_identifier_list().

247 {
248  stream << std::setw(indent) << "" << "fun\n";
250  stream << '=';
251  definition().print(stream, indent + 2);
252 }
node definition() const
Definition: node_impl.cpp:228
void print(std::ostream &stream, int indent=0) const
Definition: node.cpp:67
void print_identifier_list(std::ostream &stream, identifier_list const &identifiers)
Definition: node_impl.cpp:234
identifier_list const & parameters() const
Definition: node_impl.cpp:222
void node_function::save_node ( std::ostream &  stream) const
overrideprivatevirtual

Implements node_impl.

Definition at line 266 of file node_impl.cpp.

References definition(), parameters(), and node::save().

268 {
269  stream << "function " << parameters().size() << ' ';
270  for (auto const& parameter : parameters())
271  stream << parameter << ' ';
272  definition().save(stream);
273 }
node definition() const
Definition: node_impl.cpp:228
void save(std::ostream &stream) const
Definition: node.cpp:91
identifier_list const & parameters() const
Definition: node_impl.cpp:222

Member Data Documentation

node node_function::definition_
private

Definition at line 114 of file node_impl.hpp.

Referenced by definition().

identifier_list node_function::parameters_
private

Definition at line 113 of file node_impl.hpp.

Referenced by parameters().


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