This repository has been archived by the owner on Jul 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.cpp
232 lines (208 loc) · 7.58 KB
/
vm.cpp
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <iostream>
#include <string.h>
#include "vm.hpp"
namespace vm
{
VM::VM() :
m_stack_size(0)
{}
VM::~VM() {}
void VM::push(Value value)
{
m_stack.push_back(value);
m_stack_size++;
}
Value VM::pop()
{
return abc::pop(m_stack, --m_stack_size);
}
void VM::clear()
{
m_stack.clear();
m_stack_size = 0;
m_variables.clear();
m_segments.clear();
}
char VM::getByte(char* bytecode, unsigned s, unsigned i)
{
if (i < s)
return bytecode[i];
throw std::runtime_error("Index out of range, can not get next byte ! => Malformed bytecode");
}
int VM::exec(char* bytecode, unsigned s)
{
clear();
for (unsigned i=0; i < s; ++i)
{
char instruction = getByte(bytecode, s, i);
switch (instruction)
{
case INST_INT:
{
std::cout << "int" << std::endl;
Value v;
v.type = TYPE_INT;
v.intValue = (getByte(bytecode, s, ++i) << 8) + getByte(bytecode, s, ++i);
push(v);
break;
}
case INST_STR:
{
std::cout << "str" << std::endl;
unsigned str_size = (getByte(bytecode, s, ++i) << 8) + getByte(bytecode, s, ++i);
if (str_size > 0)
{
Value a;
a.type = TYPE_STRING;
++i;
for (unsigned j=i; i - j < str_size; ++i)
{ a.stringValue += getByte(bytecode, s, i); }
i--;
push(a);
}
else
{ throw std::logic_error("Invalid size given for the string to store"); }
break;
}
case INST_BOOL:
{
std::cout << "bool" << std::endl;
Value a;
a.type = TYPE_BOOL;
a.boolValue = getByte(bytecode, s, ++i) > 0;
push(a);
break;
}
case INST_VAR:
{
std::cout << "var" << std::endl;
unsigned str_size = getByte(bytecode, s, ++i);
if (str_size > 0)
{
Value a;
a.type = TYPE_STRING;
++i;
for (unsigned j=i; i - j < str_size; ++i)
{ a.stringValue += getByte(bytecode, s, i); }
i--;
push(a);
}
else
{ throw std::logic_error("Invalid size given for the variable name to store"); }
break;
}
case INST_SEGMENT:
{
std::cout << "segment" << std::endl;
unsigned seg_size = getByte(bytecode, s, ++i);
std::string seg_name;
if (seg_size > 0)
{
++i;
for (unsigned j=i; i - j < seg_size; ++i)
{ seg_name += getByte(bytecode, s, i); }
--i;
}
else
{ throw std::logic_error("Invalid size for the segment name"); }
m_segments[seg_name] = i;
break;
}
case INST_STORE_VAR:
{
std::cout << "store var" << std::endl;
Value var_name = pop();
Value val = pop();
if (var_name.type == TYPE_STRING)
{ m_variables[var_name.stringValue] = val; }
else
{ throw std::logic_error("A variable name should be a string"); }
break;
}
case INST_PUSH_VAR:
{
std::cout << "push var" << std::endl;
unsigned str_size = getByte(bytecode, s, ++i);
if (str_size > 0)
{
std::string v;
++i;
for (unsigned j=i; i - j < str_size; ++i)
{ v += getByte(bytecode, s, i); }
i--;
if (m_variables.find(v) != m_variables.end())
{ push(m_variables[v]); }
else
{ throw std::runtime_error("Can not push an undefined variable onto the stack"); }
}
else
{ throw std::logic_error("Invalid size given for the variable name to fetch"); }
break;
}
case INST_JUMP:
{
std::cout << "jump" << std::endl;
unsigned str_size = getByte(bytecode, s, ++i);
if (str_size > 0)
{
std::string v;
++i;
for (unsigned j=i; i - j < str_size; ++i)
{ v += getByte(bytecode, s, i); }
i--;
if (m_segments.find(v) != m_segments.end())
{ i = m_segments[v]; }
else
{ throw std::runtime_error("Can not jump to an undefined segment"); }
}
else
{ throw std::logic_error("Invalid size given for the segment name to fetch"); }
break;
}
case INST_ADD:
{
std::cout << "add" << std::endl;
Value b = pop();
Value a = pop();
if (a.type == b.type)
{
if (a.type == TYPE_INT)
{
Value c;
c.type = TYPE_INT;
c.intValue = b.intValue + a.intValue;
push(c);
}
else if (a.type == TYPE_STRING)
{
Value c;
c.type = TYPE_STRING;
c.stringValue = std::string(a.stringValue) + std::string(b.stringValue);
push(c);
}
else
{ throw std::logic_error("Can not add two booleans"); }
}
else
{ throw std::logic_error("Can not add two variables of heterogeneous types"); }
break;
}
default:
{
// could be end of string
if (instruction != 0x00)
{ throw std::runtime_error("Can not identify the instruction " + std::string(instruction, 1)); }
}
}
}
return 0;
}
int VM::execSegment(char* bytecode, char* segment_name)
{
return 0;
}
std::vector<Value>& VM::getStack()
{
return m_stack;
}
} // namespace vm