1 #include "ast/ast_node_type.h"
2 #include "ast/ast_base.h"
3 #include "ast/context.h"
6 using namespace tanlang;
11 ASTNodeType ASTBase::get_node_type()
const {
return _node_type; }
13 void ASTBase::set_node_type(ASTNodeType node_type) { _node_type = node_type; }
15 int ASTBase::get_bp()
const {
return _bp; }
24 str ret = fmt::format(
"{} {}\n", prefix, this->
to_string(
true));
27 size_t n_children = children.size();
28 for (
size_t i = 0; i < n_children; ++i) {
29 auto *c = children[i];
31 ret += c->repr(prefix +
"-");
41 if (include_source_code) {
42 str code = src()->get_source_code(start(), end());
43 ret = fmt::format(
"{}: `{}`", ret, code);
56 #define MAKE_ASTTYPE_NAME_PAIR(t) \
57 { ASTNodeType::t, #t }
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),
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),
92 #undef MAKE_ASTTYPE_NAME_PAIR
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 }
virtual ASTBase * get() const
Get the "actual" this. Used for implementing proxy classes.
static umap< ASTNodeType, str > ASTTypeNames
string representation of ASTNodeType
virtual str to_string(bool include_source_code=false) const
Different from repr, to_string output doesn't include child nodes.
str repr(const str &prefix="-") const
AST tree string representation.
static umap< ASTNodeType, int > OpPrecedence
operator precedence of tokens
virtual vector< ASTBase * > get_children() const
Get a ordered list of child nodes.
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.