diff options
Diffstat (limited to 'ue3/mycpu/cprogram.cpp')
| -rw-r--r-- | ue3/mycpu/cprogram.cpp | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/ue3/mycpu/cprogram.cpp b/ue3/mycpu/cprogram.cpp new file mode 100644 index 0000000..03d4c30 --- /dev/null +++ b/ue3/mycpu/cprogram.cpp | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | /** | ||
| 2 | * @module cprogram | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief TODO | ||
| 5 | * @date 12.05.2009 | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <boost/algorithm/string.hpp> | ||
| 9 | #include <boost/algorithm/string/split.hpp> | ||
| 10 | #ifdef DEBUG | ||
| 11 | # include <iostream> | ||
| 12 | # include <iomanip> | ||
| 13 | #endif | ||
| 14 | #include "cprogram.h" | ||
| 15 | #include "instructions.h" | ||
| 16 | |||
| 17 | using namespace std; | ||
| 18 | |||
| 19 | CProgram::CProgram() | ||
| 20 | { | ||
| 21 | m_instrset.insert(new CInstructionInc); | ||
| 22 | } | ||
| 23 | |||
| 24 | /*----------------------------------------------------------------------------*/ | ||
| 25 | |||
| 26 | CProgram::~CProgram() | ||
| 27 | { | ||
| 28 | /* free instruction set */ | ||
| 29 | set<CInstruction *>::iterator it; | ||
| 30 | for (it = m_instrset.begin(); it != m_instrset.end(); ++it) | ||
| 31 | delete *it; | ||
| 32 | |||
| 33 | /* free instruction */ | ||
| 34 | for (iterator it = begin(); it != end(); ++it) | ||
| 35 | delete *it; | ||
| 36 | } | ||
| 37 | |||
| 38 | /*----------------------------------------------------------------------------*/ | ||
| 39 | |||
| 40 | void CProgram::compile(std::istream& in) | ||
| 41 | { | ||
| 42 | if (!in.good()) | ||
| 43 | return; | ||
| 44 | |||
| 45 | string line; | ||
| 46 | unsigned i = 0; | ||
| 47 | while (!in.eof() && in.good()) | ||
| 48 | { | ||
| 49 | ++i; | ||
| 50 | |||
| 51 | /* read stream per line */ | ||
| 52 | getline(in, line); | ||
| 53 | if (line.empty()) | ||
| 54 | continue; | ||
| 55 | |||
| 56 | boost::trim(line); | ||
| 57 | |||
| 58 | /* ignore comments */ | ||
| 59 | if (line.find_first_of('#') == 0) | ||
| 60 | continue; | ||
| 61 | |||
| 62 | /* get instruction name */ | ||
| 63 | size_t pos = line.find_first_of(' '); | ||
| 64 | string instrname = line.substr(0, pos); | ||
| 65 | |||
| 66 | /* search and create instruction */ | ||
| 67 | CInstruction *instrptr = NULL; | ||
| 68 | set<CInstruction *>::iterator it; | ||
| 69 | for (it = m_instrset.begin(); it != m_instrset.end(); ++it) | ||
| 70 | { | ||
| 71 | if (*(*it) == instrname) | ||
| 72 | { | ||
| 73 | instrptr = *it; | ||
| 74 | break; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | if (instrptr == NULL) | ||
| 78 | { | ||
| 79 | stringstream sstr; | ||
| 80 | sstr << "Unknown instruction '" << instrname << "' on line " << i << "."; | ||
| 81 | throw runtime_error(sstr.str()); | ||
| 82 | } | ||
| 83 | |||
| 84 | /* create instruction */ | ||
| 85 | CInstruction *instr = instrptr->factory(); | ||
| 86 | |||
| 87 | /* parse instruction parameters */ | ||
| 88 | string params = (pos == string::npos) ? "" : line.substr(pos + 1); | ||
| 89 | boost::trim(params); | ||
| 90 | list<string> instrparams; | ||
| 91 | boost::split(instrparams, params, boost::is_any_of(", "), boost::token_compress_on); | ||
| 92 | |||
| 93 | /* let instruction parse the parameters. catch+throw exception */ | ||
| 94 | try | ||
| 95 | { | ||
| 96 | instr->compile(instrparams); | ||
| 97 | } | ||
| 98 | catch(runtime_error& ex) | ||
| 99 | { | ||
| 100 | stringstream sstr; | ||
| 101 | sstr << "Unable to compile instruction '" << instrname | ||
| 102 | << "' (line " << i << "): " << ex.what(); | ||
| 103 | throw runtime_error(sstr.str()); | ||
| 104 | } | ||
| 105 | |||
| 106 | push_back(instr); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | /*----------------------------------------------------------------------------*/ | ||
| 111 | |||
| 112 | #if DEBUG | ||
| 113 | void CProgram::dump(std::ostream& out) | ||
| 114 | { | ||
| 115 | out << "[PROGRAM DUMP]" << endl; | ||
| 116 | unsigned i = 0; | ||
| 117 | for(iterator it = begin(); it < end(); ++it) | ||
| 118 | { | ||
| 119 | out << "[" << std::setw(4) << std::setfill('0') << i << "] " | ||
| 120 | << *(*it) << endl; | ||
| 121 | ++i; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | #endif | ||
| 125 | |||
| 126 | /* vim: set et sw=2 ts=2: */ | ||
