forked from asavine/Scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptingNodes.h
More file actions
239 lines (181 loc) · 5.3 KB
/
scriptingNodes.h
File metadata and controls
239 lines (181 loc) · 5.3 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
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
/*
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
using namespace std;
#include <vector>
#include <memory>
#include "scriptingVisitor.h"
// Typedefs
struct Node;
using ExprTree = unique_ptr<Node>;
using Expression = ExprTree;
using Statement = ExprTree;
using Event = vector<Statement>;
// Base nodes
struct Node : VisitableBase<VISITORS>
{
using VisitableBase<VISITORS>::accept;
vector<ExprTree> arguments;
virtual ~Node() {}
};
// Hierarchy
// Nodes that return a number
struct exprNode : Node
{
bool isConst = false;
double constVal;
};
// Action nodes
struct actNode : Node
{};
// Nodes that return a bool
struct boolNode : Node
{
bool alwaysTrue;
bool alwaysFalse;
};
// All the concrete nodes
// Binary expressions
struct NodeAdd : Visitable<exprNode, NodeAdd, VISITORS> {};
struct NodeSub : Visitable<exprNode, NodeSub, VISITORS> {};
struct NodeMult : Visitable<exprNode, NodeMult, VISITORS> {};
struct NodeDiv : Visitable<exprNode, NodeDiv, VISITORS> {};
struct NodePow : Visitable<exprNode, NodePow, VISITORS> {};
struct NodeMax : Visitable<exprNode, NodeMax, VISITORS> {};
struct NodeMin : Visitable<exprNode, NodeMin, VISITORS> {};
// Unary expressions
struct NodeUplus : Visitable<exprNode, NodeUplus, VISITORS> {};
struct NodeUminus : Visitable<exprNode, NodeUminus, VISITORS> {};
// Math operators
struct NodeLog : Visitable<exprNode, NodeLog, VISITORS> {};
struct NodeSqrt : Visitable<exprNode, NodeSqrt, VISITORS> {};
// Multi expressions
struct NodeSmooth : Visitable<exprNode, NodeSmooth, VISITORS> {};
// Comparisons
struct compNode : boolNode
{
// Fuzzying stuff
bool discrete; // Continuous or discrete
// Continuous eps
double eps;
// Discrete butterfly bounds
double lb;
double rb;
// End of fuzzying stuff
};
struct NodeEqual : Visitable<compNode, NodeEqual, VISITORS> {};
struct NodeSup : Visitable<compNode, NodeSup, VISITORS> {};
struct NodeSupEqual : Visitable<compNode, NodeSupEqual, VISITORS> {};
// And/or/not
struct NodeAnd : Visitable<boolNode, NodeAnd, VISITORS> {};
struct NodeOr : Visitable<boolNode, NodeOr, VISITORS> {};
struct NodeNot : Visitable<boolNode, NodeNot, VISITORS> {};
// Leaves
// Market access
struct NodeSpot : Visitable<exprNode, NodeSpot, VISITORS> {};
// Const
struct NodeConst : Visitable<exprNode, NodeConst, VISITORS>
{
NodeConst(const double val)
{
isConst = true;
constVal = val;
}
};
struct NodeTrue : Visitable<boolNode, NodeTrue, VISITORS>
{
NodeTrue()
{
alwaysTrue = true;
}
};
struct NodeFalse : Visitable<boolNode, NodeFalse, VISITORS>
{
NodeFalse()
{
alwaysFalse = true;
}
};
// Variable
struct NodeVar : Visitable<exprNode, NodeVar, VISITORS>
{
NodeVar(const string n) : name(n)
{
isConst = true;
constVal = 0.0;
}
const string name;
size_t index;
};
// Assign, Pays
struct NodeAssign : Visitable<actNode, NodeAssign, VISITORS> {};
struct NodePays : Visitable<actNode, NodePays, VISITORS> {};
// If
struct NodeIf : Visitable<actNode, NodeIf, VISITORS>
{
int firstElse;
// For fuzzy eval: indices of variables affected in statements, including nested
vector<size_t> affectedVars;
// Always true/false as per domain processor
bool alwaysTrue;
bool alwaysFalse;
};
// Collection of statements
struct NodeCollect : Visitable<actNode, NodeCollect, VISITORS> {};
// Utilities
// Downcast Node to concrete type, crashes if used on another Node type
template<class Concrete>
const Concrete* downcast(const unique_ptr<Node>& node)
{
return static_cast<const Concrete*>(node.get());
}
template<class Concrete>
Concrete* downcast(unique_ptr<Node>& node)
{
return static_cast<Concrete*>(node.get());
}
// Factories
// Make concrete node
template <typename ConcreteNode, typename... Args>
unique_ptr<ConcreteNode> make_node(Args&&... args)
{
return unique_ptr<ConcreteNode>(new ConcreteNode(forward<Args>(args)...));
}
// Same but return as pointer on base
template <typename ConcreteNode, typename... Args>
unique_ptr<Node> make_base_node(Args&&... args)
{
return unique_ptr<Node>(new ConcreteNode(forward<Args>(args)...));
}
// Build binary concrete, and set its arguments to lhs and rhs
template <class NodeType>
unique_ptr<NodeType> make_binary(ExprTree& lhs, ExprTree& rhs)
{
auto top = make_node<NodeType>();
top->arguments.resize(2);
// Take ownership of lhs and rhs
top->arguments[0] = move(lhs);
top->arguments[1] = move(rhs);
// Return
return top;
}
// Same but return as pointer on base
template <class ConcreteNode>
ExprTree make_base_binary(ExprTree& lhs, ExprTree& rhs)
{
auto top = make_base_node<ConcreteNode>();
top->arguments.resize(2);
// Take ownership of lhs and rhs
top->arguments[0] = move(lhs);
top->arguments[1] = move(rhs);
// Return
return top;
}