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)

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

References symbol_tables.

Referenced by get_function(), and get_variable().

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

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.

Referenced by node_function_call::evaluate_node().

00062 {
00063   node result;
00064   if (not find_symbol(name, result))
00065     throw no_such_function(name);
00066   else
00067     return result;
00068 }

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.

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

00046 {
00047   node result;
00048   if (not find_symbol(name, result))
00049     return node();
00050   else if (result.get_parameters().empty())
00051     return result;
00052   else
00053     throw function_error(name, result.get_parameters().size(), 0);
00054 }

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.

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

00071 {
00072   set_variable(name, value);
00073 }

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().

00057 {
00058   variables[name] = value;
00059 }


Variable Documentation

initializer init [static]

Definition at line 19 of file variables.cpp.

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

Definition at line 9 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 8 of file variables.cpp.

Referenced by set_variable().


Generated on Sun Nov 30 10:05:27 2008 for Calculator by  doxygen 1.5.3