-
Notifications
You must be signed in to change notification settings - Fork 2
/
CNode.h
95 lines (72 loc) · 3.33 KB
/
CNode.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
/*
* CNode.h
* HyperCompiler
*
* Created by Uli Kusterer on 10.05.07.
* Copyright 2007 M. Uli Kusterer. All rights reserved.
*
*/
#pragma once
// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------
#include <ostream>
#include <functional>
#if 1
#define CNODE_PURE_VIRTUAL = 0
#else
#define CNODE_PURE_VIRTUAL {}
#endif
namespace Carlson
{
// -----------------------------------------------------------------------------
// Classes:
// -----------------------------------------------------------------------------
class CCodeBlock;
class CParseTree;
// Abstract root class for things in a parse tree:
// These are stupid, and can simply be debug-printed or turned into code, and
// that is it.
class CNode
{
public:
explicit CNode( CParseTree* inTree ) : mParseTree(inTree) {};
virtual ~CNode() {};
virtual void Simplify(); // For optimizing our parse tree before we actually generate code.
virtual void Visit( std::function<void(CNode*)> visitorBlock );
virtual void GenerateCode( CCodeBlock* inCodeBlock ); // Generate the actual bytecode.
virtual void DebugPrint( std::ostream& destStream, size_t indentLevel ) = 0;
virtual CParseTree* GetParseTree() { return mParseTree; };
protected:
CParseTree* mParseTree;
};
} // namespace Carlson
// -----------------------------------------------------------------------------
// Helper Macros:
// -----------------------------------------------------------------------------
// The INDENT_PREPARE(level) Macro gives you a char array named 'indentChars'
// containing the requested number of tabs (or, exceeding an arbitrary limit,
// just gives you that many tabs and calls it a day). This is used in debug
// output of the parse tree to indent each level of output dynamically.
#define INDENT_CHARS { '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',\
'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', 0 }
#define INDENT_PREPARE(indentLevel) char indentChars[] = INDENT_CHARS;\
if( indentLevel < sizeof(indentChars) ) indentChars[indentLevel] = 0