summaryrefslogtreecommitdiffstats
path: root/ue4/mycpu/cprogram.h
diff options
context:
space:
mode:
Diffstat (limited to 'ue4/mycpu/cprogram.h')
-rw-r--r--ue4/mycpu/cprogram.h76
1 files changed, 54 insertions, 22 deletions
diff --git a/ue4/mycpu/cprogram.h b/ue4/mycpu/cprogram.h
index d8f7d5e..6908db7 100644
--- a/ue4/mycpu/cprogram.h
+++ b/ue4/mycpu/cprogram.h
@@ -11,17 +11,43 @@
11#include <vector> 11#include <vector>
12#include <set> 12#include <set>
13#include <map> 13#include <map>
14#include <stdexcept>
14#include <boost/algorithm/string.hpp> 15#include <boost/algorithm/string.hpp>
15#include <boost/algorithm/string/split.hpp> 16#include <boost/algorithm/string/split.hpp>
16#ifdef DEBUG 17#ifdef DEBUG
17# include <iostream> 18# include <iostream>
18# include <iomanip> 19# include <iomanip>
19#endif 20#endif
21
22/**
23 * @class CProgramError
24 *
25 * Exception thrown by implemententations of CProgram
26 */
27class CProgramError
28 : public std::invalid_argument
29{
30 public:
31 /**
32 * @method CProgramError
33 * @brief Default exception ctor
34 * @param what message to pass along
35 * @return -
36 * @globalvars none
37 * @exception none
38 * @pre none
39 * @post none
40 */
41 CProgramError(const std::string& what)
42 : std::invalid_argument(what)
43 {}
44};
45
20#include "cinstruction.h" 46#include "cinstruction.h"
21#include "instructions.h" 47#include "instructions.h"
22 48
23/* forward declare CInstruction */ 49/* forward declare CInstruction */
24template<class T=CDat<int>, int width=0> 50template <class T>
25class CInstruction; 51class CInstruction;
26 52
27/** 53/**
@@ -30,9 +56,9 @@ class CInstruction;
30 * CProgram extends std::vector and adds a method for parsing 56 * CProgram extends std::vector and adds a method for parsing
31 * programfile. This adds instances of CInstruction to CProgram itself. 57 * programfile. This adds instances of CInstruction to CProgram itself.
32 */ 58 */
33template<class T=CDat<int>, int width=0> 59template <class T>
34class CProgram 60class CProgram
35 : public std::vector<CInstruction<T> *> 61 : public std::vector<CInstruction<T> *>
36{ 62{
37 typedef typename std::set<CInstruction<T> *>::iterator setiterator; 63 typedef typename std::set<CInstruction<T> *>::iterator setiterator;
38 typedef std::vector<CInstruction<T> *> super; 64 typedef std::vector<CInstruction<T> *> super;
@@ -49,7 +75,8 @@ class CProgram
49 * @return - 75 * @return -
50 * @globalvars none 76 * @globalvars none
51 * @exception none 77 * @exception none
52 * @conditions none 78 * @pre none
79 * @post none
53 */ 80 */
54 CProgram(); 81 CProgram();
55 82
@@ -60,7 +87,8 @@ class CProgram
60 * @return - 87 * @return -
61 * @globalvars none 88 * @globalvars none
62 * @exception none 89 * @exception none
63 * @conditions none 90 * @pre none
91 * @post none
64 */ 92 */
65 ~CProgram(); 93 ~CProgram();
66 94
@@ -71,7 +99,8 @@ class CProgram
71 * @return reference to labels map 99 * @return reference to labels map
72 * @globalvars none 100 * @globalvars none
73 * @exception none 101 * @exception none
74 * @conditions none 102 * @pre none
103 * @post none
75 */ 104 */
76 const std::map<std::string, unsigned>& getLabels() const 105 const std::map<std::string, unsigned>& getLabels() const
77 { 106 {
@@ -84,8 +113,9 @@ class CProgram
84 * @param label name of label to search for 113 * @param label name of label to search for
85 * @return index of found label in program 114 * @return index of found label in program
86 * @globalvars none 115 * @globalvars none
87 * @exception std::runtime_error 116 * @exception CProgramError
88 * @conditions none 117 * @pre none
118 * @post none
89 */ 119 */
90 unsigned findLabel(const std::string& label) const; 120 unsigned findLabel(const std::string& label) const;
91 121
@@ -95,8 +125,9 @@ class CProgram
95 * @param in inputstream to read from 125 * @param in inputstream to read from
96 * @return void 126 * @return void
97 * @globalvars none 127 * @globalvars none
98 * @exception std::runtime_error 128 * @exception CProgramError
99 * @conditions none 129 * @pre none
130 * @post none
100 */ 131 */
101 void compile(std::istream& in); 132 void compile(std::istream& in);
102 133
@@ -108,7 +139,8 @@ class CProgram
108 * @return void 139 * @return void
109 * @globalvars none 140 * @globalvars none
110 * @exception none 141 * @exception none
111 * @conditions none 142 * @pre none
143 * @post none
112 */ 144 */
113 void dump(std::ostream& out); 145 void dump(std::ostream& out);
114#endif 146#endif
@@ -122,7 +154,7 @@ class CProgram
122 154
123/*----------------------------------------------------------------------------*/ 155/*----------------------------------------------------------------------------*/
124 156
125template<class T=CDat<int>, int width=0> 157template <class T>
126CProgram<T>::CProgram() 158CProgram<T>::CProgram()
127{ 159{
128 m_instrset.insert(new CInstructionInc<T>); 160 m_instrset.insert(new CInstructionInc<T>);
@@ -143,7 +175,7 @@ CProgram<T>::CProgram()
143 175
144/*----------------------------------------------------------------------------*/ 176/*----------------------------------------------------------------------------*/
145 177
146template<class T=CDat<int>, int width=0> 178template <class T>
147CProgram<T>::~CProgram() 179CProgram<T>::~CProgram()
148{ 180{
149 /* free instruction set */ 181 /* free instruction set */
@@ -157,7 +189,7 @@ CProgram<T>::~CProgram()
157 189
158/*----------------------------------------------------------------------------*/ 190/*----------------------------------------------------------------------------*/
159 191
160template<class T=CDat<int>, int width=0> 192template <class T>
161void CProgram<T>::compile(std::istream& in) 193void CProgram<T>::compile(std::istream& in)
162{ 194{
163 if (!in.good()) 195 if (!in.good())
@@ -200,7 +232,7 @@ void CProgram<T>::compile(std::istream& in)
200 { 232 {
201 std::stringstream sstr; 233 std::stringstream sstr;
202 sstr << "Unknown instruction '" << instrname << "' on line " << i << "."; 234 sstr << "Unknown instruction '" << instrname << "' on line " << i << ".";
203 throw std::runtime_error(sstr.str()); 235 throw CProgramError(sstr.str());
204 } 236 }
205 237
206 /* create instruction */ 238 /* create instruction */
@@ -219,20 +251,20 @@ void CProgram<T>::compile(std::istream& in)
219 if (instrname == "label") 251 if (instrname == "label")
220 { 252 {
221 if (instrparams.size() != 1) 253 if (instrparams.size() != 1)
222 throw std::runtime_error("Invalid paramater count - must be 1"); 254 throw CInstructionError("Invalid paramater count - must be 1");
223 std::string label(instrparams.front()); 255 std::string label(instrparams.front());
224 if (label.length() < 2 || label[ label.length() - 1] != ':') 256 if (label.length() < 2 || label[ label.length() - 1] != ':')
225 throw std::runtime_error("Label has invalid syntax"); 257 throw CInstructionError("Label has invalid syntax");
226 m_labels[ label.substr(0, label.length() - 1) ] = size(); 258 m_labels[ label.substr(0, label.length() - 1) ] = size();
227 } 259 }
228 instr->compile(instrparams); 260 instr->compile(instrparams);
229 } 261 }
230 catch(std::runtime_error& ex) 262 catch(CInstructionError& ex)
231 { 263 {
232 std::stringstream sstr; 264 std::stringstream sstr;
233 sstr << "Unable to compile instruction '" << instrname 265 sstr << "Unable to compile instruction '" << instrname
234 << "' (line " << i << "): " << ex.what(); 266 << "' (line " << i << "): " << ex.what();
235 throw std::runtime_error(sstr.str()); 267 throw CProgramError(sstr.str());
236 } 268 }
237 269
238 push_back(instr); 270 push_back(instr);
@@ -241,20 +273,20 @@ void CProgram<T>::compile(std::istream& in)
241 273
242/*----------------------------------------------------------------------------*/ 274/*----------------------------------------------------------------------------*/
243 275
244template<class T=CDat<int>, int width=0> 276template <class T>
245unsigned CProgram<T>::findLabel(const std::string& label) const 277unsigned CProgram<T>::findLabel(const std::string& label) const
246{ 278{
247 std::map<std::string, unsigned>::const_iterator it; 279 std::map<std::string, unsigned>::const_iterator it;
248 it = m_labels.find(label); 280 it = m_labels.find(label);
249 if (it == m_labels.end()) 281 if (it == m_labels.end())
250 throw std::runtime_error("Unknown label '" + label + "'"); 282 throw CProgramError("Unknown label '" + label + "'");
251 return it->second; 283 return it->second;
252} 284}
253 285
254/*----------------------------------------------------------------------------*/ 286/*----------------------------------------------------------------------------*/
255 287
256#if DEBUG 288#if DEBUG
257template<class T=CDat<int>, int width=0> 289template <class T>
258void CProgram<T>::dump(std::ostream& out) 290void CProgram<T>::dump(std::ostream& out)
259{ 291{
260 out << "[PROGRAM DUMP]" << std::endl; 292 out << "[PROGRAM DUMP]" << std::endl;