tan  0.0.1
ast_base.h
1 #ifndef __TAN_SRC_AST_AST_BASE_H__
2 #define __TAN_SRC_AST_AST_BASE_H__
3 #include "base.h"
4 #include "source_traceable.h"
5 #include "precedence.h"
6 #include "ast_node_type.h"
7 #include <typeinfo>
8 
9 namespace tanlang {
10 
11 class Context;
12 
13 class ASTBase : public SourceTraceable {
14 public:
15  /// string representation of ASTNodeType
16  static umap<ASTNodeType, str> ASTTypeNames;
17 
18  /// operator precedence of tokens
19  static umap<ASTNodeType, int> OpPrecedence;
20 
21 public:
22  ASTBase() = delete;
23  ASTBase(ASTNodeType node_type, TokenizedSourceFile *src, int bp);
24  virtual ~ASTBase() = default;
25 
26 public:
27  [[nodiscard]] ASTNodeType get_node_type() const;
28  void set_node_type(ASTNodeType node_type);
29  [[nodiscard]] int get_bp() const;
30  [[nodiscard]] Context *ctx();
31 
32  /**
33  * \brief Get a ordered list of child nodes
34  */
35  [[nodiscard]] virtual vector<ASTBase *> get_children() const;
36 
37  /// AST tree string representation
38  str repr(const str &prefix = "-") const;
39 
40 public:
41  virtual bool is_stmt() const = 0;
42  virtual bool is_expr() const = 0;
43 
44  /// Which terminal token is expected immediately after this node
45  virtual str terminal_token() const { return ";"; }
46 
47 protected:
48  /// Different from repr, to_string output doesn't include child nodes
49  [[nodiscard]] virtual str to_string(bool include_source_code = false) const;
50 
51  /**
52  * \brief Get the "actual" this. Used for implementing proxy classes.
53  */
54  [[nodiscard]] virtual ASTBase *get() const;
55 
56 private:
57  ASTNodeType _node_type;
58  int _bp = 0; /// binding power
59  Context *_ctx = nullptr;
60 };
61 
62 } // namespace tanlang
63 
64 #endif //__TAN_SRC_AST_AST_BASE_H__
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 str terminal_token() const
Which terminal token is expected immediately after this node.
Definition: ast_base.h:45
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.