-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsharpunit.cpp
More file actions
75 lines (56 loc) · 2.1 KB
/
csharpunit.cpp
File metadata and controls
75 lines (56 loc) · 2.1 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
#include "CSharpUnit.h"
const std::vector<std::string> CSharpClassUnit::ACCESS_MODIFIERS = {
"public",
"protected",
"private",
"internal"
};
CSharpClassUnit::CSharpClassUnit(const std::string& name) : m_name(name) {
m_fields.resize(ACCESS_MODIFIERS.size());
}
void CSharpClassUnit::add(const std::shared_ptr<Unit>& unit, Flags flags) {
int accessModifier = PRIVATE;
if (flags < ACCESS_MODIFIERS.size()) {
accessModifier = flags;
}
m_fields[accessModifier].push_back(unit);
}
std::string CSharpClassUnit::compile(unsigned int level) const {
std::string result = generateShift(level) + "class " + m_name + "\n";
result += generateShift(level) + "{\n";
for (size_t i = 0; i < ACCESS_MODIFIERS.size(); ++i) {
if (m_fields[i].empty()) continue;
result += generateShift(level + 1) + ACCESS_MODIFIERS[i] + "\n";
result += generateShift(level + 1) + "{\n";
for (const auto& f : m_fields[i]) {
result += f->compile(level + 2);
}
result += generateShift(level + 1) + "}\n\n";
}
result += generateShift(level) + "}\n";
return result;
}
CSharpMethodUnit::CSharpMethodUnit(const std::string& name, const std::string& returnType, Flags flags)
: m_name(name), m_returnType(returnType), m_flags(flags) {}
void CSharpMethodUnit::add(const std::shared_ptr<Unit>& unit, Flags) {
m_body.push_back(unit);
}
std::string CSharpMethodUnit::compile(unsigned int level) const {
std::string result = generateShift(level);
if (m_flags & STATIC) {
result += "static ";
} else if (m_flags & VIRTUAL) {
result += "virtual ";
}
result += m_returnType + " " + m_name + "()";
result += "\n" + generateShift(level) + "{\n";
for (const auto& b : m_body) {
result += b->compile(level + 1);
}
result += generateShift(level) + "}\n";
return result;
}
CSharpPrintUnit::CSharpPrintUnit(const std::string& text) : m_text(text) {}
std::string CSharpPrintUnit::compile(unsigned int level) const {
return generateShift(level) + "Console.WriteLine(\"" + m_text + "\");\n";
}