-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCCodeBlockNode.cpp
More file actions
138 lines (105 loc) · 3.31 KB
/
CCodeBlockNode.cpp
File metadata and controls
138 lines (105 loc) · 3.31 KB
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
137
138
/*
* CCodeBlockNode.cpp
* HyperCompiler
*
* Created by Uli Kusterer on 10.05.07.
* Copyright 2007 M. Uli Kusterer. All rights reserved.
*
*/
#include "CCodeBlockNode.h"
#include "CParser.h"
#include "CNodeTransformation.h"
namespace Carlson
{
bool CCodeBlockNodeBase::LocalVariableExists( const std::string& inStr )
{
std::map<std::string,CVariableEntry>& locals = GetLocals();
return locals.find( inStr ) != locals.end();
}
void CCodeBlockNodeBase::DebugPrint( std::ostream& destStream, size_t indentLevel )
{
INDENT_PREPARE(indentLevel);
destStream << indentChars << "Code Block" << std::endl;
DebugPrintInner( destStream, indentLevel );
}
void CCodeBlockNodeBase::DebugPrintInner( std::ostream& destStream, size_t indentLevel )
{
INDENT_PREPARE(indentLevel);
destStream << indentChars << "{" << std::endl;
std::vector<CNode*>::iterator itty;
for( itty = mCommands.begin(); itty != mCommands.end(); itty++ )
{
(*itty)->DebugPrint( destStream, indentLevel +1 );
}
destStream << indentChars << "}" << std::endl;
}
void CCodeBlockNodeBase::Simplify()
{
CNode::Simplify();
std::vector<CNode*>::iterator itty;
for( itty = mCommands.begin(); itty != mCommands.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 CCodeBlockNodeBase::Visit( std::function<void(CNode*)> visitorBlock )
{
for( auto currCommand : mCommands )
currCommand->Visit( visitorBlock );
CNode::Visit( visitorBlock );
}
void CCodeBlockNodeBase::GenerateCode( CCodeBlock* inCodeBlock )
{
std::vector<CNode*>::iterator itty;
for( itty = mCommands.begin(); itty != mCommands.end(); itty++ )
{
(*itty)->GenerateCode( inCodeBlock );
}
}
CCodeBlockNodeBase::~CCodeBlockNodeBase()
{
std::vector<CNode*>::iterator itty;
for( itty = mCommands.begin(); itty != mCommands.end(); itty++ )
{
delete *itty;
*itty = NULL;
}
}
void CCodeBlockNode::AddLocalVar( const std::string& inName, const std::string& inUserName,
TVariantType theType, bool initWithName,
bool isParam, bool isGlobal,
bool dontDispose )
{
CVariableEntry newEntry( inUserName, theType, initWithName,
isParam, isGlobal || GetAllVarsAreGlobals(), dontDispose );
std::map<std::string,CVariableEntry>::iterator foundVariable = (*mLocals).find( inName );
if( foundVariable == (*mLocals).end() )
(*mLocals)[inName] = newEntry;
if( isGlobal || GetAllVarsAreGlobals() )
{
foundVariable = (*mGlobals).find( inName );
if( foundVariable == (*mGlobals).end() )
(*mGlobals)[inName] = newEntry;
}
}
int16_t CCodeBlockNode::GetBPRelativeOffsetForLocalVar( const std::string& inName )
{
std::map<std::string,CVariableEntry>::iterator foundVariable = (*mLocals).find( inName );
if( foundVariable != (*mLocals).end() )
{
int16_t bpRelOffs = foundVariable->second.mBPRelativeOffset;
if( bpRelOffs == INT16_MAX)
{
bpRelOffs = (*mLocalVariableCount)++;
foundVariable->second.mBPRelativeOffset = bpRelOffs;
}
return bpRelOffs;
}
else
return INT16_MAX;
}
} // namespace Carlson