Calculator Step 4

- Main Page
- Classes
- Files

- File List
- File Members

Functions
variables.cpp File Reference
`#include <map>`
`#include "calc_error.hpp"`
`#include "node.hpp"`
`#include "variables.hpp"`

Go to the source code of this file.

## Functions
bool | find_symbol (std::string const &name, node &value)
node | get_variable (std::string const &name)
void | set_variable (std::string const &name, node value)
node | get_function (std::string const &name)
void | set_function (std::string const &name, node value)

## Function Documentation

bool find_symbol | ( | std::string const & | name,
|  | node & | value
| ) |  |

Definition at line 32 of file variables.cpp.

Referenced by get_function(), and get_variable().

33 {
34 for (auto iter(symbol_tables.rbegin()), end(symbol_tables.rend()); iter != end; ++iter) {
35 symbol_table const& table{ **iter };
36 symbol_table::const_iterator entry{ table.find(name) };
37 if (entry != table.end()) {
38 value = entry->second;
39 return true;
40 }
41 }
42 return false;
43 }
symbol_table
std::map< std::string, node > symbol_table
Definition: variables.hpp:13

node get_function | ( | std::string const & | name | ) |

Get the definition of a function.

Parameters

  name | The function name

Returns
  The definition of function named `name`

Exceptions

  no_such_function | if the function is not defined

Definition at line 61 of file variables.cpp.

References find_symbol().

Referenced by node_function_call::evaluate_node().

62 {
63 node result{};
64 if (not find_symbol(name, result))
65 throw no_such_function{name};
66 else
67 return result;
68 }
find_symbol
bool find_symbol(std::string const &name, node &value)
Definition: variables.cpp:32
node
Definition: node.hpp:26
no_such_function
Definition: calc_error.hpp:45

node get_variable | ( | std::string const & | name | ) |

Get the value of a variable.

Parameters

  name | The variable name

Returns
  The value of variable `name` or node() if the variable is undefined.

Definition at line 45 of file variables.cpp.

References find_symbol().

Referenced by BOOST_AUTO_TEST_CASE(), and node_identifier::evaluate_node().

46 {
47 node result{};
48 if (not find_symbol(name, result))
49 return node();
50 else if (result.get_parameters().empty())
51 return result;
52 else
53 throw function_error{name, result.get_parameters().size(), 0};
54 }
find_symbol
bool find_symbol(std::string const &name, node &value)
Definition: variables.cpp:32
node
Definition: node.hpp:26
function_error
Definition: calc_error.hpp:34

void set_function | ( | std::string const & | name,
|  | node | definition
| ) |  |

Set the definition of a function.

Parameters

  name | The function name
  definition | The function definition

Definition at line 70 of file variables.cpp.

References set_variable().

Referenced by BOOST_AUTO_TEST_CASE(), and parser::get_statement().

71 {
72 set_variable(name, value);
73 }
set_variable
void set_variable(std::string const &name, node value)
Definition: variables.cpp:56

void set_variable | ( | std::string const & | name,
|  | node | value
| ) |  |

Set the value of a variable.

Parameters

  name | The variable name
  value | The value. if the variable is already defined, changes its value.

Definition at line 56 of file variables.cpp.

Referenced by BOOST_AUTO_TEST_CASE(), and set_function().

57 {
58 variables[name] = value;
59 }