-
Notifications
You must be signed in to change notification settings - Fork 2
/
CValueNode.h
289 lines (207 loc) · 9.37 KB
/
CValueNode.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* CValueNode.h
* HyperCompiler
*
* Created by Uli Kusterer on 10.05.07.
* Copyright 2007 M. Uli Kusterer. All rights reserved.
*
*/
#pragma once
// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------
#include "CNode.h"
#include <math.h>
#include "CForgeExceptions.h"
#include "LEOValue.h"
#include <vector>
extern "C"
{
#include "LEOStringUtilities.h"
}
namespace Carlson
{
class CCodeBlockNodeBase;
// -----------------------------------------------------------------------------
// Classes:
// -----------------------------------------------------------------------------
class CValueNode : public CNode
{
public:
explicit CValueNode( CParseTree* inTree, size_t inLineNum ) : CNode(inTree), mLineNum(inLineNum) {};
virtual ~CValueNode() {};
virtual size_t GetLineNum() { return mLineNum; };
virtual void SetLineNum( size_t inLineNum ) { mLineNum = inLineNum; };
virtual void Visit( std::function<void(CNode*)> visitorBlock ) { visitorBlock(this); };
virtual void GenerateCode( CCodeBlock* inCodeBlock ); // Generate the actual bytecode so it leaves the result on the stack.
virtual bool IsConstant() { return false; };
virtual CValueNode* Copy() { return NULL; };
// If IsConstant() gives TRUE, you can call the following to try and get at your values:
virtual int GetAsInt() { throw CForgeParseError( "Can't make value into integer.", GetLineNum() ); return 0; };
virtual std::string GetAsString() { throw CForgeParseError( "Can't make value into string.", GetLineNum() ); return std::string(); };
virtual bool GetAsBool() { throw CForgeParseError( "Can't make value into boolean.", GetLineNum() ); return false; };
virtual float GetAsFloat() { throw CForgeParseError( "Can't make value into float.", GetLineNum() ); return 0.0; };
protected:
size_t mLineNum;
};
class CNumericValueNodeBase : public CValueNode
{
public:
CNumericValueNodeBase( CParseTree* inTree, size_t inLineNum ) : CValueNode(inTree,inLineNum), mUnit(kLEOUnitNone) {};
virtual void SetUnit( LEOUnit inUnit ) { mUnit = inUnit; };
virtual LEOUnit GetUnit() { return mUnit; };
protected:
LEOUnit mUnit;
};
class CIntValueNode : public CNumericValueNodeBase
{
public:
CIntValueNode( CParseTree* inTree, long long n, size_t inLineNum ) : CNumericValueNodeBase(inTree,inLineNum), mIntValue(n) {};
virtual void GenerateCode( CCodeBlock* inCodeBlock ); // Generate the actual bytecode so it leaves the result on the stack.
virtual bool IsConstant() { return true; };
virtual CIntValueNode* Copy() { CIntValueNode* theNode = new CIntValueNode( mParseTree, mIntValue, mLineNum ); theNode->SetUnit(GetUnit()); return theNode; };
virtual void DebugPrint( std::ostream& destStream, size_t indentLevel )
{
INDENT_PREPARE(indentLevel);
destStream << indentChars << "int( " << mIntValue << gUnitLabels[mUnit] << " )" << std::endl;
};
virtual int GetAsInt() { return (int)mIntValue; };
virtual long GetAsLong() { return (long)mIntValue; };
virtual long long GetAsLongLong() { return mIntValue; };
virtual float GetAsFloat() { return (float)mIntValue; };
virtual std::string GetAsString() { char numStr[256]; snprintf(numStr, 256, "%lld%s", mIntValue, gUnitLabels[mUnit]); return std::string( numStr ); };
protected:
long long mIntValue;
};
class CFloatValueNode : public CNumericValueNodeBase
{
public:
CFloatValueNode( CParseTree* inTree, float n, size_t inLineNum ) : CNumericValueNodeBase(inTree,inLineNum), mFloatValue(n) {};
virtual void GenerateCode( CCodeBlock* inCodeBlock ); // Generate the actual bytecode so it leaves the result on the stack.
virtual bool IsConstant() { return true; };
virtual CFloatValueNode* Copy() { CFloatValueNode* theNode = new CFloatValueNode( mParseTree, mFloatValue, mLineNum ); theNode->SetUnit(GetUnit()); return theNode; };
virtual void DebugPrint( std::ostream& destStream, size_t indentLevel )
{
INDENT_PREPARE(indentLevel);
destStream << indentChars << "float( " << mFloatValue << gUnitLabels[mUnit] << " )" << std::endl;
};
virtual float GetAsFloat() { return mFloatValue; };
virtual int GetAsInt()
{
if( mFloatValue == truncf(mFloatValue) )
return (int)mFloatValue;
else
throw CForgeParseError( "Can't make floating point number into integer.", GetLineNum() );
return 0;
};
virtual std::string GetAsString() { char numStr[256]; snprintf(numStr, 256, "%f%s", mFloatValue,gUnitLabels[mUnit]); return std::string( numStr ); };
protected:
float mFloatValue;
};
class CBoolValueNode : public CValueNode
{
public:
CBoolValueNode( CParseTree* inTree, bool n, size_t inLineNum ) : CValueNode(inTree,inLineNum), mBoolValue(n) {};
virtual void GenerateCode( CCodeBlock* inCodeBlock ); // Generate the actual bytecode so it leaves the result on the stack.
virtual bool IsConstant() { return true; };
virtual CBoolValueNode* Copy() { return new CBoolValueNode( mParseTree, mBoolValue, mLineNum ); };
virtual void DebugPrint( std::ostream& destStream, size_t indentLevel )
{
INDENT_PREPARE(indentLevel);
destStream << indentChars << (mBoolValue ? "true" : "false") << std::endl;
};
virtual bool GetAsBool() { return mBoolValue; };
virtual std::string GetAsString() { return std::string( mBoolValue ? "true" : "false" ); };
protected:
bool mBoolValue;
};
class CStringValueNode : public CValueNode
{
public:
CStringValueNode( CParseTree* inTree, const std::string& n, size_t inLineNum ) : CValueNode(inTree,inLineNum), mStringValue(n) {};
virtual void GenerateCode( CCodeBlock* inCodeBlock ); // Generate the actual bytecode so it leaves the result on the stack.
virtual bool IsConstant() { return true; };
virtual CStringValueNode* Copy() { return new CStringValueNode( mParseTree, mStringValue, mLineNum ); };
virtual void DebugPrint( std::ostream& destStream, size_t indentLevel )
{
INDENT_PREPARE(indentLevel);
destStream << indentChars << "\"" << LEOStringEscapedForPrintingInQuotes(mStringValue.c_str()) << "\"" << std::endl;
};
virtual std::string GetAsString() { return mStringValue; };
virtual bool GetAsBool()
{
if( mStringValue.compare("true") == 0 )
return true;
else if( mStringValue.compare("false") != 0 )
throw CForgeParseError( "Can't make string into boolean.", GetLineNum() );
return false;
};
protected:
std::string mStringValue;
};
class CUnsetValueNode : public CValueNode
{
public:
CUnsetValueNode( CParseTree* inTree, size_t inLineNum ) : CValueNode(inTree,inLineNum) {};
virtual void GenerateCode( CCodeBlock* inCodeBlock ); // Generate the actual bytecode so it leaves the result on the stack.
virtual bool IsConstant() { return true; };
virtual CUnsetValueNode* Copy() { return new CUnsetValueNode( mParseTree, mLineNum ); };
virtual void DebugPrint( std::ostream& destStream, size_t indentLevel )
{
INDENT_PREPARE(indentLevel);
destStream << indentChars << "<unset>" << std::endl;
};
virtual std::string GetAsString() { return ""; };
virtual bool GetAsBool()
{
throw CForgeParseError( "Can't make unset value into boolean.", GetLineNum() );
return false;
};
};
class CLocalVariableRefValueNode : public CValueNode
{
public:
CLocalVariableRefValueNode( CParseTree* inTree, CCodeBlockNodeBase *inCodeBlockNode, const std::string& inVarName, const std::string& inRealVarName, size_t inLineNum );
virtual void Simplify();
virtual void GenerateCode( CCodeBlock* inCodeBlock ); // Generate the actual bytecode so it leaves the result on the stack.
virtual CLocalVariableRefValueNode* Copy() { return new CLocalVariableRefValueNode( mParseTree, mCodeBlockNode, mVarName, mRealVarName, mLineNum ); };
virtual void DebugPrint( std::ostream& destStream, size_t indentLevel )
{
INDENT_PREPARE(indentLevel);
destStream << indentChars << "localVar( " << mVarName.c_str() << " )" << std::endl;
};
int16_t GetBPRelativeOffset();
std::string GetVarName() { return mVarName; };
std::string GetRealVarName() { return mRealVarName; };
protected:
std::string mVarName;
std::string mRealVarName;
CCodeBlockNodeBase * mCodeBlockNode;
};
class CArrayValueNode : public CValueNode
{
public:
CArrayValueNode( CParseTree* inTree, size_t inLineNum ) : CValueNode(inTree,inLineNum) {}
virtual ~CArrayValueNode() { for( CValueNode * currItem : mArray ) { if( currItem ) delete currItem; } }
virtual void AddItem( CValueNode * inNode ) { mArray.push_back(inNode); } // Takes over ownership.
virtual void SetItemAtIndex( CValueNode * inNode, size_t idx ) { mArray[idx] = inNode; } // Takes over ownership.
virtual size_t GetItemCount() { return mArray.size(); }
virtual CValueNode * GetItem( size_t idx ) { return mArray[idx]; }
virtual void GenerateCode( CCodeBlock* inCodeBlock ); // Generate the actual bytecode so it leaves the result on the stack.
virtual void Simplify();
virtual CArrayValueNode* Copy();
virtual void DebugPrint( std::ostream& destStream, size_t indentLevel )
{
INDENT_PREPARE(indentLevel);
destStream << indentChars << "array" << std::endl;
destStream << indentChars << "{" << std::endl;
for( CValueNode * currItem : mArray )
{
currItem->DebugPrint( destStream, indentLevel +1 );
}
destStream << indentChars << "}" << std::endl;
};
protected:
std::vector<CValueNode*> mArray;
};
}