/** * @module cprogram * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) * @brief class for parsing and saving a program * @date 11.05.2009 */ #include #include #include #include #include #include #include "cprogram.h" using namespace std; using namespace boost; CProgram::CProgram(const std::string& progfile) : m_programfile(progfile) { parse(); dump(std::cout); } /*----------------------------------------------------------------------------*/ CProgram::~CProgram() { } /*----------------------------------------------------------------------------*/ void CProgram::parse() { /* open and read file */ ifstream file(m_programfile.c_str(), ios::in); // if (!file) // throw ParserError("Unable to open scriptfile '" + m_scriptfile + "'."); int cur_line_nr = 0; while (!file.eof() && file.good()) { string cur_line; /* read file per line */ getline(file, cur_line); if (cur_line.empty()) continue; trim(cur_line); /* ignore comments */ if (cur_line.find_first_of('#') == 0) continue; /*remove commas from current line */ unsigned int pos; while (( pos = cur_line.find_first_of(",") ) != string::npos) cur_line.erase(pos, 1); /* add source line*/ m_progsource.push_back(vector()); algorithm::split( m_progsource.back(), cur_line, is_any_of(" \t")); /* jump addr as line number */ if(m_progsource[cur_line_nr][0] == "label") { int size = m_progsource[cur_line_nr][01].size() - 1; string label(m_progsource[cur_line_nr][01].substr(0, size)); m_jumpaddr[label] = cur_line_nr; } cur_line_nr++; } file.close(); } /*----------------------------------------------------------------------------*/ #ifdef DEBUG void CProgram::dump(std::ostream& out) { out << endl << "Program file:" << endl << m_programfile << endl; out << endl << "Program source:" << endl; for (int i = 0; i < (int) m_progsource.size();i++) { for(int x = 0; x < (int) m_progsource[i].size();x++) out << m_progsource[i][x] << " "; out << std::endl; } out << endl << "Jump addresses:" << endl; map::iterator it; for (it = m_jumpaddr.begin(); it != m_jumpaddr.end(); it++) out << (*it).first << " " << (*it).second << endl; } #endif