Joos1W Compiler Framework
All Classes Functions Typedefs Pages
Stmt.h
1 #pragma once
2 
3 #include <ranges>
4 
5 #include "AstNode.h"
6 #include "Decl.h"
7 #include "diagnostics/Location.h"
8 #include "utils/Utils.h"
9 
10 namespace ast {
11 
12 class VarDecl;
13 
14 class BlockStatement final : public Stmt {
15 public:
16  BlockStatement(BumpAllocator& alloc, array_ref<Stmt*> stmts) noexcept
17  : stmts_{alloc} {
18  utils::move_vector<Stmt*>(stmts, stmts_);
19  }
20 
21  std::ostream& print(std::ostream& os, int indentation = 0) const override;
22  int printDotNode(DotPrinter& dp) const override;
23 
24  utils::Generator<ast::AstNode const*> children() const override {
25  for(auto stmt : stmts_) co_yield stmt;
26  }
27  utils::Generator<const Expr*> exprs() const override { co_yield nullptr; }
28  auto stmts() const { return std::views::all(stmts_); }
29 
30 private:
31  pmr_vector<Stmt*> stmts_;
32 };
33 
34 class DeclStmt final : public Stmt {
35 public:
36  DeclStmt(VarDecl* decl) : decl_{decl} {}
37 
38  std::ostream& print(std::ostream& os, int indentation = 0) const override;
39  int printDotNode(DotPrinter& dp) const override;
40 
41  auto decl() const { return decl_; }
42  utils::Generator<ast::AstNode const*> children() const override {
43  co_yield decl_;
44  }
45  utils::Generator<const Expr*> exprs() const override { co_yield nullptr; }
46 
47 private:
48  VarDecl* decl_;
49 };
50 
51 class ExprStmt final : public Stmt {
52 public:
53  ExprStmt(Expr* expr) : expr_{expr} {}
54 
55  std::ostream& print(std::ostream& os, int indentation = 0) const override;
56  int printDotNode(DotPrinter& dp) const override;
57 
58  auto expr() const { return expr_; }
59  utils::Generator<const Expr*> exprs() const override { co_yield expr_; }
60 
61 private:
62  Expr* expr_;
63 };
64 
65 class IfStmt final : public Stmt {
66 public:
67  IfStmt(Expr* condition, Stmt* thenStmt, Stmt* elseStmt)
68  : condition_{condition}, thenStmt_{thenStmt}, elseStmt_{elseStmt} {}
69 
70  std::ostream& print(std::ostream& os, int indentation = 0) const override;
71  int printDotNode(DotPrinter& dp) const override;
72 
73  auto condition() const { return condition_; }
74  auto thenStmt() const { return thenStmt_; }
75  auto elseStmt() const { return elseStmt_; }
76  utils::Generator<const Expr*> exprs() const override { co_yield condition_; }
77  utils::Generator<ast::AstNode const*> children() const override {
78  co_yield thenStmt_;
79  if(elseStmt_) co_yield elseStmt_;
80  }
81 
82 private:
83  Expr* condition_;
84  Stmt* thenStmt_;
85  Stmt* elseStmt_;
86 };
87 
88 class WhileStmt final : public Stmt {
89 public:
90  WhileStmt(Expr* condition, Stmt* body) : condition_{condition}, body_{body} {}
91  std::ostream& print(std::ostream& os, int indentation = 0) const override;
92  int printDotNode(DotPrinter& dp) const override;
93  auto condition() const { return condition_; }
94  auto body() const { return body_; }
95  utils::Generator<const Expr*> exprs() const override { co_yield condition_; }
96  utils::Generator<ast::AstNode const*> children() const override {
97  co_yield body_;
98  }
99 
100 private:
101  Expr* condition_;
102  Stmt* body_;
103 };
104 
105 class ForStmt final : public Stmt {
106 public:
107  ForStmt(Stmt* init, Expr* condition, Stmt* update, Stmt* body)
108  : init_{init}, condition_{condition}, update_{update}, body_{body} {}
109 
110  std::ostream& print(std::ostream& os, int indentation = 0) const override;
111  int printDotNode(DotPrinter& dp) const override;
112 
113  auto init() const { return init_; }
114  auto condition() const { return condition_; }
115  auto update() const { return update_; }
116  auto body() const { return body_; }
117  utils::Generator<const Expr*> exprs() const override { co_yield condition_; }
118  utils::Generator<ast::AstNode const*> children() const override {
119  co_yield init_;
120  co_yield update_;
121  co_yield body_;
122  }
123 
124 private:
125  Stmt* init_;
126  Expr* condition_;
127  Stmt* update_;
128  Stmt* body_;
129 };
130 
131 class ReturnStmt final : public Stmt {
132 public:
133  ReturnStmt(SourceRange loc, Expr* expr) : expr_{expr}, loc_{loc} {}
134 
135  std::ostream& print(std::ostream& os, int indentation = 0) const override;
136  int printDotNode(DotPrinter& dp) const override;
137 
138  auto expr() const { return expr_; }
139  utils::Generator<const Expr*> exprs() const override { co_yield expr_; }
140  SourceRange location() const {
141  return loc_;
142  }
143 
144 private:
145  Expr* expr_;
146  SourceRange loc_;
147 };
148 
149 class NullStmt final : public Stmt {
150 public:
151  std::ostream& print(std::ostream& os, int indentation = 0) const override;
152  int printDotNode(DotPrinter& dp) const override;
153 
154  utils::Generator<const Expr*> exprs() const override { co_yield nullptr; }
155 };
156 
157 } // namespace ast