aboutsummaryrefslogtreecommitdiff
path: root/src/ast.cpp
blob: 03ed4b62ce6ad01a979ec456d7df63c9f822314b (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
#include "ast.h"

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

Ast::Ast()
{
}

Ast::~Ast()
{
}

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

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

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

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

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

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

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

void Ast::addNode( AstNode::Type eType, const char *sVal )
{
	addNode( new AstLeaf( 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;
}