-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.h
42 lines (32 loc) · 912 Bytes
/
ast.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef AST_H
#define AST_H
#include <string>
#include <iostream>
namespace AST {
class ASTVisitor;
class ASTNode {
public:
ASTNode() : parent_( nullptr ) {}
virtual ~ASTNode() {}
virtual void Print( std::ostream & ) = 0;
inline std::string DebugIndent( unsigned int tabs = 0 ) {
return std::string( ( GetLevel() + tabs ) * 3, ' ' );
}
friend std::ostream & operator<<( std::ostream &out, ASTNode &node ) {
node.Print( out );
return out;
}
virtual void Accept( ASTVisitor * ) = 0;
inline ASTNode *GetParent() { return parent_; }
inline void SetParent( ASTNode *parent ) { parent_ = parent; }
protected:
virtual unsigned int GetWidth() { return 1; }
inline unsigned int GetLevel() {
return ( GetParent() == nullptr ? 0 :
GetParent()->GetLevel() + GetParent()->GetWidth() );
}
private:
ASTNode *parent_;
};
}
#endif