-
Notifications
You must be signed in to change notification settings - Fork 0
/
VbCodeExpressionFactory.cpp
287 lines (270 loc) · 10.7 KB
/
VbCodeExpressionFactory.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "VbCodeExpressionFactory.h"
#include "VbCodeUnaryExpression.h"
#include "VbCodeBinaryExpression.h"
#include "VbCodeConstantExpression.h"
#include "VbCodeWithExpression.h"
#include "VbCodeMemberExpression.h"
#include "VbCodeIdExpression.h"
#include "VbCodeCallExpression.h"
#include "VbCodeMissingExpression.h"
#include "VbCodeLenExpression.h"
#include "VbCodeMeExpression.h"
#include "VbCodeParenthesisExpression.h"
#include "VbExpression.h"
#include "VbXorExpression.h"
#include "VbOrExpression.h"
#include "VbAndExpression.h"
#include "VbMultiplicativeExpression.h"
#include "VbAdditiveExpression.h"
#include "VbEqualityExpression.h"
#include "VbRelationalExpression.h"
#include "VbUnaryExpression.h"
#include "VbPostfixExpression.h"
#include "VbPrimaryExpression.h"
#include "VbCodeValueFactory.h"
#include "VbDefaultValue.h"
#include "VbExpressionClause.h"
#include "VbExpressionList.h"
#include "VbExpressionItem.h"
#include "VbLValue.h"
#include "VbLValueTerminal.h"
#include "VbCallStatement.h"
#include "VbCallSuffix.h"
#include "VbCallTerminal.h"
VbCodeExpressionPtr VbCodeExpressionFactory::CreateDefaultValue(const optional<Sentence>& sentence)
{
if (!sentence)
return nullptr;
VbDefaultValue defaultValue{ *sentence };
return CreateExpression(defaultValue.expression);
}
VbCodeExpressionPtr VbCodeExpressionFactory::CreateExpression(const Sentence& sentence)
{
VbExpression expression{ sentence };
return CreateOrExpression(expression.orExpression);
}
VbCodeExpressionPtr VbCodeExpressionFactory::CreateLValue(const Sentence& sentence)
{
VbLValue lValue{ sentence };
VbCodeExpressionPtr expression;
if (lValue.wsDot)
expression = std::make_shared<VbCodeWithExpression>(ToDotType(*lValue.wsDot), lValue.id.GetValue());
else if (lValue.id == "me")
expression = std::make_shared<VbCodeMeExpression>();
else
expression = std::make_shared<VbCodeIdExpression>(lValue.id.GetValue());
if (lValue.suffix)
for (auto& terminalSentence : *lValue.suffix)
{
VbLValueTerminal terminal{ terminalSentence };
if (terminal.dot)
expression = std::make_shared<VbCodeMemberExpression>(
expression,
ToDotType(*terminal.dot),
terminal.id->GetValue());
else
expression = std::make_shared<VbCodeCallExpression>(
expression,
CreateExpressionClause(*terminal.expressionClause));
}
return expression;
}
VbCodeExpressionPtr VbCodeExpressionFactory::CreateCallStatement(const Sentence& sentence)
{
VbCallStatement callStatement{ sentence };
if (callStatement.lValue)
{
auto lValue = CreateLValue(*callStatement.lValue);
std::vector<VbCodeExpressionPtr> arguments;
if (callStatement.expressionList)
arguments = CreateExpressionList(*callStatement.expressionList);
return std::make_shared<VbCodeCallExpression>(lValue, arguments);
}
VbCallSuffix callSuffix{ *callStatement.callSuffix };
VbCallTerminal firstTerminal{ callSuffix.callTerminal };
VbCodeExpressionPtr expression;
if (callStatement.isMe)
expression = std::make_shared<VbCodeMemberExpression>(
std::make_shared<VbCodeMeExpression>(),
VbCodeDotType::Dot,
firstTerminal.name.GetValue());
else if (callStatement.wsDot)
expression = std::make_shared<VbCodeWithExpression>(
ToDotType(*callStatement.wsDot),
firstTerminal.name.GetValue());
else
expression = std::make_shared<VbCodeIdExpression>(firstTerminal.name.GetValue());
if (firstTerminal.expressionClause)
expression = std::make_shared<VbCodeCallExpression>(expression, CreateExpressionClause(*firstTerminal.expressionClause));
for (auto& suffix : callSuffix.suffix)
{
VbCallTerminal nextTerminal{ suffix.second };
expression = std::make_shared<VbCodeMemberExpression>(
expression,
ToDotType(suffix.first),
nextTerminal.name.GetValue());
if (nextTerminal.expressionClause)
expression = std::make_shared<VbCodeCallExpression>(expression, CreateExpressionClause(*nextTerminal.expressionClause));
}
return expression;
}
VbCodeExpressionPtr VbCodeExpressionFactory::CreateOrExpression(const Sentence& sentence)
{
VbOrExpression expression{ sentence };
if (!expression.orExpression)
return CreateXorExpression(expression.xorExpression);
return std::make_shared<VbCodeBinaryExpression>(
VbCodeBinaryExpressionType::Or,
CreateOrExpression(*expression.orExpression),
CreateXorExpression(expression.xorExpression));
}
VbCodeExpressionPtr VbCodeExpressionFactory::CreateXorExpression(const Sentence& sentence)
{
VbXorExpression expression{ sentence };
if (!expression.xorExpression)
return CreateAndExpression(expression.andExpression);
return std::make_shared<VbCodeBinaryExpression>(
VbCodeBinaryExpressionType::Xor,
CreateXorExpression(*expression.xorExpression),
CreateAndExpression(expression.andExpression));
}
VbCodeExpressionPtr VbCodeExpressionFactory::CreateAndExpression(const Sentence& sentence)
{
VbAndExpression expression{ sentence };
if (!expression.andExpression)
return CreateEqualityExpression(expression.equalityExpression);
return std::make_shared<VbCodeBinaryExpression>(
VbCodeBinaryExpressionType::And,
CreateAndExpression(*expression.andExpression),
CreateEqualityExpression(expression.equalityExpression));
}
VbCodeExpressionPtr VbCodeExpressionFactory::CreateEqualityExpression(const Sentence& sentence)
{
VbEqualityExpression expression{ sentence };
if (!expression.equalityExpression)
return CreateRelationalExpression(expression.relationalExpression);
return std::make_shared<VbCodeBinaryExpression>(
ToEqualityOperator(*expression.op),
CreateEqualityExpression(*expression.equalityExpression),
CreateRelationalExpression(expression.relationalExpression));
}
VbCodeExpressionPtr VbCodeExpressionFactory::CreateRelationalExpression(const Sentence& sentence)
{
VbRelationalExpression expression{ sentence };
if (!expression.relationalExpression)
return CreateAdditiveExpression(expression.additiveExpression);
return std::make_shared<VbCodeBinaryExpression>(
ToRelationalOperator(*expression.op),
CreateRelationalExpression(*expression.relationalExpression),
CreateAdditiveExpression(expression.additiveExpression));
}
VbCodeExpressionPtr VbCodeExpressionFactory::CreateAdditiveExpression(const Sentence& sentence)
{
VbAdditiveExpression expression{ sentence };
if (!expression.additiveExpression)
return CreateMultiplicativeExpression(expression.multiplicativeExpression);
return std::make_shared<VbCodeBinaryExpression>(
ToAdditiveOperator(*expression.op),
CreateAdditiveExpression(*expression.additiveExpression),
CreateMultiplicativeExpression(expression.multiplicativeExpression));
}
VbCodeExpressionPtr VbCodeExpressionFactory::CreateMultiplicativeExpression(const Sentence& sentence)
{
VbMultiplicativeExpression expression{ sentence };
if (!expression.multiplicativeExpression)
return CreateUnaryExpression(expression.unaryExpression);
return std::make_shared<VbCodeBinaryExpression>(
ToMultiplicativeOperator(*expression.op),
CreateMultiplicativeExpression(*expression.multiplicativeExpression),
CreateUnaryExpression(expression.unaryExpression));
}
VbCodeExpressionPtr VbCodeExpressionFactory::CreateUnaryExpression(const Sentence& sentence)
{
VbUnaryExpression expression{ sentence };
if (expression.postfixExpression)
return CreatePostfixExpression(*expression.postfixExpression);
auto& op = *expression.op;
if (op == "new")
throw std::runtime_error("Not implemented: new");
if (op == "addressof")
throw std::runtime_error("Not implemented: addressof");
if (op == "typeof")
throw std::runtime_error("Not implemented: typeof");
if (op == "string")
throw std::runtime_error("Not implemented: string");
if (op == "len")
{
auto expressions = CreateExpressionClause(*expression.expressionClause);
if (expressions.size() != 1)
throw std::runtime_error("Only one argument to Len is supported.");
return std::make_shared<VbCodeLenExpression>(expressions[0]);
}
return std::make_shared<VbCodeUnaryExpression>(
ToUnaryOperator(op),
CreateUnaryExpression(*expression.unaryExpression));
}
VbCodeExpressionPtr VbCodeExpressionFactory::CreatePostfixExpression(const Sentence& sentence)
{
VbPostfixExpression expression{ sentence };
if (expression.primaryExpression)
return CreatePrimaryExpression(*expression.primaryExpression);
if (expression.dot)
return std::make_shared<VbCodeMemberExpression>(
CreatePostfixExpression(*expression.postfixExpression),
ToDotType(*expression.dot),
expression.id->GetValue());
return std::make_shared<VbCodeCallExpression>(
CreatePostfixExpression(*expression.postfixExpression),
CreateExpressionClause(*expression.expressionClause));
}
VbCodeExpressionPtr VbCodeExpressionFactory::CreatePrimaryExpression(const Sentence& sentence)
{
VbPrimaryExpression expression{ sentence };
if (expression.literal)
return CreateConstantExpression(*expression.literal);
if (expression.wsDot)
return std::make_shared<VbCodeWithExpression>(ToDotType(*expression.wsDot), expression.id->GetValue());
if (expression.expression2)
throw std::runtime_error("Not implemented: (expression, expression)");
if (expression.expression1)
return std::make_shared<VbCodeParenthesisExpression>(CreateExpression(*expression.expression1));
if (*expression.id == "me")
return std::make_shared<VbCodeMeExpression>();
return std::make_shared<VbCodeIdExpression>(expression.id->GetValue());
}
VbCodeExpressionPtr VbCodeExpressionFactory::CreateConstantExpression(const Sentence& sentence)
{
if (sentence.GetNodes().size() != 1)
throw std::runtime_error("Literal should contain exactly 1 token.");
auto& value = VbCodeValueFactory::Create(sentence.GetNodes()[0]->AsToken());
return std::make_shared<VbCodeConstantExpression>(value);
}
std::vector<VbCodeExpressionPtr> VbCodeExpressionFactory::CreateExpressionClause(const Sentence& sentence)
{
VbExpressionClause expressionClause{ sentence };
if (expressionClause.expressionList)
return CreateExpressionList(*expressionClause.expressionList);
return{};
}
std::vector<VbCodeExpressionPtr> VbCodeExpressionFactory::CreateExpressionList(const Sentence& sentence)
{
std::vector<VbCodeExpressionPtr> expressions;
VbExpressionList expressionList{ sentence };
if (expressionList.separator && *expressionList.separator != ",")
throw std::runtime_error("';' statement separator not yet implemented.");
for (auto& expressionItemSentence : expressionList.expressionItems)
{
VbExpressionItem expressionItem{ expressionItemSentence };
if (!expressionItem.expression)
expressions.push_back(std::make_shared<VbCodeMissingExpression>());
else
{
if (expressionItem.passBy)
throw std::runtime_error("Pass-by on expression item not yet implemented.");
if (expressionItem.name)
throw std::runtime_error("Named expression items not yet implemented.");
expressions.push_back(CreateExpression(*expressionItem.expression));
}
}
return expressions;
}