-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathscriptingDebugger.h
More file actions
188 lines (151 loc) · 4.23 KB
/
scriptingDebugger.h
File metadata and controls
188 lines (151 loc) · 4.23 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
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
/*
Written by Antoine Savine in 2018
This code is the strict IP of Antoine Savine
License to use and alter this code for personal and commercial applications
is freely granted to any person or company who purchased a copy of the book
Modern Computational Finance: Scripting for Derivatives and XVA
Jesper Andreasen & Antoine Savine
Wiley, 2018
As long as this comment is preserved at the top of the file
*/
#pragma once
#include "scriptingNodes.h"
#include "quickStack.h"
class Debugger : public constVisitor<Debugger>
{
string myPrefix;
staticStack<string> myStack;
// The main function call from every node visitor
void debug( const Node& node, const string& nodeId)
{
// One more tab
myPrefix += '\t';
// Visit arguments, right to left
for( auto it = node.arguments.rbegin(); it != node.arguments.rend(); ++it)
(*it)->accept( *this);
// One less tab
myPrefix.pop_back();
string str( myPrefix + nodeId);
if( ! node.arguments.empty())
{
str += "(\n";
// First argument, pushed last
str += myStack.top();
myStack.pop();
if( node.arguments.size() > 1) str += myPrefix + ",\n";
// Args 2 to n-1
for(size_t i=1; i<node.arguments.size()-1; ++i)
{
str += myStack.top() + myPrefix + ",\n";
myStack.pop();
}
if( node.arguments.size() > 1)
{
// Last argument, pushed first
str += myStack.top();
myStack.pop();
}
// Close ')'
str += myPrefix + ')';
}
str += '\n';
myStack.push( move( str));
}
public:
using constVisitor<Debugger>::visit;
// Access the top of the stack, contains the functional form after the tree is traversed
string getString() const
{
return myStack.top();
}
// All concrete node visitors, visit arguments by default unless overridden
void visit(const NodeCollect& node) { debug( node, "COLLECT"); }
void visit(const NodeUplus& node) { debug( node, "UPLUS"); }
void visit(const NodeUminus& node) { debug( node, "UMINUS"); }
void visit(const NodeAdd& node) { debug( node, "ADD"); }
void visit(const NodeSub& node) { debug( node, "SUBTRACT"); }
void visit(const NodeMult& node) { debug( node, "MULT"); }
void visit(const NodeDiv& node) { debug( node, "DIV"); }
void visit(const NodePow& node) { debug( node, "POW"); }
void visit(const NodeLog& node) { debug( node, "LOG"); }
void visit(const NodeSqrt& node) { debug( node, "SQRT"); }
void visit(const NodeMax& node) { debug( node, "MAX"); }
void visit(const NodeMin& node) { debug( node, "MIN"); }
void visit(const NodeSmooth& node) { debug( node, "SMOOTH"); }
void visit(const NodeEqual& node)
{
string s="EQUALZERO";
if( !node.discrete)
{
s+= "[CONT,EPS=" + to_string( node.eps) + "]";
}
else
{
s+= "[DISCRETE,";
s+= "BOUNDS=" + to_string( node.lb) + "," + to_string( node.rb) + "]";
}
debug( node, s);
}
void visit(const NodeNot& node)
{
string s = "NOT";
debug(node, s);
}
void visit(const NodeSup& node)
{
string s = "GTZERO";
if( !node.discrete)
{
s+= "[CONT,EPS=" + to_string( node.eps) + "]";
}
else
{
s+= "[DISCRETE,";
s+= "BOUNDS=" + to_string( node.lb) + "," + to_string( node.rb) + "]";
}
debug( node, s);
}
void visit(const NodeSupEqual& node)
{
string s = "GTEQUALZERO";
if( !node.discrete)
{
s+= "[CONT,EPS=" + to_string( node.eps) + "]";
}
else
{
s+= "[DISCRETE,";
s+= "BOUNDS=" + to_string( node.lb) + "," + to_string( node.rb) + "]";
}
debug( node, s);
}
void visit(const NodeAnd& node)
{
string s = "AND";
debug(node, s);
}
void visit(const NodeOr& node)
{
string s = "OR";
debug(node, s);
}
void visit(const NodeAssign& node) { debug( node, "ASSIGN"); }
void visit(const NodePays& node) { debug( node, "PAYS"); }
void visit(const NodeSpot& node) { debug( node, "SPOT"); }
void visit(const NodeIf& node)
{
string s = "IF";
s += "[FIRSTELSE=" + to_string( node.firstElse)+"]";
debug(node, s);
}
void visit(const NodeTrue& node) { debug( node, "TRUE"); }
void visit(const NodeFalse& node) { debug( node, "FALSE"); }
void visit(const NodeConst& node)
{
debug(node, string( "CONST[")+to_string( node.constVal)+']');
}
void visit(const NodeVar& node)
{
debug( node, string( "VAR[")+node.name+','+to_string( node.index)+']');
}
};