Calculator Step 5

- Main Page
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

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:

## 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_impl > | read_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 {}
node_function_call::name
std::string name() const
Definition: node_impl.cpp:296
node_function_call::arguments
node_list const & arguments() const
Definition: node_impl.cpp:302
node_impl
Definition: node_impl.hpp:20
node_function_call::arguments_
node_list arguments_
Definition: node_impl.hpp:127
node_function_call::name_
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 }
node_function_call::name
std::string name() const
Definition: node_impl.cpp:296
calc_error
Definition: calc_error.hpp:11
node_impl
Definition: node_impl.hpp:20
node_function_call::arguments_
node_list arguments_
Definition: node_impl.hpp:127
node_function_call::name_
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_function_call::arguments_
node_list arguments_
Definition: node_impl.hpp:127

double node_function_call::evaluate_node | ( |  | ) | const
| override private virtual

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 }
get_function
node get_function(std::string const &name)
Definition: variables.cpp:65
node_function_call::name
std::string name() const
Definition: node_impl.cpp:296
node_function_call::arguments
node_list const & arguments() const
Definition: node_impl.cpp:302
node
Definition: node.hpp:26
identifier_list
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:19
set_symbol_table
Definition: variables.hpp:19
function_error
Definition: calc_error.hpp:31
node::get_parameters
identifier_list const & get_parameters() const
Definition: node.cpp:72
symbol_table
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 }
node_function_call::name_
std::string name_
Definition: node_impl.hpp:126

void node_function_call::print_node | ( | std::ostream & | stream,
|  | int | indent
| ) |  | const
| override private virtual

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 }
node_function_call::name
std::string name() const
Definition: node_impl.cpp:296
node_function_call::arguments
node_list const & arguments() const
Definition: node_impl.cpp:302

void node_function_call::save_node | ( | std::ostream & | stream | ) | const
| override private virtual

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 }
node_function_call::name
std::string name() const
Definition: node_impl.cpp:296
node_function_call::arguments
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:

- node_impl.hpp
- node_impl.cpp