Repository navigation
Expand file tree
/
Copy pathVar.cpp
More file actions
237 lines (193 loc) · 4.86 KB
/
Copy pathVar.cpp
File metadata and controls
237 lines (193 loc) · 4.86 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
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
233
234
235
236
237
#include "code_generator/Var.hpp"
#include "code_generator/Array.hpp"
#include "code_generator/Ptr.hpp"
#include "code_generator/Evaluate.hpp"
#include "code_generator/Function.hpp"
#include "fmt/format.h"
#include "code_generator/StringCodeWriter.hpp"
#include <stdexcept>
const uint16_t Var::ID = IdGenerator::nextID();
const uint16_t VarDefinition::ID = IdGenerator::nextID();
VarRef Var::create(const String &name, TypeRef parent)
{
return createRefObject<Var>(name, parent);
}
Var::Var(const String &name, TypeRef parent)
: m_type (parent)
, m_name (name)
{
}
VarRef Var::assign(const CodeRef &code)
{
return this->operator=(code);
}
VarRef Var::operator=(const CodeRef &code)
{
VarRef newVar = Var::create(name(), type());
newVar->m_value = code;
return newVar;
}
VarRef Var::index(size_t idx)
{
TypeRef p = type();
if (p == nullptr)
return nullptr;
if (p->id() != Array::ID) {
return nullptr;
}
ArrayRef array = StaticCastRefObject<Array>(p);
if (array == nullptr)
return nullptr;
String idxName = fmt::format("{}[{}]", name(), idx);
if (int32_t(idx) >= array->size()) {
throw std::out_of_range("Var::index: out of range, value is " + idxName);
}
return Var::create(idxName, array->parent());
}
VarRef Var::ref()
{
TypeRef p = type();
if (p == nullptr)
return nullptr;
return nullptr;
}
VarRef Var::element()
{
TypeRef p = type();
if (p == nullptr)
return nullptr;
if (p->id() != Ptr::ID) {
throw std::domain_error("Var::element: could not unref for a not ptr type");
}
TypeRef typ = p->parent();
if (typ == nullptr) {
throw std::domain_error("Var::element: could not create a empty type");
}
String eName = fmt::format("*{}", name());
return Var::create(eName, typ);
}
VarRef Var::address()
{
TypeRef p = type();
if (p == nullptr)
return nullptr;
String eName = fmt::format("&{}", name());
return Var::create(eName, Ptr::create(p));
}
CodeRef Var::call(const String &name, const Var::CodeContainer &container)
{
return call_(name, container, shared_from_this());
}
String Var::toString()
{
StringCodeWriter writer;
write(writer);
return writer.str();
}
int Var::write(CodeWriter &writer)
{
writer.writeString(name());
if (value() != nullptr) {
writer.writeString("$s=$s");
value()->write(writer);
}
return 0;
}
Code::Kind Var::kind() const {
if (m_type == nullptr)
return CodeStatment;
return m_type->kind();
}
CodeRef Var::value() const
{
return m_value;
}
TypeRef Var::type() const
{
return m_type;
}
String Var::name() const
{
return m_name;
}
VarDefinitionRef Var::definition() const
{
return VarDefinition::create(name(), type());
}
VarDefinitionRef VarDefinition::create(const String &name, TypeRef typ, const CodeRef &value)
{
return createRefObject<VarDefinition>(name, typ, value);
}
VarDefinition::VarDefinition(const String &name, TypeRef parent, const CodeRef &value)
: m_namedType (NamedType::create(name, parent))
, m_value(value) {
}
String VarDefinition::toString()
{
return m_namedType->toString();
}
int VarDefinition::write(CodeWriter &writer)
{
m_namedType->write(writer);
if (value() != nullptr) {
if (assignKind() == AssignKind::EQUAL) {
writer.writeString("$s=$s");
value()->write(writer);
} else {
writer.writeString("(");
value()->write(writer);
writer.writeString(")");
}
}
return 0;
}
Code::Kind VarDefinition::kind() const
{
if (m_namedType == nullptr)
return CodeStatment;
return m_namedType->kind();
}
CodeRef VarDefinition::value() const
{
return m_value;
}
TypeRef VarDefinition::type() const
{
return m_namedType->type();
}
String VarDefinition::name() const
{
return m_namedType->name();
}
VarDefinitionRef VarDefinition::assign(const CodeRef &code)
{
VarDefinitionRef vardef = VarDefinition::create(name(), type(), code);
vardef->setAssignKind(AssignKind::EQUAL);
return vardef;
}
VarDefinitionRef VarDefinition::construct(const CodeRef &code)
{
VarDefinitionRef vardef = VarDefinition::create(name(), type(), code);
vardef->setAssignKind(AssignKind::CONSTRUCTOR);
return vardef;
}
VarDefinitionRef VarDefinition::construct(const VarDefinition::CodeRefList &codes)
{
StringCodeWriter writer;
for (size_t i = 0; i < codes.size(); ++i) {
if (i != 0) {
writer.writeString(",$s");
}
auto& code = codes[i];
code->write(writer);
}
return construct($(writer.str()));
}
VarDefinition::AssignKind VarDefinition::assignKind() const
{
return m_assignKind;
}
void VarDefinition::setAssignKind(const AssignKind &assignKind)
{
m_assignKind = assignKind;
}