forked from dsenta00/BOXVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoxProgram.h
More file actions
171 lines (146 loc) · 3.65 KB
/
Copy pathBoxProgram.h
File metadata and controls
171 lines (146 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#ifndef BOXPROGRAM_H
#define BOXPROGRAM_H
#include "BoxInfo.h"
#include "ProgramMonitor.h"
#include "DataNode.h"
#include "InvokeBox.h"
#include "Execute.h"
class BoxProgram {
public:
BoxProgram();
BoxProgram(ProgramMonitor *);
~BoxProgram();
void ReadSourceCode(char *);
void SetStack();
void SetPool();
void SetHeap();
void ExecuteProgram();
protected:
InvokeBox boxfile;
Stack stack;
Heap *heap;
ProgramMonitor *monitor;
};
BoxProgram::BoxProgram()
{
heap = null;
monitor = null;
}
BoxProgram::BoxProgram(ProgramMonitor *_monitor)
{
heap = null;
monitor = _monitor;
}
void BoxProgram::ReadSourceCode(char *_filepath)
{
boxfile.ReadAll(_filepath, monitor);
}
void BoxProgram::SetStack()
{
Type datatype = 0;
int intID = 0, i = 0;
char buffer[MAXBUFFERWORD] = { 0 };
stack.SetMonitor(monitor);
boxfile.GetNextWord(&intID);
stack.SetVirtualMemory(intID);
if (EOK)
{
do
{
boxfile.GetNextWord(&datatype);
if (datatype == '@')
{
break;
}
else if(datatype > 6 || datatype < 0)
{
SETERR(STAT_TYPE_ERR);
break;
}
switch (datatype)
{
case _CHAR:
boxfile.GetNextWord(&buffer[0]);
break;
case _INT: case _FLOAT:
boxfile.GetNextWord(&buffer[0]);
boxfile.GetNextWord(&buffer[1]);
boxfile.GetNextWord(&buffer[2]);
boxfile.GetNextWord(&buffer[3]);
break;
case _LONG: case _DOUBLE:
boxfile.GetNextWord(&buffer[0]);
boxfile.GetNextWord(&buffer[1]);
boxfile.GetNextWord(&buffer[2]);
boxfile.GetNextWord(&buffer[3]);
boxfile.GetNextWord(&buffer[4]);
boxfile.GetNextWord(&buffer[5]);
boxfile.GetNextWord(&buffer[6]);
boxfile.GetNextWord(&buffer[7]);
break;
case _SHORT:
boxfile.GetNextWord(&buffer[0]);
boxfile.GetNextWord(&buffer[1]);
break;
case _STRING:
i = 0;
boxfile.GetNextWord(&buffer[i]);
while (buffer[i]) // 0 is end of string
boxfile.GetNextWord(&buffer[++i]);
buffer[i] = '\0';
break;
}
stack.PushStack(datatype, buffer);
}while(EOK);
}
}
void BoxProgram::SetPool()
{
int intID = 0;
boxfile.GetNextWord(&intID);
if (intID)
{
heap = new Heap(intID, monitor);
}
}
void BoxProgram::SetHeap()
{
Type datatype = 0;
do
{
boxfile.GetNextWord(&datatype);
if(datatype >= _CHAR && datatype <= _STRING)
{
heap->PushPointer(datatype);
}
else if(datatype == START)
{
break;
}
else
{
SETERR(DYN_TYPE_ERR);
break;
}
} while (EOK);
}
void BoxProgram::ExecuteProgram()
{
Execute processor (
boxfile.GetCodeSegment(),
boxfile.GetExecuteCode(),
monitor
);
if (EOK)
{
processor.Do(stack, *heap);
}
}
BoxProgram::~BoxProgram()
{
if(heap)
{
delete heap;
}
}
#endif //BOXPROGRAM_H