preprocessor: streamline addSymbol

issue#70
Houtan Bastani 2013-12-10 11:39:41 +01:00
parent 0d1fe6899f
commit 973f20dab5
3 changed files with 21 additions and 42 deletions

View File

@ -139,9 +139,9 @@ ParsingDriver::declare_symbol(const string *name, SymbolType type, const string
mod_file->symbol_table.addSymbol(*name, type); mod_file->symbol_table.addSymbol(*name, type);
else else
if (tex_name == NULL) if (tex_name == NULL)
mod_file->symbol_table.createTexNameAndAddSymbolWithLongName(*name, type, *long_name); mod_file->symbol_table.addSymbol(*name, type, "", *long_name);
else if (long_name == NULL) else if (long_name == NULL)
mod_file->symbol_table.addSymbol(*name, type, *tex_name); mod_file->symbol_table.addSymbol(*name, type, *tex_name, "");
else else
mod_file->symbol_table.addSymbol(*name, type, *tex_name, *long_name); mod_file->symbol_table.addSymbol(*name, type, *tex_name, *long_name);
} }

View File

@ -52,49 +52,37 @@ SymbolTable::addSymbol(const string &name, SymbolType type, const string &tex_na
throw AlreadyDeclaredException(name, false); throw AlreadyDeclaredException(name, false);
} }
string final_tex_name = tex_name;
if (final_tex_name.empty())
{
final_tex_name = name;
size_t pos = 0;
while ((pos = final_tex_name.find('_', pos)) != string::npos)
{
final_tex_name.insert(pos, "\\");
pos += 2;
}
}
string final_long_name = long_name;
if (final_long_name.empty())
final_long_name = name;
int id = size++; int id = size++;
symbol_table[name] = id; symbol_table[name] = id;
type_table.push_back(type); type_table.push_back(type);
name_table.push_back(name); name_table.push_back(name);
tex_name_table.push_back(tex_name); tex_name_table.push_back(final_tex_name);
long_name_table.push_back(long_name); long_name_table.push_back(final_long_name);
return id; return id;
} }
int
SymbolTable::addSymbol(const string &name, SymbolType type, const string &tex_name) throw (AlreadyDeclaredException, FrozenException)
{
return addSymbol(name, type, tex_name, name);
}
int int
SymbolTable::addSymbol(const string &name, SymbolType type) throw (AlreadyDeclaredException, FrozenException) SymbolTable::addSymbol(const string &name, SymbolType type) throw (AlreadyDeclaredException, FrozenException)
{ {
string tex_name; return addSymbol(name, type, "", "");
return addSymbol(name, type, construct_tex_name(name, tex_name));
}
int
SymbolTable::createTexNameAndAddSymbolWithLongName(const string &name, SymbolType type, const string &long_name) throw (AlreadyDeclaredException, FrozenException)
{
string tex_name;
return addSymbol(name, type, construct_tex_name(name, tex_name), long_name);
}
string &
SymbolTable::construct_tex_name(const string &name, string &tex_name)
{
// Construct "tex_name" by prepending an antislash to all underscores in "name"
tex_name = name;
size_t pos = 0;
while ((pos = tex_name.find('_', pos)) != string::npos)
{
tex_name.insert(pos, "\\");
pos += 2;
}
return tex_name;
} }
void void

View File

@ -185,18 +185,9 @@ public:
//! Add a symbol //! Add a symbol
/*! Returns the symbol ID */ /*! Returns the symbol ID */
int addSymbol(const string &name, SymbolType type, const string &tex_name, const string &long_name) throw (AlreadyDeclaredException, FrozenException); int addSymbol(const string &name, SymbolType type, const string &tex_name, const string &long_name) throw (AlreadyDeclaredException, FrozenException);
//! Add a symbol without its long name (will be equal to its name)
/*! Returns the symbol ID */
int addSymbol(const string &name, SymbolType type, const string &tex_name) throw (AlreadyDeclaredException, FrozenException);
//! Add a symbol without its TeX name (will be equal to its name) //! Add a symbol without its TeX name (will be equal to its name)
/*! Returns the symbol ID */ /*! Returns the symbol ID */
int addSymbol(const string &name, SymbolType type) throw (AlreadyDeclaredException, FrozenException); int addSymbol(const string &name, SymbolType type) throw (AlreadyDeclaredException, FrozenException);
//! Tmp addSymbol function that creates a tex_name and declares a symbol
//! when the name and long_name are passed
/*! Returns the symbol ID */
int createTexNameAndAddSymbolWithLongName(const string &name, SymbolType type, const string &long_name) throw (AlreadyDeclaredException, FrozenException);
//! Create a tex_name based on the variable name
string &construct_tex_name(const string &name, string &tex_name);
//! Adds an auxiliary variable for endogenous with lead >= 2 //! Adds an auxiliary variable for endogenous with lead >= 2
/*! /*!
\param[in] index Used to construct the variable name \param[in] index Used to construct the variable name