Calculator Step 6

- 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 ()
| node_impl (node_impl &&)=default
node_impl & | operator= (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 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 118 of file node_impl.hpp.

## Constructor & Destructor Documentation

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

Definition at line 276 of file node_impl.cpp.

277 : node_impl{}, name_{std::move(name)}, arguments_{std::move(arguments)}
278 {}
node_function_call::name
std::string name() const
Definition: node_impl.cpp:295
node_function_call::arguments
node_list const & arguments() const
Definition: node_impl.cpp:301
node_impl
Definition: node_impl.hpp:20
node_function_call::arguments_
node_list arguments_
Definition: node_impl.hpp:130
node_function_call::name_
std::string name_
Definition: node_impl.hpp:129

node_function_call::node_function_call | ( | std::istream & | stream | ) |

Definition at line 280 of file node_impl.cpp.

281 : node_impl{}
282 {
283 std::string name{};
284 if (not (stream >> name_))
285 throw calc_error{"malformed library file, cannot read function call name"};
286 std::size_t size{};
287 if (not (stream >> size))
288 throw calc_error{"malformed library file, cannot read function call"};
289 arguments_.reserve(size);
290 while (size-- != 0) {
291 arguments_.emplace_back(stream);
292 }
293 }
node_function_call::name
std::string name() const
Definition: node_impl.cpp:295
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:130
node_function_call::name_
std::string name_
Definition: node_impl.hpp:129

## Member Function Documentation

node_list const & node_function_call::arguments | ( |  | ) | const

Definition at line 301 of file node_impl.cpp.

References arguments_.

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

303 {
304 return arguments_;
305 }
node_function_call::arguments_
node_list arguments_
Definition: node_impl.hpp:130

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

Implements node_impl.

Definition at line 320 of file node_impl.cpp.

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

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

std::string node_function_call::name | ( |  | ) | const

Definition at line 295 of file node_impl.cpp.

References name_.

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

297 {
298 return name_;
299 }
node_function_call::name_
std::string name_
Definition: node_impl.hpp:129

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

Implements node_impl.

Definition at line 307 of file node_impl.cpp.

References arguments(), and name().

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

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

Implements node_impl.

Definition at line 342 of file node_impl.cpp.

References arguments(), and name().

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

## Member Data Documentation

node_list node_function_call::arguments_
| private

Definition at line 130 of file node_impl.hpp.

Referenced by arguments().

std::string node_function_call::name_
| private

Definition at line 129 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