diff options
Diffstat (limited to 'ue3/mycpu/cprogram.cpp')
| -rw-r--r-- | ue3/mycpu/cprogram.cpp | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/ue3/mycpu/cprogram.cpp b/ue3/mycpu/cprogram.cpp new file mode 100644 index 0000000..9297b6e --- /dev/null +++ b/ue3/mycpu/cprogram.cpp | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | /** | ||
| 2 | * @module cprogram | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief class for parsing and saving a program | ||
| 5 | * @date 11.05.2009 | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <fstream> | ||
| 9 | #include <vector> | ||
| 10 | #include <string> | ||
| 11 | #include <boost/tokenizer.hpp> | ||
| 12 | #include <boost/algorithm/string.hpp> | ||
| 13 | #include <boost/algorithm/string/split.hpp> | ||
| 14 | #include "cprogram.h" | ||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | using namespace std; | ||
| 19 | using namespace boost; | ||
| 20 | |||
| 21 | CProgram::CProgram(const std::string& progfile) : | ||
| 22 | m_programfile(progfile) | ||
| 23 | { | ||
| 24 | parse(); | ||
| 25 | dump(std::cout); | ||
| 26 | |||
| 27 | } | ||
| 28 | |||
| 29 | /*----------------------------------------------------------------------------*/ | ||
| 30 | |||
| 31 | CProgram::~CProgram() | ||
| 32 | { | ||
| 33 | |||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | |||
| 38 | /*----------------------------------------------------------------------------*/ | ||
| 39 | |||
| 40 | void CProgram::parse() | ||
| 41 | { | ||
| 42 | /* open and read file */ | ||
| 43 | ifstream file(m_programfile.c_str(), ios::in); | ||
| 44 | // if (!file) | ||
| 45 | // throw ParserError("Unable to open scriptfile '" + m_scriptfile + "'."); | ||
| 46 | |||
| 47 | int cur_line_nr = 0; | ||
| 48 | |||
| 49 | while (!file.eof() && file.good()) | ||
| 50 | { | ||
| 51 | string cur_line; | ||
| 52 | |||
| 53 | /* read file per line */ | ||
| 54 | getline(file, cur_line); | ||
| 55 | if (cur_line.empty()) | ||
| 56 | continue; | ||
| 57 | |||
| 58 | trim(cur_line); | ||
| 59 | |||
| 60 | /* ignore comments */ | ||
| 61 | if (cur_line.find_first_of('#') == 0) | ||
| 62 | continue; | ||
| 63 | |||
| 64 | /*remove commas from current line */ | ||
| 65 | unsigned int pos; | ||
| 66 | while (( pos = cur_line.find_first_of(",") ) != string::npos) | ||
| 67 | cur_line.erase(pos, 1); | ||
| 68 | |||
| 69 | /* add source line*/ | ||
| 70 | m_progsource.push_back(vector<string>()); | ||
| 71 | algorithm::split( m_progsource.back(), cur_line, is_any_of(" \t")); | ||
| 72 | |||
| 73 | /* jump addr as line number */ | ||
| 74 | if(m_progsource[cur_line_nr][0] == "label") | ||
| 75 | { | ||
| 76 | int size = m_progsource[cur_line_nr][01].size() - 1; | ||
| 77 | string label(m_progsource[cur_line_nr][01].substr(0, size)); | ||
| 78 | m_jumpaddr[label] = cur_line_nr; | ||
| 79 | } | ||
| 80 | |||
| 81 | cur_line_nr++; | ||
| 82 | } | ||
| 83 | file.close(); | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | /*----------------------------------------------------------------------------*/ | ||
| 88 | |||
| 89 | #ifdef DEBUG | ||
| 90 | void CProgram::dump(std::ostream& out) | ||
| 91 | { | ||
| 92 | |||
| 93 | out << endl << "Program file:" << endl << m_programfile << endl; | ||
| 94 | |||
| 95 | out << endl << "Program source:" << endl; | ||
| 96 | for (int i = 0; i < (int) m_progsource.size();i++) | ||
| 97 | { | ||
| 98 | for(int x = 0; x < (int) m_progsource[i].size();x++) | ||
| 99 | out << m_progsource[i][x] << " "; | ||
| 100 | out << std::endl; | ||
| 101 | } | ||
| 102 | |||
| 103 | out << endl << "Jump addresses:" << endl; | ||
| 104 | map<string, unsigned int>::iterator it; | ||
| 105 | for (it = m_jumpaddr.begin(); it != m_jumpaddr.end(); it++) | ||
| 106 | out << (*it).first << " " << (*it).second << endl; | ||
| 107 | |||
| 108 | } | ||
| 109 | #endif | ||
