tan  0.0.1
stmt.h
1 #ifndef __TAN_SRC_AST_STMT_H__
2 #define __TAN_SRC_AST_STMT_H__
3 #include "base.h"
4 #include "ast_base.h"
5 #include "fwd.h"
6 
7 namespace llvm {
8 class BasicBlock;
9 }
10 
11 namespace tanlang {
12 
13 class Stmt : public ASTBase {
14 public:
15  [[nodiscard]] vector<ASTBase *> get_children() const override;
16 
17  bool is_stmt() const override { return true; }
18  bool is_expr() const override { return false; }
19 
20 protected:
21  Stmt(ASTNodeType type, TokenizedSourceFile *src);
22 };
23 
24 class CompoundStmt : public Stmt {
25 protected:
26  explicit CompoundStmt(TokenizedSourceFile *src);
27 
28 public:
30 
31  void set_child_at(size_t idx, ASTBase *node);
32  void append_child(ASTBase *node);
33  void clear_children();
34  [[nodiscard]] size_t get_children_size() const;
35  [[nodiscard]] vector<ASTBase *> get_children() const override;
36  vector<ASTBase *> &get_children();
37 
38  str terminal_token() const override { return "}"; }
39 
40 protected:
41  str to_string(bool = false) const override { return Stmt::to_string(false); }
42 
43 protected:
44  vector<ASTBase *> _children{};
45 };
46 
47 class Program : public CompoundStmt {
48 protected:
49  explicit Program(TokenizedSourceFile *src);
50 
51 public:
52  static Program *Create(TokenizedSourceFile *src);
53 };
54 
55 class Return : public Stmt {
56 protected:
57  explicit Return(TokenizedSourceFile *src);
58 
59 public:
60  static Return *Create(TokenizedSourceFile *src);
61 
62  void set_rhs(Expr *rhs);
63  [[nodiscard]] Expr *get_rhs() const;
64 
65  [[nodiscard]] vector<ASTBase *> get_children() const override;
66 
67 private:
68  Expr *_rhs = nullptr;
69 };
70 
71 class Import : public Stmt {
72 protected:
73  explicit Import(TokenizedSourceFile *src);
74 
75 public:
76  static Import *Create(TokenizedSourceFile *src);
77 
78  void set_name(const str &s);
79  [[nodiscard]] const str &get_name() const;
80 
81 public:
82  vector<FunctionDecl *> _imported_funcs{};
83  vector<TypeDecl *> _imported_types{};
84 
85 private:
86  str _name;
87 };
88 
89 class BreakContinue : public Stmt {
90 protected:
91  BreakContinue(ASTNodeType type, TokenizedSourceFile *src);
92 
93 public:
94  [[nodiscard]] Loop *get_parent_loop() const;
95  void set_parent_loop(Loop *parent_loop);
96 
97 private:
98  Loop *_parent_loop = nullptr;
99 };
100 
101 class Break : public BreakContinue {
102 protected:
103  explicit Break(TokenizedSourceFile *src);
104 
105 public:
106  static Break *Create(TokenizedSourceFile *src);
107 };
108 
109 class Continue : public BreakContinue {
110 protected:
111  explicit Continue(TokenizedSourceFile *src);
112 
113 public:
114  static Continue *Create(TokenizedSourceFile *src);
115 };
116 
117 enum class ASTLoopType { FOR, WHILE };
118 
119 class Loop final : public Stmt {
120 protected:
121  explicit Loop(TokenizedSourceFile *src);
122 
123 public:
124  static Loop *Create(TokenizedSourceFile *src);
125 
126  [[nodiscard]] vector<ASTBase *> get_children() const override;
127 
128  str terminal_token() const override { return "}"; }
129 
130 public:
131  ASTLoopType _loop_type = ASTLoopType::WHILE;
132  llvm::BasicBlock *_loop_start = nullptr;
133  llvm::BasicBlock *_loop_end = nullptr;
134 
135  Expr *_initialization = nullptr;
136  Expr *_predicate = nullptr;
137  Stmt *_body = nullptr;
138  Expr *_iteration = nullptr;
139 };
140 
141 /**
142  * \brief Represent if-[else] or if-elif-[else] statements
143  */
144 class If : public Stmt {
145 protected:
146  explicit If(TokenizedSourceFile *src);
147 
148 public:
149  static If *Create(TokenizedSourceFile *src);
150 
151 public:
152  str terminal_token() const override { return "}"; }
153 
154 public:
155  void add_if_then_branch(Expr *pred, Stmt *branch);
156  void add_else_branch(Stmt *branch);
157 
158  /**
159  * \note Return value can be a nullptr if the branch is an "else"
160  */
161  [[nodiscard]] Expr *get_predicate(size_t i) const;
162 
163  void set_predicate(size_t i, Expr *expr);
164 
165  [[nodiscard]] Stmt *get_branch(size_t i) const;
166  [[nodiscard]] size_t get_num_branches() const;
167 
168  [[nodiscard]] vector<ASTBase *> get_children() const override;
169 
170 private:
171  /// \note The last element can be a nullptr if the last branch is an "else"
172  vector<Expr *> _predicates{};
173  vector<Stmt *> _branches{};
174 };
175 
176 class PackageDecl : public Stmt {
177 protected:
178  explicit PackageDecl(TokenizedSourceFile *src);
179 
180 public:
181  static PackageDecl *Create(TokenizedSourceFile *src);
182 
183 public:
184  str get_name() const;
185  void set_name(const str &name);
186 
187 private:
188  str _name = "";
189 };
190 
191 } // namespace tanlang
192 
193 #endif //__TAN_SRC_AST_STMT_H__
virtual str to_string(bool include_source_code=false) const
Different from repr, to_string output doesn't include child nodes.
Definition: ast_base.cpp:38
BreakContinue(ASTNodeType type, TokenizedSourceFile *src)
Definition: stmt.cpp:63
static CompoundStmt * Create(TokenizedSourceFile *src)
Definition: stmt.cpp:14
str terminal_token() const override
Which terminal token is expected immediately after this node.
Definition: stmt.h:38
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: stmt.cpp:31
str to_string(bool=false) const override
Different from repr, to_string output doesn't include child nodes.
Definition: stmt.h:41
Represent if-[else] or if-elif-[else] statements.
Definition: stmt.h:144
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: stmt.cpp:121
str terminal_token() const override
Which terminal token is expected immediately after this node.
Definition: stmt.h:152
Expr * get_predicate(size_t i) const
Definition: stmt.cpp:104
If(TokenizedSourceFile *src)
Definition: stmt.cpp:89
static Import * Create(TokenizedSourceFile *src)
Definition: stmt.cpp:53
static Loop * Create(TokenizedSourceFile *src)
Definition: stmt.cpp:81
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: stmt.cpp:85
str terminal_token() const override
Which terminal token is expected immediately after this node.
Definition: stmt.h:128
static Program * Create(TokenizedSourceFile *src)
Definition: stmt.cpp:35
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: stmt.cpp:49
Return(TokenizedSourceFile *src)
Definition: stmt.cpp:43
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: stmt.cpp:10
Stmt(ASTNodeType type, TokenizedSourceFile *src)
Definition: stmt.cpp:8
Different from SourceFile, TokenizedSourceFile manages the tokenized text of a source file.