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)

Variables

symbol_table variables
std::vector
< symbol_table const * > 
symbol_tables
initializer init


Function Documentation

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

Definition at line 36 of file variables.cpp.

References symbol_tables.

Referenced by get_function(), and get_variable().

00037 {
00038   for (std::vector<symbol_table const*>::reverse_iterator iter(symbol_tables.rbegin()); iter != symbol_tables.rend(); ++iter) {
00039     symbol_table const& table( **iter );
00040     symbol_table::const_iterator entry = table.find(name);
00041     if (entry != table.end()) {
00042       value = entry->second;
00043       return true;
00044     }
00045   }
00046   return false;
00047 }

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 65 of file variables.cpp.

Referenced by node_function_call::evaluate_node().

00066 {
00067   node result;
00068   if (not find_symbol(name, result))
00069     throw no_such_function(name);
00070   else
00071     return result;
00072 }

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 49 of file variables.cpp.

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

00050 {
00051   node result;
00052   if (not find_symbol(name, result))
00053     return node();
00054   else if (result.get_parameters().empty())
00055     return result;
00056   else
00057     throw function_error(name, result.get_parameters().size(), 0);
00058 }

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:
filename The file name
Exceptions:
calc_error for I/O errors

Definition at line 99 of file variables.cpp.

Referenced by parser::get_statement().

00100 {
00101   std::ifstream stream(filename.c_str());
00102   if (not stream)
00103     throw file_error(filename);
00104   std::string token;
00105   if (not std::getline(stream, token))
00106     throw calc_error(filename + ": is empty");
00107   if (token != ":library:")
00108     throw calc_error(filename + ": is not a calculator library file");
00109   while (stream >> token) {
00110     if (token == "*")
00111       return;
00112     else
00113       set_variable(token, node(stream));
00114   }
00115 }

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:
filename The name of the file.
Exceptions:
calc_error if the file cannot be created

Definition at line 79 of file variables.cpp.

Referenced by parser::get_statement().

00080 {
00081   std::ofstream stream(filename.c_str());
00082   if (not stream)
00083     throw file_error(filename);
00084   // Be sure to save numbers with maximum precision.
00085   stream.precision(std::numeric_limits<double>::digits10);
00086   stream << ":library:\n";
00087   for (symbol_table::iterator symbol(variables.begin()); symbol != variables.end(); ++symbol) {
00088     stream << symbol->first << ' ';
00089     symbol->second.save(stream);
00090     if (stream.fail())
00091       throw file_error(filename);
00092   }
00093   stream << "*\n";
00094   stream.close();
00095   if (stream.fail())
00096     throw file_error(filename);
00097 }

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 74 of file variables.cpp.

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

00075 {
00076   set_variable(name, value);
00077 }

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 60 of file variables.cpp.

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

00061 {
00062   variables[name] = value;
00063 }


Variable Documentation

initializer init [static]

Definition at line 23 of file variables.cpp.

std::vector<symbol_table const*> symbol_tables [static]

Definition at line 13 of file variables.cpp.

Referenced by find_symbol(), set_symbol_table::set_symbol_table(), and set_symbol_table::~set_symbol_table().

symbol_table variables [static]

Definition at line 12 of file variables.cpp.

Referenced by save_library(), and set_variable().


Generated on Sun Nov 30 10:06:52 2008 for Calculator by  doxygen 1.5.3