1 // =================================
  2 // Copyright (c) 2020 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 #include <sngcpp/ast/Statement.hpp>
  7 #include <sngcpp/ast/Visitor.hpp>
  8 #include <sngcpp/ast/Writer.hpp>
  9 #include <sngcpp/ast/Reader.hpp>
 10 
 11 namespace sngcpp { namespace ast {
 12 
 13 LabeledStatementNode::LabeledStatementNode() : UnaryNode(NodeType::labeledStatementNode)
 14 {
 15 }
 16 
 17 LabeledStatementNode::LabeledStatementNode(const Span& span_const std::u32string& label_Node* statement_) : UnaryNode(NodeType::labeledStatementNodespan_statement_)label(label_)
 18 {
 19 }
 20 
 21 void LabeledStatementNode::Accept(Visitor& visitor)
 22 {
 23     visitor.Visit(*this);
 24 }
 25 
 26 void LabeledStatementNode::Write(Writer& writer)
 27 {
 28     UnaryNode::Write(writer);
 29     writer.GetBinaryWriter().Write(label);
 30 }
 31 
 32 void LabeledStatementNode::Read(Reader& reader)
 33 {
 34     UnaryNode::Read(reader);
 35     label = reader.GetBinaryReader().ReadUtf32String();
 36 }
 37 
 38 CaseStatementNode::CaseStatementNode() : UnaryNode(NodeType::caseStatementNode)
 39 {
 40 }
 41 
 42 CaseStatementNode::CaseStatementNode(const Span& span_Node* caseExpr_Node* statement_) : UnaryNode(NodeType::caseStatementNodespan_statement_)caseExpr(caseExpr_)
 43 {
 44 }
 45 
 46 void CaseStatementNode::Accept(Visitor& visitor)
 47 {
 48     visitor.Visit(*this);
 49 }
 50 
 51 void CaseStatementNode::Write(Writer& writer)
 52 {
 53     UnaryNode::Write(writer);
 54     caseExpr->Write(writer);
 55 }
 56 
 57 void CaseStatementNode::Read(Reader& reader)
 58 {
 59     UnaryNode::Read(reader);
 60     caseExpr.reset(reader.ReadNode());
 61 }
 62 
 63 DefaultStatementNode::DefaultStatementNode() : UnaryNode(NodeType::defaultStatementNode)
 64 {
 65 }
 66 
 67 DefaultStatementNode::DefaultStatementNode(const Span& span_Node* statement_) : UnaryNode(NodeType::defaultStatementNodespan_statement_)
 68 {
 69 }
 70 
 71 void DefaultStatementNode::Accept(Visitor& visitor)
 72 {
 73     visitor.Visit(*this);
 74 }
 75 
 76 ExpressionStatementNode::ExpressionStatementNode() : UnaryNode(NodeType::expressionStatementNode)
 77 {
 78 }
 79 
 80 ExpressionStatementNode::ExpressionStatementNode(const Span& span_Node* expression_) : UnaryNode(NodeType::expressionStatementNodespan_expression_)
 81 {
 82 }
 83 
 84 void ExpressionStatementNode::Accept(Visitor& visitor)
 85 {
 86     visitor.Visit(*this);
 87 }
 88 
 89 CompoundStatementNode::CompoundStatementNode() : UnaryNode(NodeType::compoundStatementNode)
 90 {
 91 }
 92 
 93 CompoundStatementNode::CompoundStatementNode(const Span& span_) : UnaryNode(NodeType::compoundStatementNodespan_)
 94 {
 95 }
 96 
 97 void CompoundStatementNode::Accept(Visitor& visitor)
 98 {
 99     visitor.Visit(*this);
100 }
101 
102 void CompoundStatementNode::Write(Writer& writer)
103 {
104     UnaryNode::Write(writer);
105     writer.Write(endBraceSpan);
106 }
107 
108 void CompoundStatementNode::Read(Reader& reader)
109 {
110     UnaryNode::Read(reader);
111     reader.Read(endBraceSpan);
112 }
113 
114 void CompoundStatementNode::AddStatement(const Span& span_Node* statement)
115 {
116     if (Child())
117     {
118         SetChild(new StatementSequenceNode(span_ReleaseChild()statement));
119     }
120     else
121     {
122         SetChild(statement);
123     }
124 }
125 
126 StatementSequenceNode::StatementSequenceNode() : BinaryNode(NodeType::statementSequenceNode)
127 {
128 }
129 
130 StatementSequenceNode::StatementSequenceNode(const Span& span_Node* leftStatement_Node* rightStatement_) : BinaryNode(NodeType::statementSequenceNodespan_leftStatement_rightStatement_)
131 {
132 }
133 
134 void StatementSequenceNode::Accept(Visitor& visitor)
135 {
136     visitor.Visit(*this);
137 }
138 
139 IfStatementNode::IfStatementNode() : Node(NodeType::ifStatementNode)
140 {
141 }
142 
143 IfStatementNode::IfStatementNode(const Span& span_Node* condition_Node* thenS_Node* elseS_const Span& elseSpan_) :
144     Node(NodeType::ifStatementNodespan_)condition(condition_)thenS(thenS_)elseS(elseS_)elseSpan(elseSpan_)
145 {
146 }
147 
148 void IfStatementNode::Accept(Visitor& visitor)
149 {
150     visitor.Visit(*this);
151 }
152 
153 void IfStatementNode::Write(Writer& writer)
154 {
155     Node::Write(writer);
156     condition->Write(writer);
157     thenS->Write(writer);
158     writer.GetBinaryWriter().Write(elseS != nullptr);
159     if (elseS)
160     {
161         elseS->Write(writer);
162         writer.Write(elseSpan);
163     }
164 }
165 
166 void IfStatementNode::Read(Reader& reader)
167 {
168     Node::Read(reader);
169     condition.reset(reader.ReadNode());
170     thenS.reset(reader.ReadNode());
171     bool hasElseS = reader.GetBinaryReader().ReadBool();
172     if (hasElseS)
173     {
174         elseS.reset(reader.ReadNode());
175         reader.Read(elseSpan);
176     }
177 }
178 
179 SwitchStatementNode::SwitchStatementNode() : Node(NodeType::switchStatementNode)
180 {
181 }
182 
183 SwitchStatementNode::SwitchStatementNode(const Span& span_Node* condition_Node* statement_) : Node(NodeType::switchStatementNodespan_)condition(condition_)statement(statement_)
184 {
185 }
186 
187 void SwitchStatementNode::Accept(Visitor& visitor)
188 {
189     visitor.Visit(*this);
190 }
191 
192 void SwitchStatementNode::Write(Writer& writer)
193 {
194     Node::Write(writer);
195     condition->Write(writer);
196     statement->Write(writer);
197 }
198 
199 void SwitchStatementNode::Read(Reader& reader)
200 {
201     Node::Read(reader);
202     condition.reset(reader.ReadNode());
203     statement.reset(reader.ReadNode());
204 }
205 
206 WhileStatementNode::WhileStatementNode() : Node(NodeType::whileStatementNode)
207 {
208 }
209 
210 WhileStatementNode::WhileStatementNode(const Span& span_Node* condition_Node* statement_) : Node(NodeType::whileStatementNodespan_)condition(condition_)statement(statement_)
211 {
212 }
213 
214 void WhileStatementNode::Accept(Visitor& visitor)
215 {
216     visitor.Visit(*this);
217 }
218 
219 void WhileStatementNode::Write(Writer& writer)
220 {
221     Node::Write(writer);
222     condition->Write(writer);
223     statement->Write(writer);
224 }
225 
226 void WhileStatementNode::Read(Reader& reader)
227 {
228     Node::Read(reader);
229     condition.reset(reader.ReadNode());
230     statement.reset(reader.ReadNode());
231 }
232 
233 DoStatementNode::DoStatementNode() : Node(NodeType::doStatementNode)
234 {
235 }
236 
237 DoStatementNode::DoStatementNode(const Span& span_Node* statement_Node* condition_const Span& whileSpan_) :
238     Node(NodeType::doStatementNodespan_)statement(statement_)condition(condition_)whileSpan(whileSpan_)
239 {
240 }
241 
242 void DoStatementNode::Accept(Visitor& visitor)
243 {
244     visitor.Visit(*this);
245 }
246 
247 void DoStatementNode::Write(Writer& writer)
248 {
249     Node::Write(writer);
250     statement->Write(writer);
251     condition->Write(writer);
252     writer.Write(whileSpan);
253 }
254 
255 void DoStatementNode::Read(Reader& reader)
256 {
257     Node::Read(reader);
258     statement.reset(reader.ReadNode());
259     condition.reset(reader.ReadNode());
260     reader.Read(whileSpan);
261 }
262 
263 RangeForStatementNode::RangeForStatementNode() : Node(NodeType::rangeForStatementNode)
264 {
265 }
266 
267 RangeForStatementNode::RangeForStatementNode(const Span& span_Node* forRangeDeclaration_Node* forRangeInitializer_Node* statement_) :
268     Node(NodeType::rangeForStatementNodespan_)forRangeDeclaration(forRangeDeclaration_)forRangeInitializer(forRangeInitializer_)statement(statement_)
269 {
270 }
271 
272 void RangeForStatementNode::Accept(Visitor& visitor)
273 {
274     visitor.Visit(*this);
275 }
276 
277 void RangeForStatementNode::Write(Writer& writer)
278 {
279     Node::Write(writer);
280     forRangeDeclaration->Write(writer);
281     forRangeInitializer->Write(writer);
282     statement->Write(writer);
283 }
284 
285 void RangeForStatementNode::Read(Reader& reader)
286 {
287     Node::Read(reader);
288     forRangeDeclaration.reset(reader.ReadNode());
289     forRangeInitializer.reset(reader.ReadNode());
290     statement.reset(reader.ReadNode());
291 }
292 
293 ForStatementNode::ForStatementNode() : Node(NodeType::forStatementNode)
294 {
295 }
296 
297 ForStatementNode::ForStatementNode(const Span& span_Node* initS_Node* condition_Node* loopExpr_Node* actionS_) :
298     Node(NodeType::forStatementNodespan_)initS(initS_)condition(condition_)loopExpr(loopExpr_)actionS(actionS_)
299 {
300 }
301 
302 void ForStatementNode::Accept(Visitor& visitor)
303 {
304     visitor.Visit(*this);
305 }
306 
307 void ForStatementNode::Write(Writer& writer)
308 {
309     Node::Write(writer);
310     initS->Write(writer);
311     writer.GetBinaryWriter().Write(condition != nullptr);
312     if (condition)
313     {
314         condition->Write(writer);
315     }
316     writer.GetBinaryWriter().Write(loopExpr != nullptr);
317     if (loopExpr)
318     {
319         loopExpr->Write(writer);
320     }
321     actionS->Write(writer);
322 }
323 
324 void ForStatementNode::Read(Reader& reader)
325 {
326     Node::Read(reader);
327     initS.reset(reader.ReadNode());
328     bool hasCondition = reader.GetBinaryReader().ReadBool();
329     if (hasCondition)
330     {
331         condition.reset(reader.ReadNode());
332     }
333     bool hasLoopExpr = reader.GetBinaryReader().ReadBool();
334     if (hasLoopExpr)
335     {
336         loopExpr.reset(reader.ReadNode());
337     }
338     actionS.reset(reader.ReadNode());
339 }
340 
341 BreakStatementNode::BreakStatementNode() : Node(NodeType::breakStatementNode)
342 {
343 }
344 
345 BreakStatementNode::BreakStatementNode(const Span& span_) : Node(NodeType::breakStatementNodespan_)
346 {
347 }
348 
349 void BreakStatementNode::Accept(Visitor& visitor)
350 {
351     visitor.Visit(*this);
352 }
353 
354 ContinueStatementNode::ContinueStatementNode() : Node(NodeType::continueStatementNode)
355 {
356 }
357 
358 ContinueStatementNode::ContinueStatementNode(const Span& span_) : Node(NodeType::continueStatementNodespan_)
359 {
360 }
361 
362 void ContinueStatementNode::Accept(Visitor& visitor)
363 {
364     visitor.Visit(*this);
365 }
366 
367 ReturnStatementNode::ReturnStatementNode() : Node(NodeType::returnStatementNode)
368 {
369 }
370 
371 ReturnStatementNode::ReturnStatementNode(const Span& span_Node* returnExpr_) : Node(NodeType::returnStatementNodespan_)returnExpr(returnExpr_)
372 {
373 }
374 
375 void ReturnStatementNode::Accept(Visitor& visitor)
376 {
377     visitor.Visit(*this);
378 }
379 
380 void ReturnStatementNode::Write(Writer& writer)
381 {
382     Node::Write(writer);
383     writer.GetBinaryWriter().Write(returnExpr != nullptr);
384     if (returnExpr)
385     {
386         returnExpr->Write(writer);
387     }
388 }
389 
390 void ReturnStatementNode::Read(Reader& reader)
391 {
392     Node::Read(reader);
393     bool hasReturnExpr = reader.GetBinaryReader().ReadBool();
394     if (hasReturnExpr)
395     {
396         returnExpr.reset(reader.ReadNode());
397     }
398 }
399 
400 GotoStatementNode::GotoStatementNode() : Node(NodeType::gotoStatementNode)
401 {
402 }
403 
404 GotoStatementNode::GotoStatementNode(const Span& span_const std::u32string& target_) : Node(NodeType::gotoStatementNodespan_)target(target_)
405 {
406 }
407 
408 void GotoStatementNode::Accept(Visitor& visitor)
409 {
410     visitor.Visit(*this);
411 }
412 
413 void GotoStatementNode::Write(Writer& writer)
414 {
415     Node::Write(writer);
416     writer.GetBinaryWriter().Write(target);
417 }
418 
419 void GotoStatementNode::GotoStatementNode::Read(Reader& reader)
420 {
421     Node::Read(reader);
422     target = reader.GetBinaryReader().ReadUtf32String();
423 }
424 
425 DeclarationStatementNode::DeclarationStatementNode() : UnaryNode(NodeType::declarationStatementNode)
426 {
427 }
428 
429 DeclarationStatementNode::DeclarationStatementNode(const Span& span_Node* declaration_) : UnaryNode(NodeType::declarationStatementNodespan_declaration_)
430 {
431 }
432 
433 void DeclarationStatementNode::Accept(Visitor& visitor)
434 {
435     visitor.Visit(*this);
436 }
437 
438 TryStatementNode::TryStatementNode() : Node(NodeType::tryStatementNode)
439 {
440 }
441 
442 TryStatementNode::TryStatementNode(const Span& span_CompoundStatementNode* tryBlock_) : Node(NodeType::tryStatementNodespan_)tryBlock(tryBlock_)
443 {
444 }
445 
446 void TryStatementNode::Accept(Visitor& visitor)
447 {
448     visitor.Visit(*this);
449 }
450 
451 void TryStatementNode::Write(Writer& writer)
452 {
453     Node::Write(writer);
454     tryBlock->Write(writer);
455     writer.GetBinaryWriter().Write(handlers != nullptr);
456     if (handlers)
457     {
458         handlers->Write(writer);
459     }
460 }
461 
462 void TryStatementNode::Read(Reader& reader)
463 {
464     Node::Read(reader);
465     tryBlock.reset(reader.ReadCompoundStatementNode());
466     bool hasHandlers = reader.GetBinaryReader().ReadBool();
467     if (hasHandlers)
468     {
469         handlers.reset(reader.ReadNode());
470     }
471 }
472 
473 void TryStatementNode::AddHandler(const Span& spanNode* handler)
474 {
475     if (handlers)
476     {
477         handlers.reset(new HandlerSequenceNode(spanhandlers.release()handler));
478     }
479     else
480     {
481         handlers.reset(handler);
482     }
483 }
484 
485 HandlerNode::HandlerNode() : Node(NodeType::handlerNode)
486 {
487 }
488 
489 HandlerNode::HandlerNode(const Span& span_Node* exceptionDeclaration_CompoundStatementNode* catchBlock_) :
490     Node(NodeType::handlerNodespan_)exceptionDeclaration(exceptionDeclaration_)catchBlock(catchBlock_)
491 {
492 }
493 
494 void HandlerNode::Accept(Visitor& visitor)
495 {
496     visitor.Visit(*this);
497 }
498 
499 void HandlerNode::Write(Writer& writer)
500 {
501     Node::Write(writer);
502     exceptionDeclaration->Write(writer);
503     catchBlock->Write(writer);
504 }
505 
506 void HandlerNode::Read(Reader& reader)
507 {
508     Node::Read(reader);
509     exceptionDeclaration.reset(reader.ReadNode());
510     catchBlock.reset(reader.ReadCompoundStatementNode());
511 }
512 
513 HandlerSequenceNode::HandlerSequenceNode() : BinaryNode(NodeType::handlerSequenceNode)
514 {
515 }
516 
517 HandlerSequenceNode::HandlerSequenceNode(const Span& span_Node* left_Node* right_) : BinaryNode(NodeType::handlerSequenceNodespan_left_right_)
518 {
519 }
520 
521 void HandlerSequenceNode::Accept(Visitor& visitor)
522 {
523     visitor.Visit(*this);
524 }
525 
526 CatchAllNode::CatchAllNode() : Node(NodeType::catchAllNode)
527 {
528 }
529 
530 CatchAllNode::CatchAllNode(const Span& span_) : Node(NodeType::catchAllNodespan_)
531 {
532 }
533 
534 void CatchAllNode::Accept(Visitor& visitor)
535 {
536     visitor.Visit(*this);
537 }
538 
539 } } // namespace sngcpp::ast