Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
astprinter.cpp
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
20
21#include <sstream>
22
23namespace ore {
24namespace data {
25
26namespace {
27class ASTPrinter : public AcyclicVisitor,
28 public Visitor<ASTNode>,
29 public Visitor<OperatorPlusNode>,
30 public Visitor<OperatorMinusNode>,
31 public Visitor<OperatorMultiplyNode>,
32 public Visitor<OperatorDivideNode>,
33 public Visitor<NegateNode>,
34 public Visitor<FunctionAbsNode>,
35 public Visitor<FunctionExpNode>,
36 public Visitor<FunctionLogNode>,
37 public Visitor<FunctionSqrtNode>,
38 public Visitor<FunctionNormalCdfNode>,
39 public Visitor<FunctionNormalPdfNode>,
40 public Visitor<FunctionMinNode>,
41 public Visitor<FunctionMaxNode>,
42 public Visitor<FunctionPowNode>,
43 public Visitor<FunctionBlackNode>,
44 public Visitor<FunctionDcfNode>,
45 public Visitor<FunctionDaysNode>,
46 public Visitor<FunctionPayNode>,
47 public Visitor<FunctionLogPayNode>,
48 public Visitor<FunctionNpvNode>,
49 public Visitor<FunctionNpvMemNode>,
50 public Visitor<HistFixingNode>,
51 public Visitor<FunctionDiscountNode>,
52 public Visitor<FunctionFwdCompNode>,
53 public Visitor<FunctionFwdAvgNode>,
54 public Visitor<FunctionAboveProbNode>,
55 public Visitor<FunctionBelowProbNode>,
56 public Visitor<FunctionDateIndexNode>,
57 public Visitor<SortNode>,
58 public Visitor<PermuteNode>,
59 public Visitor<ConstantNumberNode>,
60 public Visitor<VariableNode>,
61 public Visitor<SizeOpNode>,
62 public Visitor<VarEvaluationNode>,
63 public Visitor<AssignmentNode>,
64 public Visitor<RequireNode>,
65 public Visitor<DeclarationNumberNode>,
66 public Visitor<SequenceNode>,
67 public Visitor<ConditionEqNode>,
68 public Visitor<ConditionNeqNode>,
69 public Visitor<ConditionLtNode>,
70 public Visitor<ConditionLeqNode>,
71 public Visitor<ConditionGtNode>,
72 public Visitor<ConditionGeqNode>,
73 public Visitor<ConditionNotNode>,
74 public Visitor<ConditionAndNode>,
75 public Visitor<ConditionOrNode>,
76 public Visitor<IfThenElseNode>,
77 public Visitor<LoopNode> {
78public:
79 ASTPrinter(const bool printLocationInfo) : tabsize_(2), printLocationInfo_(printLocationInfo), indent_(0) {}
80 std::string str() const { return out_.str(); }
81 void visit(ASTNode& n) override { print("Unknown", n); }
82 void visit(OperatorPlusNode& n) override { print("OperatorPlus", n); }
83 void visit(OperatorMinusNode& n) override { print("OperatorMinus", n); }
84 void visit(OperatorMultiplyNode& n) override { print("OperatorMultiply", n); }
85 void visit(OperatorDivideNode& n) override { print("OperatorDivide", n); }
86 void visit(NegateNode& n) override { print("Negate", n); }
87 void visit(FunctionAbsNode& n) override { print("FunctionAbs", n); }
88 void visit(FunctionExpNode& n) override { print("FunctionExp", n); }
89 void visit(FunctionLogNode& n) override { print("FunctionLog", n); }
90 void visit(FunctionSqrtNode& n) override { print("FunctionSqrt", n); }
91 void visit(FunctionNormalCdfNode& n) override { print("FunctionNormalCdf", n); }
92 void visit(FunctionNormalPdfNode& n) override { print("FunctionNormalPdf", n); }
93 void visit(FunctionMinNode& n) override { print("FunctionMin", n); }
94 void visit(FunctionMaxNode& n) override { print("FunctionMax", n); }
95 void visit(FunctionPowNode& n) override { print("FunctionPow", n); }
96 void visit(FunctionBlackNode& n) override { print("FunctionBlack", n); }
97 void visit(FunctionDcfNode& n) override { print("FunctionDcf", n); }
98 void visit(FunctionDaysNode& n) override { print("FunctionDays", n); }
99 void visit(FunctionPayNode& n) override { print("FunctionPay", n); }
100 void visit(FunctionLogPayNode& n) override { print("FunctionLogPay", n); }
101 void visit(FunctionNpvNode& n) override { print("FunctionNpv", n); }
102 void visit(FunctionNpvMemNode& n) override { print("FunctionNpvMem", n); }
103 void visit(HistFixingNode& n) override { print("HistFixing", n); }
104 void visit(FunctionDiscountNode& n) override { print("FunctionDiscount", n); }
105 void visit(FunctionFwdCompNode& n) override { print("FunctionFwdComp", n); }
106 void visit(FunctionFwdAvgNode& n) override { print("FunctionFwdAvg", n); }
107 void visit(FunctionAboveProbNode& n) override { print("FunctionAboveProb", n); }
108 void visit(FunctionBelowProbNode& n) override { print("FunctionBelowProb", n); }
109 void visit(FunctionDateIndexNode& n) override { print("FunctionDateIndex(" + n.name + "," + n.op + ")", n); }
110 void visit(SortNode& n) override { print("Sort", n); }
111 void visit(PermuteNode& n) override { print("Permute", n); }
112 void visit(ConstantNumberNode& n) override { print("ConstantNumber(" + std::to_string(n.value) + ")", n); }
113 void visit(VariableNode& n) override { print("Variable(" + n.name + ")", n); }
114 void visit(SizeOpNode& n) override { print("Size(" + n.name + ")", n); }
115 void visit(VarEvaluationNode& n) override { print("VarEvaluation", n); }
116 void visit(AssignmentNode& n) override { print("Assignment", n); }
117 void visit(RequireNode& n) override { print("Require", n); }
118 void visit(DeclarationNumberNode& n) override { print("DeclarationNumber", n); }
119 void visit(SequenceNode& n) override { print("Sequence", n); }
120 void visit(ConditionEqNode& n) override { print("ConditionEq", n); }
121 void visit(ConditionNeqNode& n) override { print("ConditionNeq", n); }
122 void visit(ConditionLtNode& n) override { print("ConditionLt", n); }
123 void visit(ConditionLeqNode& n) override { print("ConditionLeq", n); }
124 void visit(ConditionGtNode& n) override { print("ConditionGt", n); }
125 void visit(ConditionGeqNode& n) override { print("ConditionGeq", n); }
126 void visit(ConditionNotNode& n) override { print("ConditionNot", n); }
127 void visit(ConditionAndNode& n) override { print("ConditionAnd", n); }
128 void visit(ConditionOrNode& n) override { print("ConditionOr", n); }
129 void visit(IfThenElseNode& n) override { print("IfThenElse", n); }
130 void visit(LoopNode& n) override { print("Loop(" + n.name + ")", n); }
131
132private:
133 void print(const std::string& s, const ASTNode& n) {
134 out_ << std::string(indent_, ' ') << s;
135 if (printLocationInfo_)
136 out_ << " at " + to_string(n.locationInfo);
137 out_ << '\n';
138 for (auto const& c : n.args) {
139 indent_ += tabsize_;
140 if (c == nullptr)
141 out_ << std::string(indent_, ' ') << "-\n";
142 else {
143 c->accept(*this);
144 }
145 indent_ -= tabsize_;
146 }
147 }
148 //
149 const Size tabsize_;
151 //
153 std::ostringstream out_;
154};
155} // namespace
156
157std::string to_string(const ASTNodePtr root, const bool printLocationInfo) {
158 ASTPrinter p(printLocationInfo);
159 root->accept(p);
160 return p.str();
161}
162
163} // namespace data
164} // namespace ore
Size indent_
Definition: astprinter.cpp:152
const Size tabsize_
Definition: astprinter.cpp:149
std::ostringstream out_
Definition: astprinter.cpp:153
const bool printLocationInfo_
Definition: astprinter.cpp:150
ast printer
@ data
Definition: log.hpp:77
void print(Distribution &dist, std::string fileName)
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
QuantLib::ext::shared_ptr< ASTNode > ASTNodePtr
Definition: ast.hpp:46
Serializable Credit Default Swap.
Definition: namespaces.docs:23