tan  0.0.1
ast_base.cpp
1 #include "ast/ast_node_type.h"
2 #include "ast/ast_base.h"
3 #include "ast/context.h"
4 #include <iostream>
5 
6 using namespace tanlang;
7 
8 ASTBase::ASTBase(ASTNodeType node_type, TokenizedSourceFile *src, int bp)
9  : SourceTraceable(src), _node_type(node_type), _bp(bp) {}
10 
11 ASTNodeType ASTBase::get_node_type() const { return _node_type; }
12 
13 void ASTBase::set_node_type(ASTNodeType node_type) { _node_type = node_type; }
14 
15 int ASTBase::get_bp() const { return _bp; }
16 
17 Context *ASTBase::ctx() {
18  if (!_ctx)
19  _ctx = new Context((ASTBase *)this); // context <-> AST node mapping
20  return _ctx;
21 }
22 
23 str ASTBase::repr(const str &prefix) const {
24  str ret = fmt::format("{} {}\n", prefix, this->to_string(true));
25 
26  vector<ASTBase *> children = get_children();
27  size_t n_children = children.size();
28  for (size_t i = 0; i < n_children; ++i) {
29  auto *c = children[i];
30  if (c) {
31  ret += c->repr(prefix + "-");
32  }
33  }
34 
35  return ret;
36 }
37 
38 str ASTBase::to_string(bool include_source_code) const {
39  str ret = ASTTypeNames[_node_type];
40 
41  if (include_source_code) {
42  str code = src()->get_source_code(start(), end());
43  ret = fmt::format("{}: `{}`", ret, code);
44  }
45 
46  return ret;
47 }
48 
49 ASTBase *ASTBase::get() const { return const_cast<ASTBase *>(this); }
50 
51 vector<ASTBase *> ASTBase::get_children() const {
52  TAN_ASSERT(false);
53  return {};
54 }
55 
56 #define MAKE_ASTTYPE_NAME_PAIR(t) \
57  { ASTNodeType::t, #t }
58 
59 umap<ASTNodeType, str> ASTBase::ASTTypeNames = {
60  MAKE_ASTTYPE_NAME_PAIR(PROGRAM),
61  MAKE_ASTTYPE_NAME_PAIR(FUNC_CALL),
62  MAKE_ASTTYPE_NAME_PAIR(FUNC_DECL),
63  MAKE_ASTTYPE_NAME_PAIR(ARG_DECL),
64  MAKE_ASTTYPE_NAME_PAIR(VAR_DECL),
65  MAKE_ASTTYPE_NAME_PAIR(STRUCT_DECL),
66  MAKE_ASTTYPE_NAME_PAIR(COMPOUND_STATEMENT),
67  MAKE_ASTTYPE_NAME_PAIR(BOP),
68  MAKE_ASTTYPE_NAME_PAIR(UOP),
69  MAKE_ASTTYPE_NAME_PAIR(BOP_OR_UOP),
70  MAKE_ASTTYPE_NAME_PAIR(ASSIGN),
71  MAKE_ASTTYPE_NAME_PAIR(CAST),
72  MAKE_ASTTYPE_NAME_PAIR(ID),
73  MAKE_ASTTYPE_NAME_PAIR(LOOP),
74  MAKE_ASTTYPE_NAME_PAIR(CONTINUE),
75  MAKE_ASTTYPE_NAME_PAIR(BREAK),
76  MAKE_ASTTYPE_NAME_PAIR(PARENTHESIS),
77  MAKE_ASTTYPE_NAME_PAIR(RET),
78  MAKE_ASTTYPE_NAME_PAIR(IF),
79  MAKE_ASTTYPE_NAME_PAIR(IMPORT),
80  MAKE_ASTTYPE_NAME_PAIR(VAR_REF),
81  MAKE_ASTTYPE_NAME_PAIR(INTRINSIC),
82 
83  MAKE_ASTTYPE_NAME_PAIR(BOOL_LITERAL),
84  MAKE_ASTTYPE_NAME_PAIR(INTEGER_LITERAL),
85  MAKE_ASTTYPE_NAME_PAIR(FLOAT_LITERAL),
86  MAKE_ASTTYPE_NAME_PAIR(CHAR_LITERAL),
87  MAKE_ASTTYPE_NAME_PAIR(STRING_LITERAL),
88  MAKE_ASTTYPE_NAME_PAIR(ARRAY_LITERAL),
89  MAKE_ASTTYPE_NAME_PAIR(NULLPTR_LITERAL),
90 };
91 
92 #undef MAKE_ASTTYPE_NAME_PAIR
93 
94 umap<ASTNodeType, int> ASTBase::OpPrecedence = {
95  {ASTNodeType::PROGRAM, PREC_LOWEST },
96  {ASTNodeType::COMPOUND_STATEMENT, PREC_LOWEST },
97  {ASTNodeType::PARENTHESIS, PREC_CALL },
98  {ASTNodeType::RET, PREC_LOWEST },
99  {ASTNodeType::IF, PREC_LOWEST },
100  {ASTNodeType::STRING_LITERAL, PREC_LITERAL},
101  {ASTNodeType::CAST, PREC_CAST },
102  {ASTNodeType::ASSIGN, PREC_ASSIGN }
103 };
virtual ASTBase * get() const
Get the "actual" this. Used for implementing proxy classes.
Definition: ast_base.cpp:49
static umap< ASTNodeType, str > ASTTypeNames
string representation of ASTNodeType
Definition: ast_base.h:16
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
str repr(const str &prefix="-") const
AST tree string representation.
Definition: ast_base.cpp:23
static umap< ASTNodeType, int > OpPrecedence
operator precedence of tokens
Definition: ast_base.h:19
virtual vector< ASTBase * > get_children() const
Get a ordered list of child nodes.
Definition: ast_base.cpp:51
Represents the nodes that can be traced back to tokens in the source file.
Different from SourceFile, TokenizedSourceFile manages the tokenized text of a source file.