-
Notifications
You must be signed in to change notification settings - Fork 4
/
AssemblyCode.cpp
89 lines (86 loc) · 2.92 KB
/
AssemblyCode.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
/*
* AssemblyCode is some sort of a decorater for the "string" class, as
* we want it to be able to do whatever the C++ "string" can (at least
* as far as this program is concerned), but it should also do some
* things useful for dealing with Assembly code, such as keeping the
* information about type and indenting it.
*/
#include <ciso646> // Necessary for Microsoft C++ Compiler.
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
struct AssemblyCode {
enum class AssemblyType {
i32,
i64,
f32,
f64,
null
} assemblyType =
AssemblyType::null; // Those are actual types from WebAssembly (they
// obviously don't correspond to the JavaScript types,
// for some reason).
std::string code;
AssemblyCode &indentBy(int numberOfTabs) {
std::vector<std::string> tokenized;
for (auto i = code.begin(); i != code.end(); i++)
if (tokenized.empty()) {
tokenized.push_back(std::string());
tokenized.back().push_back(*i);
} else if (*i == '\n') {
tokenized.back().push_back(*i);
tokenized.push_back(std::string());
} else
tokenized.back().push_back(*i);
for (auto i = tokenized.begin(); i != tokenized.end(); i++)
for (int j = 0; j < numberOfTabs; j++)
*i = "\t" + *i;
std::stringstream stream;
std::ostream_iterator<std::string> streamIterator(stream);
std::copy(tokenized.begin(), tokenized.end(), streamIterator);
code = stream.str();
while (code.back() == '\t')
code = code.substr(0, code.size() - 1);
return *this;
}
AssemblyCode substr(const unsigned long long first,
const unsigned long long length = std::string::npos) {
return code.substr(first, length);
}
AssemblyCode(std::string code, AssemblyType type = AssemblyType::null) {
this->code = code;
assemblyType = type;
}
operator std::string() { return code; }
AssemblyCode operator+(AssemblyCode anotherAssemblyCode) {
return AssemblyCode(code + anotherAssemblyCode.code, assemblyType);
}
AssemblyCode operator+(std::string newCode) {
return AssemblyCode(code + newCode, assemblyType);
}
AssemblyCode operator+(char character) {
std::string newCode = code;
newCode += character;
return AssemblyCode(newCode, assemblyType);
}
AssemblyCode &operator+=(char character) {
code += character;
return *this;
}
AssemblyCode &operator+=(AssemblyCode anotherAssemblyCode) {
return *this = *this + anotherAssemblyCode;
}
AssemblyCode &operator+=(std::string newCode) {
return *this = *this + newCode;
}
AssemblyCode &operator=(std::string newCode) {
code = newCode;
return *this;
}
};
AssemblyCode operator+(std::string str, AssemblyCode assembly) {
AssemblyCode::AssemblyType assemblyType = assembly.assemblyType;
std::string code = str + assembly.code;
return AssemblyCode(code, assemblyType);
}