Calculator  Step 5
Public Member Functions | Private Member Functions | Private Attributes | List of all members
node_function_call Class Reference

#include <node_impl.hpp>

Inheritance diagram for node_function_call:
node_impl

Public Member Functions

 node_function_call (std::string name, node_list arguments)
 
 node_function_call (std::istream &stream)
 
std::string name () const
 
node_list const & arguments () const
 
- Public Member Functions inherited from node_impl
 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
 

Private Member Functions

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

Private Attributes

std::string name_
 
node_list arguments_
 

Additional Inherited Members

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

Detailed Description

Function call.

Definition at line 115 of file node_impl.hpp.

Constructor & Destructor Documentation

node_function_call::node_function_call ( std::string  name,
node_list  arguments 
)

Definition at line 277 of file node_impl.cpp.

278 : node_impl{}, name_{std::move(name)}, arguments_{std::move(arguments)}
279 {}
std::string name() const
Definition: node_impl.cpp:296
node_list const & arguments() const
Definition: node_impl.cpp:302
node_list arguments_
Definition: node_impl.hpp:127
std::string name_
Definition: node_impl.hpp:126
node_function_call::node_function_call ( std::istream &  stream)

Definition at line 281 of file node_impl.cpp.

282 : node_impl{}
283 {
284  std::string name{};
285  if (not (stream >> name_))
286  throw calc_error{"malformed library file, cannot read function call name"};
287  std::size_t size{};
288  if (not (stream >> size))
289  throw calc_error{"malformed library file, cannot read function call"};
290  arguments_.reserve(size);
291  while (size-- != 0) {
292  arguments_.emplace_back(stream);
293  }
294 }
std::string name() const
Definition: node_impl.cpp:296
node_list arguments_
Definition: node_impl.hpp:127
std::string name_
Definition: node_impl.hpp:126

Member Function Documentation

node_list const & node_function_call::arguments ( ) const

Definition at line 302 of file node_impl.cpp.

References arguments_.

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

304 {
305  return arguments_;
306 }
node_list arguments_
Definition: node_impl.hpp:127
double node_function_call::evaluate_node ( ) const
overrideprivatevirtual

Implements node_impl.

Definition at line 321 of file node_impl.cpp.

References arguments(), get_function(), node::get_parameters(), and name().

323 {
324  // Create a local symbol table, assigning all the node values to the parameters.
325  node function{ get_function(name()) };
326  identifier_list const& parameters{ function.get_parameters() };
327  if (parameters.size() != arguments().size())
328  throw function_error{name(), parameters.size(), arguments().size()};
329  else
330  {
331  // Create a local symbol table by assigning the arguments to the function parameters.
332  symbol_table locals{};
333  identifier_list::const_iterator parm{parameters.begin()};
334  for (auto const& arg : arguments()) {
335  locals.emplace(*parm, arg);
336  ++parm;
337  }
338  set_symbol_table syms{locals};
339  return function.evaluate();
340  }
341 }
node get_function(std::string const &name)
Definition: variables.cpp:65
std::string name() const
Definition: node_impl.cpp:296
node_list const & arguments() const
Definition: node_impl.cpp:302
Definition: node.hpp:26
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:19
identifier_list const & get_parameters() const
Definition: node.cpp:72
std::map< std::string, node > symbol_table
Definition: variables.hpp:13
std::string node_function_call::name ( ) const

Definition at line 296 of file node_impl.cpp.

References name_.

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

298 {
299  return name_;
300 }
std::string name_
Definition: node_impl.hpp:126
void node_function_call::print_node ( std::ostream &  stream,
int  indent 
) const
overrideprivatevirtual

Implements node_impl.

Definition at line 308 of file node_impl.cpp.

References arguments(), and name().

310 {
311  stream << std::setw(indent) << "" << name() << "(\n";
312  std::size_t index{0};
313  for (auto const& arg : arguments()) {
314  stream << std::setw(indent+1) << "" << "arg " << index << ": ";
315  arg.print(stream, indent + 2);
316  ++index;
317  }
318  stream << std::setw(indent) << "" << ")\n";
319 }
std::string name() const
Definition: node_impl.cpp:296
node_list const & arguments() const
Definition: node_impl.cpp:302
void node_function_call::save_node ( std::ostream &  stream) const
overrideprivatevirtual

Implements node_impl.

Definition at line 343 of file node_impl.cpp.

References arguments(), and name().

345 {
346  stream << "call " << name() << ' ' << arguments().size() << ' ';
347  for (auto const& arg : arguments())
348  arg.save(stream);
349 }
std::string name() const
Definition: node_impl.cpp:296
node_list const & arguments() const
Definition: node_impl.cpp:302

Member Data Documentation

node_list node_function_call::arguments_
private

Definition at line 127 of file node_impl.hpp.

Referenced by arguments().

std::string node_function_call::name_
private

Definition at line 126 of file node_impl.hpp.

Referenced by name().


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