Loading...
Searching...
No Matches
exprbase.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2023, John Wiegley. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of New Artisans LLC nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
54#pragma once
55
56#include "utils.h"
57#include "amount.h"
58
59namespace ledger {
60
61DECLARE_EXCEPTION(parse_error, std::runtime_error);
62DECLARE_EXCEPTION(compile_error, std::runtime_error);
63DECLARE_EXCEPTION(calc_error, std::runtime_error);
64DECLARE_EXCEPTION(usage_error, std::runtime_error);
65
66class scope_t;
67class call_scope_t;
68
69template <typename ResultType>
71{
72public:
74
76
77protected:
79 string str;
81
82 virtual result_type real_calc(scope_t& scope) = 0;
83
84public:
94 virtual ~expr_base_t() {
96 }
97
99 if (this != &_expr) {
100 str = _expr.str;
101 context = _expr.context;
102 compiled = _expr.compiled;
103 }
104 return *this;
105 }
106 expr_base_t& operator=(const string& _expr) {
107 parse(_expr);
108 return *this;
109 }
110
111 virtual operator bool() const throw() {
112 return ! str.empty();
113 }
114
115 virtual string text() const throw() {
116 return str;
117 }
118 void set_text(const string& txt) {
119 str = txt;
120 compiled = false;
121 }
122
123 void parse(const string& expr_str,
124 const parse_flags_t& flags = PARSE_DEFAULT) {
125 std::istringstream stream(expr_str);
126 return parse(stream, flags, expr_str);
127 }
128 virtual void parse(std::istream&,
132 }
133
134 virtual void mark_uncompiled() {
135 compiled = false;
136 }
137
138 void recompile(scope_t& scope) {
139 compiled = false;
140 compile(scope);
141 }
142
143 virtual void compile(scope_t& scope) {
144 if (! compiled) {
145 // Derived classes need to do something here.
146 context = &scope;
147 compiled = true;
148 }
149 }
150
152 return calc(scope);
153 }
154
156 {
157 if (! compiled) {
158#if DEBUG_ON
159 if (SHOW_DEBUG("expr.compile")) {
160 DEBUG("expr.compile", "Before compilation:");
162 }
163#endif // DEBUG_ON
164
165 DEBUG("expr.compile", "Compiling: " << str);
166 compile(scope);
167
168#if DEBUG_ON
169 if (SHOW_DEBUG("expr.compile")) {
170 DEBUG("expr.compile", "After compilation:");
172 }
173#endif // DEBUG_ON
174 }
175
176 DEBUG("expr.calc", "Calculating: " << str);
177 return real_calc(scope);
178 }
179
182 return calc(*context);
183 }
184
186 return context;
187 }
188 void set_context(scope_t * scope) {
189 context = scope;
190 }
191
192 virtual string context_to_str() const {
193 return empty_string;
194 }
195
196 string print_to_str() const {
197 std::ostringstream out;
198 print(out);
199 return out.str();
200 }
201 string dump_to_str() const {
202 std::ostringstream out;
203 dump(out);
204 return out.str();
205 }
206 string preview_to_str(scope_t&) const {
207 std::ostringstream out;
208 preview(out);
209 return out.str();
210 }
211
212 virtual void print(std::ostream&) const {}
213 virtual void dump(std::ostream&) const {}
214
215 result_type preview(std::ostream& out, scope_t& scope) const {
216 out << _("--- Input expression ---") << std::endl;
217 out << text() << std::endl;
218
219 out << std::endl << _("--- Text as parsed ---") << std::endl;
220 print(out);
221 out << std::endl;
222
223 out << std::endl << _("--- Expression tree ---") << std::endl;
224 dump(out);
225
226 out << std::endl << _("--- Compiled tree ---") << std::endl;
227 compile(scope);
228 dump(out);
229
230 out << std::endl << _("--- Result value ---") << std::endl;
231 return calc();
232 }
233};
234
235template <typename ResultType>
236std::ostream& operator<<(std::ostream& out,
237 const expr_base_t<ResultType>& expr) {
238 expr.print(out);
239 return out;
240}
241
242} // namespace ledger
General utility facilities used by Ledger.
#define SHOW_DEBUG(cat)
Definition utils.h:318
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
#define DEBUG(cat, msg)
Definition utils.h:327
#define assert(x)
Definition utils.h:92
Basic type for handling commoditized math: amount_t.
#define DECLARE_EXCEPTION(name, kind)
Definition error.h:88
string empty_string
std::ostream & operator<<(std::ostream &out, const account_t &account)
T & downcast(U &object)
Definition utils.h:468
@ PARSE_DEFAULT
Definition amount.h:68
string preview_to_str(scope_t &) const
Definition exprbase.h:206
virtual void parse(std::istream &, const parse_flags_t &=PARSE_DEFAULT, const optional< string > &original_string=none)
Definition exprbase.h:128
void recompile(scope_t &scope)
Definition exprbase.h:138
function< result_type(call_scope_t &) func_t)
Definition exprbase.h:75
void set_context(scope_t *scope)
Definition exprbase.h:188
result_type calc(scope_t &scope)
Definition exprbase.h:155
expr_base_t & operator=(const expr_base_t &_expr)
Definition exprbase.h:98
expr_base_t(scope_t *_context=NULL)
Definition exprbase.h:89
virtual void dump(std::ostream &) const
Definition exprbase.h:213
scope_t * get_context()
Definition exprbase.h:185
scope_t * context
Definition exprbase.h:78
virtual result_type real_calc(scope_t &scope)=0
virtual string context_to_str() const
Definition exprbase.h:192
virtual void mark_uncompiled()
Definition exprbase.h:134
string print_to_str() const
Definition exprbase.h:196
result_type operator()(scope_t &scope)
Definition exprbase.h:151
result_type calc()
Definition exprbase.h:180
result_type preview(std::ostream &out, scope_t &scope) const
Definition exprbase.h:215
virtual string text() const
Definition exprbase.h:115
string dump_to_str() const
Definition exprbase.h:201
expr_base_t(const expr_base_t &other)
Definition exprbase.h:85
virtual ~expr_base_t()
Definition exprbase.h:94
virtual void print(std::ostream &) const
Definition exprbase.h:212
void set_text(const string &txt)
Definition exprbase.h:118
ResultType result_type
Definition exprbase.h:73
virtual void compile(scope_t &scope)
Definition exprbase.h:143
expr_base_t & operator=(const string &_expr)
Definition exprbase.h:106
void parse(const string &expr_str, const parse_flags_t &flags=PARSE_DEFAULT)
Definition exprbase.h:123