Loading...
Searching...
No Matches
expr.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
42#pragma once
43
44#include "exprbase.h"
45#include "value.h"
46
47namespace ledger {
48
49class expr_t : public expr_base_t<value_t>
50{
51 class parser_t;
53
54public:
55 struct token_t;
56 class op_t;
59
65
66 typedef std::pair<expr_t, check_expr_kind_t> check_expr_pair;
67 typedef std::list<check_expr_pair> check_expr_list;
68
69protected:
71
72public:
76
77 expr_t(const string& _str, const parse_flags_t& flags = PARSE_DEFAULT);
78 expr_t(std::istream& in, const parse_flags_t& flags = PARSE_DEFAULT);
79
80 virtual ~expr_t();
81
83
84 virtual operator bool() const throw();
85
87
88 void parse(const string& str, const parse_flags_t& flags = PARSE_DEFAULT) {
89 std::istringstream stream(str);
90 return parse(stream, flags, str);
91 }
92
93 virtual void parse(std::istream& in,
94 const parse_flags_t& flags = PARSE_DEFAULT,
96 virtual void compile(scope_t& scope);
97 virtual value_t real_calc(scope_t& scope);
98
99 bool is_constant() const;
101 const value_t& constant_value() const;
102 bool is_function() const;
104
105 virtual string context_to_str() const;
106 virtual void print(std::ostream& out) const;
107 virtual void dump(std::ostream& out) const;
108};
109
113inline bool is_expr(const value_t& val) {
114 return val.is_any() && val.as_any().type() == typeid(expr_t::ptr_op_t);
115}
116
120
121// A merged expression allows one to set an expression term, "foo", and
122// a base expression, "bar", and then merge in later expressions that
123// utilize foo. For example:
124//
125// foo: bar
126// merge: foo * 10
127// merge: foo + 20
128//
129// When this expression is finally compiled, the base and merged
130// elements are written into this:
131//
132// __tmp=(foo=bar; foo=foo*10; foo=foo+20);__tmp
133//
134// This allows users to select flags like -O, -B or -I at any time, and
135// also combine flags such as -V and -A.
136
137class merged_expr_t : public expr_t
138{
139public:
140 string term;
141 string base_expr;
143
144 std::list<string> exprs;
145
146 merged_expr_t(const string& _term, const string& expr,
147 const string& merge_op = ";")
149 TRACE_CTOR(merged_expr_t, "string, string, string");
150 }
151 virtual ~merged_expr_t() {
153 }
154
155 void set_term(const string& _term) {
156 term = _term;
157 }
158 void set_base_expr(const string& expr) {
159 base_expr = expr;
160 }
161 void set_merge_operator(const string& merge_op) {
163 }
164
165 bool check_for_single_identifier(const string& expr);
166
167 void prepend(const string& expr) {
168 if (! check_for_single_identifier(expr))
169 exprs.push_front(expr);
170 }
171 void append(const string& expr) {
172 if (! check_for_single_identifier(expr))
173 exprs.push_back(expr);
174 }
175 void remove(const string& expr) {
176 exprs.remove(expr);
177 }
178
179 virtual void compile(scope_t& scope);
180};
181
182class call_scope_t;
184
185} // namespace ledger
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
Abstract dynamic type representing various numeric types.
expr_t::ptr_op_t as_expr(const value_t &val)
value_t source_command(call_scope_t &scope)
value_t expr_value(expr_t::ptr_op_t op)
T & downcast(U &object)
Definition utils.h:468
bool is_expr(const value_t &val)
Dealing with expr pointers tucked into value objects.
Definition expr.h:113
@ PARSE_DEFAULT
Definition amount.h:68
void set_expr(value_t &val, expr_t::ptr_op_t op)
virtual void parse(std::istream &in, const parse_flags_t &flags=PARSE_DEFAULT, const optional< string > &original_string=none)
expr_t(const string &_str, const parse_flags_t &flags=PARSE_DEFAULT)
intrusive_ptr< op_t > ptr_op_t
Definition expr.h:57
virtual void compile(scope_t &scope)
func_t & get_function()
value_t & constant_value()
virtual value_t real_calc(scope_t &scope)
bool is_function() const
void parse(const string &str, const parse_flags_t &flags=PARSE_DEFAULT)
Definition expr.h:88
intrusive_ptr< const op_t > const_ptr_op_t
Definition expr.h:58
check_expr_kind_t
Definition expr.h:60
@ EXPR_ASSERTION
Definition expr.h:62
@ EXPR_GENERAL
Definition expr.h:61
std::pair< expr_t, check_expr_kind_t > check_expr_pair
Definition expr.h:66
const value_t & constant_value() const
expr_t & operator=(const expr_t &_expr)
bool is_constant() const
ptr_op_t get_op()
ptr_op_t ptr
Definition expr.h:70
virtual void dump(std::ostream &out) const
std::list< check_expr_pair > check_expr_list
Definition expr.h:67
virtual void print(std::ostream &out) const
expr_t(ptr_op_t _ptr, scope_t *_context=NULL)
expr_t(const expr_t &other)
virtual ~expr_t()
virtual string context_to_str() const
expr_t(std::istream &in, const parse_flags_t &flags=PARSE_DEFAULT)
void append(const string &expr)
Definition expr.h:171
merged_expr_t(const string &_term, const string &expr, const string &merge_op=";")
Definition expr.h:146
std::list< string > exprs
Definition expr.h:144
string merge_operator
Definition expr.h:142
virtual ~merged_expr_t()
Definition expr.h:151
void set_term(const string &_term)
Definition expr.h:155
void remove(const string &expr)
Definition expr.h:175
bool check_for_single_identifier(const string &expr)
void set_merge_operator(const string &merge_op)
Definition expr.h:161
virtual void compile(scope_t &scope)
void prepend(const string &expr)
Definition expr.h:167
void set_base_expr(const string &expr)
Definition expr.h:158
function< result_type(call_scope_t &) func_t)
Definition exprbase.h:75
Dynamic type representing various numeric types.
Definition value.h:83