Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
ast.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file ored/scripting/ast.hpp
20 \brief abstract syntax tree for payoff scripting
21 \ingroup utilities
22*/
23
24#pragma once
25
27
28#include <ql/patterns/visitor.hpp>
29#include <ql/types.hpp>
30#include <ql/utilities/null.hpp>
31
32#include <boost/fusion/container/vector.hpp>
33#include <boost/fusion/include/at_c.hpp>
34#include <ql/shared_ptr.hpp>
35
36#include <iostream>
37#include <string>
38#include <vector>
39
40namespace ore {
41namespace data {
42
43using namespace QuantLib;
44
45struct ASTNode;
46using ASTNodePtr = QuantLib::ext::shared_ptr<ASTNode>;
47
50 LocationInfo(const Size lineStart, const Size columnStart, const Size lineEnd, const Size columnEnd)
54};
55
56std::string to_string(const LocationInfo& l);
57
58struct ASTNode {
60 virtual ~ASTNode() {}
61 ASTNode(const std::vector<ASTNodePtr>& args, const Size minArgs, const Size maxArgs = Null<Size>());
62 virtual void accept(AcyclicVisitor&);
64 std::vector<ASTNodePtr> args;
65};
66
67struct OperatorPlusNode : public ASTNode {
68 OperatorPlusNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2) {}
69 void accept(AcyclicVisitor&) override;
70};
71
72struct OperatorMinusNode : public ASTNode {
73 OperatorMinusNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2) {}
74 void accept(AcyclicVisitor&) override;
75};
76
78 OperatorMultiplyNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2) {}
79 void accept(AcyclicVisitor&) override;
80};
81
82struct OperatorDivideNode : public ASTNode {
83 OperatorDivideNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2) {}
84 void accept(AcyclicVisitor&) override;
85};
86
87struct NegateNode : public ASTNode {
88 NegateNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 1, 1) {}
89 void accept(AcyclicVisitor&) override;
90};
91
92struct FunctionAbsNode : public ASTNode {
93 FunctionAbsNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 1, 1) {}
94 void accept(AcyclicVisitor&) override;
95};
96
97struct FunctionExpNode : public ASTNode {
98 FunctionExpNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 1, 1) {}
99 void accept(AcyclicVisitor&) override;
100};
101
102struct FunctionLogNode : public ASTNode {
103 FunctionLogNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 1, 1) {}
104 void accept(AcyclicVisitor&) override;
105};
106
107struct FunctionSqrtNode : public ASTNode {
108 FunctionSqrtNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 1, 1) {}
109 void accept(AcyclicVisitor&) override;
110};
111
113 FunctionNormalCdfNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 1, 1) {}
114 void accept(AcyclicVisitor&) override;
115};
116
118 FunctionNormalPdfNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 1, 1) {}
119 void accept(AcyclicVisitor&) override;
120};
121
122struct FunctionMaxNode : public ASTNode {
123 FunctionMaxNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2) {}
124 void accept(AcyclicVisitor&) override;
125};
126
127struct FunctionMinNode : public ASTNode {
128 FunctionMinNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2) {}
129 void accept(AcyclicVisitor&) override;
130};
131
132struct FunctionPowNode : public ASTNode {
133 FunctionPowNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2) {}
134 void accept(AcyclicVisitor&) override;
135};
136
137struct FunctionBlackNode : public ASTNode {
138 FunctionBlackNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 6, 6) {}
139 void accept(AcyclicVisitor&) override;
140};
141
142struct FunctionDcfNode : public ASTNode {
143 FunctionDcfNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 3, 3) {}
144 void accept(AcyclicVisitor&) override;
145};
146
147struct FunctionDaysNode : public ASTNode {
148 FunctionDaysNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 3, 3) {}
149 void accept(AcyclicVisitor&) override;
150};
151
152struct FunctionPayNode : public ASTNode {
153 FunctionPayNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 4, 4) {}
154 void accept(AcyclicVisitor&) override;
155};
156
158 FunctionLogPayNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 4, 7) {}
159 void accept(AcyclicVisitor&) override;
160};
161
162struct FunctionNpvNode : public ASTNode {
163 FunctionNpvNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 5) {}
164 void accept(AcyclicVisitor&) override;
165};
166
168 FunctionNpvMemNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 3, 6) {}
169 void accept(AcyclicVisitor&) override;
170};
171
172struct HistFixingNode : public ASTNode {
173 HistFixingNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2) {}
174 void accept(AcyclicVisitor&) override;
175};
176
178 FunctionDiscountNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 3, 3) {}
179 void accept(AcyclicVisitor&) override;
180};
181
183 FunctionFwdCompNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 4, 14) {}
184 void accept(AcyclicVisitor&) override;
185};
186
188 FunctionFwdAvgNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 4, 14) {}
189 void accept(AcyclicVisitor&) override;
190};
191
193 FunctionAboveProbNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 4, 4) {}
194 void accept(AcyclicVisitor&) override;
195};
196
198 FunctionBelowProbNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 4, 4) {}
199 void accept(AcyclicVisitor&) override;
200};
201
203 FunctionDateIndexNode(const boost::fusion::vector<std::string, std::string>& params,
204 const std::vector<ASTNodePtr>& args)
205 : ASTNode(args, 1, 1), name(boost::fusion::at_c<0>(params)), op(boost::fusion::at_c<1>(params)) {}
206 void accept(AcyclicVisitor&) override;
207 std::string name, op;
208};
209
210struct SortNode : public ASTNode {
211 SortNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 1, 3) {}
212 void accept(AcyclicVisitor&) override;
213};
214
215struct PermuteNode : public ASTNode {
216 PermuteNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 3) {}
217 void accept(AcyclicVisitor&) override;
218};
219
221 ConstantNumberNode(const double value, const std::vector<ASTNodePtr>& args = {})
222 : ASTNode(args, 0, 0), value(value) {}
223 void accept(AcyclicVisitor&) override;
224 const double value;
225};
226
227struct VariableNode : public ASTNode {
228 VariableNode(const std::string& name, const std::vector<ASTNodePtr>& args = {}) : ASTNode(args, 0, 1), name(name) {}
229 void accept(AcyclicVisitor&) override;
230 const std::string name;
231 // cache for optimised variable reference retrieval
232 bool isCached = false, isScalar = false;
234 std::vector<ValueType>* cachedVector = nullptr;
235};
236
237struct SizeOpNode : public ASTNode {
238 SizeOpNode(const std::string& name, const std::vector<ASTNodePtr>& args = {}) : ASTNode(args, 0, 0), name(name) {}
239 void accept(AcyclicVisitor&) override;
240 const std::string name;
241};
242
243struct VarEvaluationNode : public ASTNode {
244 VarEvaluationNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 3) {}
245 void accept(AcyclicVisitor&) override;
246};
247
248struct AssignmentNode : public ASTNode {
249 AssignmentNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2) {}
250 void accept(AcyclicVisitor&) override;
251};
252
253struct RequireNode : public ASTNode {
254 RequireNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 1, 1) {}
255 void accept(AcyclicVisitor&) override;
256};
257
259 DeclarationNumberNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 1) {}
260 void accept(AcyclicVisitor&) override;
261};
262
263struct SequenceNode : public ASTNode {
264 SequenceNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 1) {}
265 void accept(AcyclicVisitor&) override;
266};
267
268struct ConditionEqNode : public ASTNode {
269 ConditionEqNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2){};
270 void accept(AcyclicVisitor&) override;
271};
272
273struct ConditionNeqNode : public ASTNode {
274 ConditionNeqNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2){};
275 void accept(AcyclicVisitor&) override;
276};
277
278struct ConditionLtNode : public ASTNode {
279 ConditionLtNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2){};
280 void accept(AcyclicVisitor&) override;
281};
282
283struct ConditionLeqNode : public ASTNode {
284 ConditionLeqNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2){};
285 void accept(AcyclicVisitor&) override;
286};
287
288struct ConditionGtNode : public ASTNode {
289 ConditionGtNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2){};
290 void accept(AcyclicVisitor&) override;
291};
292
293struct ConditionGeqNode : public ASTNode {
294 ConditionGeqNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2){};
295 void accept(AcyclicVisitor&) override;
296};
297
298struct ConditionNotNode : public ASTNode {
299 ConditionNotNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 1, 1){};
300 void accept(AcyclicVisitor&) override;
301};
302
303struct ConditionAndNode : public ASTNode {
304 ConditionAndNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2){};
305 void accept(AcyclicVisitor&) override;
306};
307
308struct ConditionOrNode : public ASTNode {
309 ConditionOrNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 2){};
310 void accept(AcyclicVisitor&) override;
311};
312
313struct IfThenElseNode : public ASTNode {
314 IfThenElseNode(const std::vector<ASTNodePtr>& args) : ASTNode(args, 2, 3) {}
315 void accept(AcyclicVisitor&) override;
316};
317
318struct LoopNode : public ASTNode {
319 LoopNode(const std::string& name, const std::vector<ASTNodePtr>& args) : ASTNode(args, 4, 4), name(name) {}
320 void accept(AcyclicVisitor&) override;
321 const std::string name;
322};
323
324} // namespace data
325} // namespace ore
@ data
Definition: log.hpp:77
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
boost::variant< RandomVariable, EventVec, CurrencyVec, IndexVec, DaycounterVec, Filter > ValueType
Definition: value.hpp:60
QuantLib::ext::shared_ptr< ASTNode > ASTNodePtr
Definition: ast.hpp:46
Serializable Credit Default Swap.
Definition: namespaces.docs:23
std::vector< ASTNodePtr > args
Definition: ast.hpp:64
virtual void accept(AcyclicVisitor &)
Definition: ast.cpp:48
LocationInfo locationInfo
Definition: ast.hpp:63
virtual ~ASTNode()
Definition: ast.hpp:60
AssignmentNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:249
void accept(AcyclicVisitor &) override
Definition: ast.cpp:328
ConditionAndNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:304
void accept(AcyclicVisitor &) override
Definition: ast.cpp:416
void accept(AcyclicVisitor &) override
Definition: ast.cpp:360
ConditionEqNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:269
ConditionGeqNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:294
void accept(AcyclicVisitor &) override
Definition: ast.cpp:400
void accept(AcyclicVisitor &) override
Definition: ast.cpp:392
ConditionGtNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:289
void accept(AcyclicVisitor &) override
Definition: ast.cpp:384
ConditionLeqNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:284
ConditionLtNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:279
void accept(AcyclicVisitor &) override
Definition: ast.cpp:376
ConditionNeqNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:274
void accept(AcyclicVisitor &) override
Definition: ast.cpp:368
void accept(AcyclicVisitor &) override
Definition: ast.cpp:408
ConditionNotNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:299
ConditionOrNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:309
void accept(AcyclicVisitor &) override
Definition: ast.cpp:424
ConstantNumberNode(const double value, const std::vector< ASTNodePtr > &args={})
Definition: ast.hpp:221
void accept(AcyclicVisitor &) override
Definition: ast.cpp:296
void accept(AcyclicVisitor &) override
Definition: ast.cpp:344
DeclarationNumberNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:259
FunctionAboveProbNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:193
void accept(AcyclicVisitor &) override
Definition: ast.cpp:256
FunctionAbsNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:93
void accept(AcyclicVisitor &) override
Definition: ast.cpp:96
void accept(AcyclicVisitor &) override
Definition: ast.cpp:264
FunctionBelowProbNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:198
FunctionBlackNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:138
void accept(AcyclicVisitor &) override
Definition: ast.cpp:168
void accept(AcyclicVisitor &) override
Definition: ast.cpp:272
FunctionDateIndexNode(const boost::fusion::vector< std::string, std::string > &params, const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:203
void accept(AcyclicVisitor &) override
Definition: ast.cpp:184
FunctionDaysNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:148
void accept(AcyclicVisitor &) override
Definition: ast.cpp:176
FunctionDcfNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:143
void accept(AcyclicVisitor &) override
Definition: ast.cpp:232
FunctionDiscountNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:178
FunctionExpNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:98
void accept(AcyclicVisitor &) override
Definition: ast.cpp:104
FunctionFwdAvgNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:188
void accept(AcyclicVisitor &) override
Definition: ast.cpp:248
void accept(AcyclicVisitor &) override
Definition: ast.cpp:240
FunctionFwdCompNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:183
void accept(AcyclicVisitor &) override
Definition: ast.cpp:112
FunctionLogNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:103
void accept(AcyclicVisitor &) override
Definition: ast.cpp:200
FunctionLogPayNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:158
void accept(AcyclicVisitor &) override
Definition: ast.cpp:144
FunctionMaxNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:123
void accept(AcyclicVisitor &) override
Definition: ast.cpp:152
FunctionMinNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:128
void accept(AcyclicVisitor &) override
Definition: ast.cpp:128
FunctionNormalCdfNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:113
void accept(AcyclicVisitor &) override
Definition: ast.cpp:136
FunctionNormalPdfNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:118
void accept(AcyclicVisitor &) override
Definition: ast.cpp:216
FunctionNpvMemNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:168
void accept(AcyclicVisitor &) override
Definition: ast.cpp:208
FunctionNpvNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:163
void accept(AcyclicVisitor &) override
Definition: ast.cpp:192
FunctionPayNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:153
void accept(AcyclicVisitor &) override
Definition: ast.cpp:160
FunctionPowNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:133
FunctionSqrtNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:108
void accept(AcyclicVisitor &) override
Definition: ast.cpp:120
HistFixingNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:173
void accept(AcyclicVisitor &) override
Definition: ast.cpp:224
void accept(AcyclicVisitor &) override
Definition: ast.cpp:432
IfThenElseNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:314
LocationInfo(const Size lineStart, const Size columnStart, const Size lineEnd, const Size columnEnd)
Definition: ast.hpp:50
void accept(AcyclicVisitor &) override
Definition: ast.cpp:440
LoopNode(const std::string &name, const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:319
const std::string name
Definition: ast.hpp:321
void accept(AcyclicVisitor &) override
Definition: ast.cpp:88
NegateNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:88
void accept(AcyclicVisitor &) override
Definition: ast.cpp:80
OperatorDivideNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:83
OperatorMinusNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:73
void accept(AcyclicVisitor &) override
Definition: ast.cpp:64
void accept(AcyclicVisitor &) override
Definition: ast.cpp:72
OperatorMultiplyNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:78
OperatorPlusNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:68
void accept(AcyclicVisitor &) override
Definition: ast.cpp:56
void accept(AcyclicVisitor &) override
Definition: ast.cpp:288
PermuteNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:216
void accept(AcyclicVisitor &) override
Definition: ast.cpp:336
RequireNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:254
SequenceNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:264
void accept(AcyclicVisitor &) override
Definition: ast.cpp:352
void accept(AcyclicVisitor &) override
Definition: ast.cpp:312
SizeOpNode(const std::string &name, const std::vector< ASTNodePtr > &args={})
Definition: ast.hpp:238
const std::string name
Definition: ast.hpp:240
void accept(AcyclicVisitor &) override
Definition: ast.cpp:280
SortNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:211
void accept(AcyclicVisitor &) override
Definition: ast.cpp:320
VarEvaluationNode(const std::vector< ASTNodePtr > &args)
Definition: ast.hpp:244
VariableNode(const std::string &name, const std::vector< ASTNodePtr > &args={})
Definition: ast.hpp:228
void accept(AcyclicVisitor &) override
Definition: ast.cpp:304
std::vector< ValueType > * cachedVector
Definition: ast.hpp:234
ValueType * cachedScalar
Definition: ast.hpp:233
const std::string name
Definition: ast.hpp:230
value type and operations