-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCParseTree.cpp
105 lines (79 loc) · 2.1 KB
/
CParseTree.cpp
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
/*
* CParseTree.cpp
* HyperCompiler
*
* Created by Uli Kusterer on 10.05.07.
* Copyright 2007 M. Uli Kusterer. All rights reserved.
*
*/
#include "CParseTree.h"
#include "CNodeTransformation.h"
#include "CFunctionDefinitionNode.h"
#include <string>
#include <assert.h>
namespace Carlson
{
CParseTree::CParseTree()
: mUniqueIdentifierSeed(0)
{
}
CParseTree::~CParseTree()
{
std::deque<CNode*>::iterator itty;
for( itty = mNodes.begin(); itty != mNodes.end(); itty++ )
{
delete *itty;
*itty = NULL;
}
}
void CParseTree::AddFunctionDefinitionNode( CFunctionDefinitionNode* inNode )
{
AddNode( inNode );
mFunctionNodes[inNode->GetName()] = inNode;
}
void CParseTree::Simplify()
{
std::deque<CNode*>::iterator itty;
for( itty = mNodes.begin(); itty != mNodes.end(); itty++ )
{
CNode * originalNode = *itty;
originalNode->Simplify(); // Give subnodes a chance to apply transformations first. Might expose simpler sub-nodes we can then simplify.
CNode* newNode = CNodeTransformationBase::Apply( originalNode ); // Returns either originalNode, or a totally new object, in which case we delete the old one.
if( newNode != originalNode )
*itty = newNode;
}
}
void CParseTree::Visit( std::function<void(CNode*)> visitorBlock )
{
for( auto currNode : mNodes )
{
currNode->Visit(visitorBlock);
}
}
void CParseTree::GenerateCode( CCodeBlock* inCodeBlock )
{
std::deque<CNode*>::iterator itty;
for( itty = mNodes.begin(); itty != mNodes.end(); itty++ )
{
(*itty)->GenerateCode( inCodeBlock );
}
}
void CParseTree::DebugPrint( std::ostream& destStream, size_t indentLevel )
{
INDENT_PREPARE(indentLevel);
std::deque<CNode*>::iterator itty;
for( itty = mNodes.begin(); itty != mNodes.end(); itty++ )
{
(*itty)->DebugPrint( destStream, indentLevel );
}
}
std::string CParseTree::GetUniqueIdentifierBasedOn( std::string inBaseIdentifier )
{
std::string outIdent( inBaseIdentifier );
assert(ULLONG_MAX <= 18446744073709551615ULL);
char numStr[20+1] = {0};
snprintf(numStr, 20, "%llu", mUniqueIdentifierSeed++);
outIdent.append(numStr);
return outIdent;
}
} // namespace Carlson