tan  0.0.1
stmt.cpp
1 #include "ast/stmt.h"
2 #include <algorithm>
3 
4 using namespace tanlang;
5 
6 /// \section Stmt
7 
8 Stmt::Stmt(ASTNodeType type, TokenizedSourceFile *src) : ASTBase(type, src, PREC_LOWEST) {}
9 
10 vector<ASTBase *> Stmt::get_children() const { return {}; }
11 
12 /// \section Compound statement
13 
15 
16 CompoundStmt::CompoundStmt(TokenizedSourceFile *src) : Stmt(ASTNodeType::COMPOUND_STATEMENT, src) {}
17 
18 void CompoundStmt::set_child_at(size_t idx, ASTBase *node) {
19  TAN_ASSERT(_children.size() > idx);
20  _children[idx] = node;
21 }
22 
23 void CompoundStmt::append_child(ASTBase *node) { _children.push_back(node); }
24 
25 void CompoundStmt::clear_children() { _children.clear(); }
26 
27 size_t CompoundStmt::get_children_size() const { return _children.size(); }
28 
29 vector<ASTBase *> &CompoundStmt::get_children() { return _children; }
30 
31 vector<ASTBase *> CompoundStmt::get_children() const { return _children; }
32 
33 /// \section Program
34 
36 
37 Program::Program(TokenizedSourceFile *src) : CompoundStmt(src) { set_node_type((ASTNodeType::PROGRAM)); }
38 
39 Return *Return::Create(TokenizedSourceFile *src) { return new Return(src); }
40 
41 /// \section Return
42 
43 Return::Return(TokenizedSourceFile *src) : Stmt(ASTNodeType::RET, src) {}
44 
45 void Return::set_rhs(Expr *rhs) { _rhs = rhs; }
46 
47 Expr *Return::get_rhs() const { return _rhs; }
48 
49 vector<ASTBase *> Return::get_children() const { return {(ASTBase *)_rhs}; }
50 
51 /// \section Import
52 
53 Import *Import::Create(TokenizedSourceFile *src) { return new Import(src); }
54 
55 Import::Import(TokenizedSourceFile *src) : Stmt(ASTNodeType::IMPORT, src) {}
56 
57 void Import::set_name(const str &s) { _name = s; }
58 
59 const str &Import::get_name() const { return _name; }
60 
61 /// \section Break or continue statement
62 
63 BreakContinue::BreakContinue(ASTNodeType type, TokenizedSourceFile *src) : Stmt(type, src) {
64  TAN_ASSERT(type == ASTNodeType::BREAK || type == ASTNodeType::CONTINUE);
65 }
66 
67 Loop *BreakContinue::get_parent_loop() const { return _parent_loop; }
68 
69 void BreakContinue::set_parent_loop(Loop *parent_loop) { _parent_loop = parent_loop; }
70 
71 Break *Break::Create(TokenizedSourceFile *src) { return new Break(src); }
72 
73 Break::Break(TokenizedSourceFile *src) : BreakContinue(ASTNodeType::BREAK, src) {}
74 
75 Continue *Continue::Create(TokenizedSourceFile *src) { return new Continue(src); }
76 
77 Continue::Continue(TokenizedSourceFile *src) : BreakContinue(ASTNodeType::CONTINUE, src) {}
78 
79 /// \section Loop
80 
81 Loop *Loop::Create(TokenizedSourceFile *src) { return new Loop(src); }
82 
83 Loop::Loop(TokenizedSourceFile *src) : Stmt(ASTNodeType::LOOP, src) {}
84 
85 vector<ASTBase *> Loop::get_children() const { return {(ASTBase *)_predicate, _body}; }
86 
87 /// \section If-else
88 
89 If::If(TokenizedSourceFile *src) : Stmt(ASTNodeType::IF, src) {}
90 
91 If *If::Create(TokenizedSourceFile *src) { return new If(src); }
92 
93 void If::add_if_then_branch(Expr *pred, Stmt *branch) {
94  TAN_ASSERT(pred);
95  _predicates.push_back(pred);
96  _branches.push_back(branch);
97 }
98 
99 void If::add_else_branch(Stmt *branch) {
100  _predicates.push_back(nullptr);
101  _branches.push_back(branch);
102 }
103 
104 Expr *If::get_predicate(size_t i) const {
105  TAN_ASSERT(i < _predicates.size());
106  return _predicates[i];
107 }
108 
109 void If::set_predicate(size_t i, Expr *expr) {
110  TAN_ASSERT(i < _predicates.size());
111  _predicates[i] = expr;
112 }
113 
114 Stmt *If::get_branch(size_t i) const {
115  TAN_ASSERT(i < _branches.size());
116  return _branches[i];
117 }
118 
119 size_t If::get_num_branches() const { return _branches.size(); }
120 
121 vector<ASTBase *> If::get_children() const {
122  vector<ASTBase *> ret = {};
123  std::for_each(_predicates.begin(), _predicates.end(), [&](Expr *e) { ret.push_back((ASTBase *)e); });
124  std::for_each(_branches.begin(), _branches.end(), [&](Stmt *e) { ret.push_back((ASTBase *)e); });
125  return ret;
126 }
127 
128 PackageDecl::PackageDecl(TokenizedSourceFile *src) : Stmt(ASTNodeType::PACKAGE_DECL, src) {}
129 
130 PackageDecl *PackageDecl::Create(TokenizedSourceFile *src) { return new PackageDecl(src); }
131 
132 str PackageDecl::get_name() const { return _name; }
133 
134 void PackageDecl::set_name(const str &name) { _name = name; }
BreakContinue(ASTNodeType type, TokenizedSourceFile *src)
Definition: stmt.cpp:63
static CompoundStmt * Create(TokenizedSourceFile *src)
Definition: stmt.cpp:14
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: stmt.cpp:31
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
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
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.