#include <map>
#include <string>
#include "node.hpp"
Go to the source code of this file.
Classes | |
class | set_symbol_table |
Typedefs | |
typedef std::map < std::string, node > | symbol_table |
Functions | |
node | get_variable (std::string const &name) |
node | get_function (std::string const &name) |
void | set_variable (std::string const &name, node value) |
void | set_function (std::string const &name, node definition) |
void | save_library (std::string const &filename) |
void | load_library (std::string const &filename) |
typedef std::map<std::string, node> symbol_table |
Represent a global or local symbol table, which stores variables and functions. In fact, there's no difference between a variable and a function of no arguments.
Definition at line 13 of file variables.hpp.
node get_function | ( | std::string const & | name | ) |
Get the definition of a function.
name | The function name |
name
no_such_function | if the function is not defined |
Definition at line 65 of file variables.cpp.
References find_symbol().
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.
name | The variable name |
name
or node() if the variable is undefined. Definition at line 49 of file variables.cpp.
References find_symbol(), and node::get_parameters().
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.
filename | The file name |
calc_error | for I/O errors |
Definition at line 99 of file variables.cpp.
References set_variable().
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.
filename | The name of the file. |
calc_error | if the file cannot be created |
Definition at line 79 of file variables.cpp.
References variables.
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.
name | The function name | |
definition | The function definition |
Definition at line 74 of file variables.cpp.
References set_variable().
00075 { 00076 set_variable(name, value); 00077 }
void set_variable | ( | std::string const & | name, | |
node | value | |||
) |
Set the value of a variable.
name | The variable name | |
value | The value. if the variable is already defined, changes its value. |
Definition at line 60 of file variables.cpp.
References variables.
00061 { 00062 variables[name] = value; 00063 }