Calculator  Step 5
Functions
variables.cpp File Reference
#include <fstream>
#include <istream>
#include <limits>
#include <map>
#include <ostream>
#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)
 
void save_library (std::string const &filename)
 
void load_library (std::string const &filename)
 

Function Documentation

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

Definition at line 36 of file variables.cpp.

Referenced by get_function(), and get_variable().

37 {
38  for (auto iter(symbol_tables.rbegin()), end(symbol_tables.rend()); iter != end; ++iter) {
39  symbol_table const& table{ **iter };
40  symbol_table::const_iterator entry{ table.find(name) };
41  if (entry != table.end()) {
42  value = entry->second;
43  return true;
44  }
45  }
46  return false;
47 }
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
nameThe function name
Returns
The definition of function named name
Exceptions
no_such_functionif the function is not defined

Definition at line 65 of file variables.cpp.

References find_symbol().

Referenced by node_function_call::evaluate_node().

66 {
67  node result{};
68  if (not find_symbol(name, result))
69  throw no_such_function{name};
70  else
71  return result;
72 }
bool find_symbol(std::string const &name, node &value)
Definition: variables.cpp:36
Definition: node.hpp:26
node get_variable ( std::string const &  name)

Get the value of a variable.

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

Definition at line 49 of file variables.cpp.

References find_symbol().

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

50 {
51  node result{};
52  if (not find_symbol(name, result))
53  return node{};
54  else if (result.get_parameters().empty())
55  return result;
56  else
57  throw function_error{name, result.get_parameters().size(), 0};
58 }
bool find_symbol(std::string const &name, node &value)
Definition: variables.cpp:36
Definition: node.hpp:26
void load_library ( std::string const &  filename)

Load variable and function definitions from a file. Definitions in the file overwrite existing definitions with the same name.

Parameters
filenameThe file name
Exceptions
calc_errorfor I/O errors

Definition at line 100 of file variables.cpp.

References set_variable().

Referenced by parser::get_statement().

101 {
102  std::ifstream stream{filename.c_str()};
103  if (not stream)
104  throw file_error{filename};
105  std::string token{};
106  if (not std::getline(stream, token))
107  throw calc_error{filename + ": is empty"};
108  if (token != ":library:")
109  throw calc_error{filename + ": is not a calculator library file"};
110  while (stream >> token) {
111  if (token == "*")
112  return;
113  else
114  set_variable(token, node{stream});
115  }
116 }
Definition: node.hpp:26
void set_variable(std::string const &name, node value)
Definition: variables.cpp:60
void save_library ( std::string const &  filename)

Save all variable and function definitions to a file. The file is a simple text file, so you can examine it in any text editor.

Parameters
filenameThe name of the file.
Exceptions
calc_errorif the file cannot be created

Definition at line 79 of file variables.cpp.

Referenced by parser::get_statement().

80 {
81  std::ofstream stream{filename.c_str()};
82  if (not stream)
83  throw file_error{filename};
84  // Be sure to save numbers with maximum precision.
85  stream.precision(std::numeric_limits<double>::digits10);
86  stream << ":library:\n";
87  for (auto const& symbol : variables)
88  {
89  stream << symbol.first << ' ';
90  symbol.second.save(stream);
91  if (stream.fail())
92  throw file_error{filename};
93  }
94  stream << "*\n";
95  stream.close();
96  if (stream.fail())
97  throw file_error{filename};
98 }
void set_function ( std::string const &  name,
node  definition 
)

Set the definition of a function.

Parameters
nameThe function name
definitionThe function definition

Definition at line 74 of file variables.cpp.

References set_variable().

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

75 {
76  set_variable(name, value);
77 }
void set_variable(std::string const &name, node value)
Definition: variables.cpp:60
void set_variable ( std::string const &  name,
node  value 
)

Set the value of a variable.

Parameters
nameThe variable name
valueThe value. if the variable is already defined, changes its value.

Definition at line 60 of file variables.cpp.

Referenced by BOOST_AUTO_TEST_CASE(), load_library(), and set_function().

61 {
62  variables[name] = value;
63 }