996
|
1 // Thread.cpp
|
|
2
|
|
3 #include "lib.h"
|
|
4 #include "Thread.h"
|
|
5 #include "Error.h"
|
|
6 #include "Action.h"
|
|
7
|
|
8 using namespace std;
|
|
9
|
|
10 Thread::Thread()
|
|
11 : status(Error::finished)
|
|
12 {
|
|
13 }
|
|
14
|
|
15 Thread::~Thread()
|
|
16 {
|
|
17 }
|
|
18
|
|
19 void Thread::reset(Board & start, map<string, Board> * newGlobals)
|
|
20 {
|
|
21 dataStack.clear();
|
|
22 programStack.clear();
|
|
23 programStack.push_back(ActivationRecord(start));
|
|
24 globals = newGlobals;
|
|
25 multi.reset();
|
|
26 if (globals != NULL)
|
|
27 {
|
|
28 status = Error::runnable;
|
|
29 }
|
|
30 else
|
|
31 {
|
|
32 status = Error::finished;
|
|
33 }
|
|
34 custom.clear();
|
|
35 }
|
|
36
|
|
37 void Thread::go(void)
|
|
38 {
|
|
39 bool done = false;
|
|
40
|
|
41 if (status != Error::runnable)
|
|
42 {
|
|
43 done = true;
|
|
44 }
|
|
45 while (!done)
|
|
46 {
|
|
47 step();
|
|
48 if (status != Error::runnable)
|
|
49 {
|
|
50 done = true;
|
|
51 }
|
|
52 }
|
|
53 status = Error::finished;
|
|
54 }
|
|
55
|
|
56 void Thread::step(void)
|
|
57 {
|
|
58 if (status == Error::runnable)
|
|
59 {
|
|
60 try
|
|
61 {
|
|
62 programStack.front().move(multi);
|
|
63 Action & act = programStack.front().getAction(multi, dataStack);
|
|
64 act.run(dataStack, programStack, *globals);
|
|
65 }
|
|
66 catch(CrashException & error)
|
|
67 {
|
|
68 status = error.get();
|
|
69 if (status == Error::custom)
|
|
70 {
|
|
71 custom = error.getCustom();
|
|
72 }
|
|
73 else
|
|
74 {
|
|
75 custom.clear();
|
|
76 }
|
|
77 cerr << "Crash: " << Error::errorToString(status);
|
|
78 if (status == Error::custom)
|
|
79 {
|
|
80 cerr << " " << custom << endl;
|
|
81 }
|
|
82 }
|
|
83 }
|
|
84
|
|
85 if (programStack.empty())
|
|
86 {
|
|
87 status = Error::finished;
|
|
88 custom.clear();
|
|
89 }
|
|
90 }
|
|
91
|
|
92 list<Binding> const & Thread::getDataStack(void)
|
|
93 {
|
|
94 return dataStack;
|
|
95 }
|
|
96
|
|
97 list<ActivationRecord> const & Thread::getProgramStack(void)
|
|
98 {
|
|
99 return programStack;
|
|
100 }
|
|
101
|
|
102 Error::t Thread::getStatus(void)
|
|
103 {
|
|
104 return status;
|
|
105 }
|
|
106
|
|
107 string Thread::getErrorString(void)
|
|
108 {
|
|
109 if (status == Error::custom)
|
|
110 {
|
|
111 return Error::errorToString(status) + ": " + custom;
|
|
112 }
|
|
113 else
|
|
114 {
|
|
115 return Error::errorToString(status);
|
|
116 }
|
|
117 }
|