-
Notifications
You must be signed in to change notification settings - Fork 2
/
CCodeBlockNode.h
103 lines (80 loc) · 3.94 KB
/
CCodeBlockNode.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
/*
* CCodeBlockNode.h
* HyperCompiler
*
* Created by Uli Kusterer on 10.05.07.
* Copyright 2007 M. Uli Kusterer. All rights reserved.
*
*/
#pragma once
#include "CValueNode.h"
#include "CParser.h"
#include <vector>
namespace Carlson
{
class CCommandNode;
// Used for top-level code blocks and the type of params to objects that accept
// a code block:
// This is an abstract class. You need to implement AddLocalVar() and GetLocals()
// yourself, to do whatever is appropriate for your kind of block:
class CCodeBlockNodeBase : public CNode
{
public:
CCodeBlockNodeBase( CParseTree* inTree, size_t inLineNum, const std::string &inFileName )
: CNode(inTree), mLineNum( inLineNum ), mFileName(inFileName) {};
virtual ~CCodeBlockNodeBase();
virtual void AddCommand( CNode* inCmd ) { mCommands.push_back( inCmd ); mParseTree->NodeWasAdded( inCmd ); }; // Function node now owns this command and will delete it!
virtual size_t GetCommandsCount() { return mCommands.size(); };
virtual void AddLocalVar( const std::string& inName, const std::string& inUserName,
TVariantType theType, bool initWithName = false,
bool isParam = false, bool isGlobal = false,
bool dontDispose = false ) = 0; // It's OK to call this several times with the same variable. Subsequent calls will be ignored.
virtual int16_t GetBPRelativeOffsetForLocalVar( const std::string& inName ) = 0;
// Sub-blocks retrieve and modify these three as needed: // TODO: This isn't really very OO.
virtual int16_t& GetLocalVariableCount() = 0;
virtual std::map<std::string,CVariableEntry>& GetLocals() = 0;
virtual std::map<std::string,CVariableEntry>& GetGlobals() = 0;
virtual bool LocalVariableExists( const std::string& inStr );
virtual void DebugPrint( std::ostream& destStream, size_t indentLevel );
virtual void Simplify();
virtual void Visit( std::function<void(CNode*)> visitorBlock );
virtual void GenerateCode( CCodeBlock* inCodeBlock );
virtual bool GetAllVarsAreGlobals() { CCodeBlockNodeBase * owner = GetContainingFunction(); return owner ? owner->GetAllVarsAreGlobals() : false; }
virtual void DebugPrintInner( std::ostream& destStream, size_t indentLevel );
virtual CCodeBlockNodeBase* GetContainingFunction() { return NULL; };
size_t GetLineNum() { return mLineNum; };
protected:
size_t mLineNum;
std::vector<CNode*> mCommands;
std::string mFileName;
};
// This is a concrete code block. It simply groups together a couple of commands
// and changes the local and global variable lists of the "owning" block you pass in:
class CCodeBlockNode : public CCodeBlockNodeBase
{
public:
CCodeBlockNode( CParseTree* inTree, size_t inLineNum, const std::string &inFileName, CCodeBlockNodeBase* owningBlock )
: CCodeBlockNodeBase( inTree, inLineNum, inFileName ), mOwningBlock(owningBlock)
{
mGlobals = &owningBlock->GetGlobals();
mLocals = &owningBlock->GetLocals();
mLocalVariableCount = &owningBlock->GetLocalVariableCount();
};
virtual ~CCodeBlockNode() {};
virtual void AddLocalVar( const std::string& inName, const std::string& inUserName,
TVariantType theType, bool initWithName = false,
bool isParam = false, bool isGlobal = false,
bool dontDispose = false );
virtual int16_t GetBPRelativeOffsetForLocalVar( const std::string& inName );
// Sub-blocks retrieve and modify these two as needed: // TODO: This isn't really very OO.
virtual int16_t& GetLocalVariableCount() { return *mLocalVariableCount; };
virtual std::map<std::string,CVariableEntry>& GetLocals() { return *mLocals; };
virtual std::map<std::string,CVariableEntry>& GetGlobals() { return *mGlobals; };
virtual CCodeBlockNodeBase* GetContainingFunction() { return mOwningBlock->GetContainingFunction(); };
protected:
std::map<std::string,CVariableEntry>* mLocals;
int16_t* mLocalVariableCount;
CCodeBlockNodeBase* mOwningBlock;
std::map<std::string,CVariableEntry>* mGlobals;
};
}