aboutsummaryrefslogtreecommitdiff
path: root/src/ast.cpp
blob: 6b213ea2814256267681fa5e05390d04e00bf3a6 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "ast.h"

#include "astleaf.h"
#include "astbranch.h"

#include "build.tab.h"

Ast::Ast()
{
}

Ast::~Ast()
{
}

void Ast::addNode( YYLTYPE &loc, AstNode::Type eType )
{
	switch( eType&AstNode::typeClassMask )
	{
		case AstNode::typeBranch:
			{
				AstBranch *pNode = new AstBranch( loc, eType );
				addNode( pNode );
				sBranch.push( pNode );
			}
			break;

		case AstNode::typeLeaf:
			{
				AstLeaf *pNode = new AstLeaf( loc, eType );
				addNode( pNode );
			}
			break;

		default:
			throw Bu::ExceptionBase("You got it wrong.");
			break;
	}
}

void Ast::addNode( YYLTYPE &loc, AstNode::Type eType, int iVal )
{
	addNode( new AstLeaf( loc, eType, iVal ) );
}

void Ast::addNode( YYLTYPE &loc, AstNode::Type eType, float fVal )
{
	addNode( new AstLeaf( loc, eType, fVal ) );
}

void Ast::addNode( YYLTYPE &loc, AstNode::Type eType, bool bVal )
{
	addNode( new AstLeaf( loc, eType, bVal ) );
}

void Ast::addNode( YYLTYPE &loc, AstNode::Type eType, const Bu::FString &sVal )
{
	addNode( new AstLeaf( loc, eType, sVal ) );
}

void Ast::addNode( YYLTYPE &loc, AstNode::Type eType, const char *sVal )
{
	addNode( new AstLeaf( loc, eType, sVal ) );
}

void Ast::addNode( AstNode::Type eType )
{
	YYLTYPE none = {-1, -1, -1, -1};
	addNode( none, eType );
}

void Ast::addNode( AstNode::Type eType, int iVal )
{
	YYLTYPE none = {-1, -1, -1, -1};
	addNode( none, eType, iVal );
}

void Ast::addNode( AstNode::Type eType, float fVal )
{
	YYLTYPE none = {-1, -1, -1, -1};
	addNode( none, eType, fVal );
}

void Ast::addNode( AstNode::Type eType, bool bVal )
{
	YYLTYPE none = {-1, -1, -1, -1};
	addNode( none, eType, bVal );
}

void Ast::addNode( AstNode::Type eType, const Bu::FString &sVal )
{
	YYLTYPE none = {-1, -1, -1, -1};
	addNode( none, eType, sVal );
}

void Ast::addNode( AstNode::Type eType, const char *sVal )
{
	YYLTYPE none = {-1, -1, -1, -1};
	addNode( none, eType, sVal );
}

void Ast::addNode( AstNode *pNode )
{
	if( sBranch.isEmpty() )
		lNode.append( pNode );
	else
		sBranch.peek()->addNode( pNode );
}

void Ast::openBranch()
{
	sBranch.peek()->addBranch();
}

void Ast::closeNode()
{
	sBranch.pop();
}

Ast::NodeList::const_iterator Ast::getNodeBegin() const
{
	return lNode.begin();
}

Bu::Formatter &operator<<( Bu::Formatter &f, const Ast &a )
{
	f << "Abstract Syntax Tree:";
	f.incIndent();
	f << f.nl;
	for( Ast::NodeList::const_iterator i = a.getNodeBegin(); i; i++ )
		f << **i << f.nl;
	f << f.nl;
	f.decIndent();
	return f;
}