-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBytecodeProgram.cpp
46 lines (37 loc) · 1.29 KB
/
BytecodeProgram.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
#include "BytecodeProgram.hpp"
#include "BytecodeMethod.hpp"
#include "serialize.hpp"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
BytecodeMethod &
BytecodeProgram::addBytecodeMethod(const std::string &name,
std::vector<std::string> variables) {
methods.push_back(BytecodeMethod(name, std::move(variables)));
return methods.back();
}
BytecodeMethod &BytecodeProgram::getBytecodeMethod(const std::string &name) {
const auto &it = std::find(methods.begin(), methods.end(), name);
if (it == methods.end()) {
std::cerr << "Error: Failed to find key " << name
<< " in BytecodeProgram::methods table\n";
}
return *it;
}
void BytecodeProgram::print(std::ostream &os) const {
for (const auto &method : methods) {
method.print(os);
}
}
void BytecodeProgram::serialize(std::ofstream &os) const {
Serializer serializer(os);
const auto &mainMethod = methods.front();
mainMethod.serialize(serializer);
serializer.writeInteger(methods.size() - 1);
std::for_each(std::next(methods.cbegin()), methods.cend(),
[&serializer](const BytecodeMethod &method) {
method.serialize(serializer);
});
}