Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Курсовая работа2 / TableMgr
.h// Purpose: Tables management
//*****************************************************************************
#ifndef _TABLES_MANAGER_
#define _TABLES_MANAGER_
//-----------------------------------------------------------------------------
#include <string>
#include <vector>
#include <map>
#include "Tokens.h"
#include "Token.h"
#include "Types.h"
//-----------------------------------------------------------------------------
struct RecordBase
{
std::string lexem;
RecordBase(std::string lex): lexem(lex) { }
RecordBase() {}
};
//------------------------------------------
struct RecordID: public RecordBase
{
TYPES type;
bool bConst; // идентификатор обозначает константу?
std::string sInternalName;
RecordID(std::string lex) : type(TYPE_UNDEFINED), bConst(false), RecordBase(lex) { }
RecordID() : type(TYPE_UNDEFINED), bConst(false) { }
};
//------------------------------------------
struct RecordKeyWord: public RecordBase
{
ENUM_TOKENS token;
RecordKeyWord(std::string lex, ENUM_TOKENS tok) : RecordBase(lex), token(tok) { }
RecordKeyWord() {}
};
//------------------------------------------
struct RecordOper: public RecordKeyWord
{
ENUM_TOKEN_VAL tok_value;
RecordOper(std::string lex, ENUM_TOKENS tok, ENUM_TOKEN_VAL val)
: RecordKeyWord(lex, tok), tok_value(val)
{ }
RecordOper() {}
};
//-----------------------------------------------------------------------------
template<class T> struct RecordConst
{
T value;
// may be something else...
RecordConst(T &val): value(val) { }
RecordConst() { }
};
typedef RecordConst<int> RecordInt;
typedef RecordConst<float> RecordFloat;
typedef RecordConst<char> RecordChar;
typedef RecordConst<std::string> RecordString;
//-----------------------------------------------------------------------------
template<class T> struct TableSimple
{
typedef std::vector<T> container_t;
typedef container_t::iterator iterator;
container_t table;
public:
virtual unsigned AddRecord(T rec)
{
table.push_back(rec);
return (table.size() - 1);
}
virtual T & GetRecord(unsigned index)
{ return table[index]; }
virtual unsigned Size()
{ return table.size(); }
virtual void Clear()
{ table.clear(); }
};
//-----------------------------------------------------------------------------
// Functional object compares two strings without regard to case
struct str_less
{
bool operator() (const std::string &left, const std::string &right) const
{
return (_stricmp(left.c_str(), right.c_str()) < 0);
}
};
//-----------------------------------------------------------------------------
template<class T> struct TableMapped: public TableSimple<T>
{
typedef std::map<std::string, unsigned , str_less> my_map;
my_map id_map;
virtual unsigned AddRecord(T rec)
{
unsigned index;
if (!find(rec.lexem, index))
{
index = TableSimple<T>::AddRecord(rec);
id_map[rec.lexem] = index;
}
return index;
}
bool find(const std::string &lexem, unsigned &index)
{
my_map::iterator it = id_map.find(lexem);
if (it == id_map.end()) return false;
index = it->second;
return true;
}
virtual void Clear()
{
TableSimple<T>::Clear();
id_map.clear();
}
};
//-----------------------------------------------------------------------------
class CTableManager
{
TableSimple<RecordInt> m_tInt;
TableSimple<RecordFloat> m_tFloat;
TableSimple<RecordChar> m_tChar;
TableSimple<RecordString> m_tString;
TableMapped<RecordID> m_tID;
TableMapped<RecordKeyWord> m_tKeyWords;
TableMapped<RecordOper> m_tSpecSymbols;
CTokenStream m_TokenStream;
public:
CTableManager();
void AddToken(ENUM_TOKEN_TYPES tt, std::string lexeme, unsigned line);
void Clear();
void FPrintTables(const char * filename);
void FPrintTokens(const char * filename);
CTokenStream::iterator GetTokenBegin() { return m_TokenStream.begin(); }
CTokenStream::iterator GetTokenEnd() { return m_TokenStream.end(); }
// ENUM_TOKENS Token(CToken &tok);
std::string Lexem(CToken &tok);
// Предоставляет доступ к полю типа идентификатора
TYPES & IDType(unsigned index) { return m_tID.GetRecord(index).type; }
// Предоставляет доступ к полю лексемы идентификатора
const char* IDLexem(unsigned index) { return m_tID.GetRecord(index).lexem.c_str(); }
// Предоставляет доступ к флагу константности идентификатора
bool & IDConst(unsigned index) { return m_tID.GetRecord(index).bConst; }
std::string &IDInternalName(unsigned index) { return m_tID.GetRecord(index).sInternalName; }
ENUM_TOKEN_VAL GetOperation(unsigned index)
{ return m_tSpecSymbols.GetRecord(index).tok_value; }
int GetValueInt(unsigned index) { return m_tInt.GetRecord(index).value; }
float GetValueReal(unsigned index) { return m_tFloat.GetRecord(index).value; }
char GetValueChar(unsigned index) { return m_tChar.GetRecord(index).value; }
std::string GetValueStr(unsigned index) { return m_tString.GetRecord(index).value; }
bool GetValueBool(unsigned index) { return (m_tKeyWords.GetRecord(index).token == TKN_TRUE); }
void ErrorOutOfRange(unsigned line);
};
extern CTableManager g_TableMgr; // One global CTableManager object
//-----------------------------------------------------------------------------
#endif
Соседние файлы в папке Курсовая работа2