]> www.ginac.de Git - ginac.git/blob - ginac/parser/default_reader.cpp
[BUGFIX] Fix crash in parser.
[ginac.git] / ginac / parser / default_reader.cpp
1 /** @file default_reader.cpp
2  *
3  *  Implementation of the default and builtin readers (part of GiNaC's parser).
4  **/
5
6 /*
7  *  GiNaC Copyright (C) 1999-2024 Johannes Gutenberg University Mainz, Germany
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  */
23
24 #include "parse_context.h"
25 #include "power.h"
26 #include "lst.h"
27 #include "operators.h"
28 #include "inifcns.h"
29
30 #include <cstdint> // for uintptr_t
31
32 namespace GiNaC
33 {
34
35 static ex sqrt_reader(const exvector& ev)
36 {
37         return GiNaC::sqrt(ev[0]);
38 }
39
40 static ex pow_reader(const exvector& ev)
41 {
42         return GiNaC::pow(ev[0], ev[1]);
43 }
44
45 static ex power_reader(const exvector& ev)
46 {
47         return GiNaC::power(ev[0], ev[1]);
48 }
49
50 static ex lst_reader(const exvector& ev)
51 {
52         return GiNaC::lst(ev.begin(), ev.end());
53 }
54
55
56 // function::registered_functions() is protected, but we need to access it
57 // TODO: add a proper const method to the `function' class, so we don't
58 // need this silly hack any more.
59 class registered_functions_hack : public function
60 {
61 public:
62         static const std::vector<function_options>& get_registered_functions()
63         {
64                 return function::registered_functions();
65         }
66 private:
67         registered_functions_hack();
68         registered_functions_hack(const registered_functions_hack&);
69         registered_functions_hack& operator=(const registered_functions_hack&);
70 };
71
72 const prototype_table& get_default_reader()
73 {
74         static bool initialized = false;
75         static prototype_table reader;
76         if (!initialized) {
77
78                 reader.insert({{"sqrt", 1}, reader_func(sqrt_reader)});
79                 reader.insert({{"pow", 2}, reader_func(pow_reader)});
80                 reader.insert({{"power", 2}, reader_func(power_reader)});
81                 reader.insert({{"lst", 0}, reader_func(lst_reader)});
82                 unsigned serial = 0;
83                 for (auto & it : registered_functions_hack::get_registered_functions()) {
84                         reader.insert({{it.get_name(), it.get_nparams()},
85                                        reader_func(serial)});
86                         ++serial;
87                 }
88                 initialized = true;
89         }
90         return reader;
91 }
92
93 const prototype_table& get_builtin_reader()
94 {
95         static bool initialized = false;
96         static prototype_table reader;
97         if (!initialized) {
98
99                 reader.insert({{"sqrt", 1}, reader_func(sqrt_reader)});
100                 reader.insert({{"pow", 2}, reader_func(pow_reader)});
101                 reader.insert({{"power", 2}, reader_func(power_reader)});
102                 reader.insert({{"lst", 0}, reader_func(lst_reader)});
103                 enum {
104                         log,
105                         exp,
106                         sin,
107                         cos,
108                         tan,
109                         asin,
110                         acos,
111                         atan,
112                         sinh,
113                         cosh,
114                         tanh,
115                         asinh,
116                         acosh,
117                         atanh,
118                         atan2,
119                         Li2,
120                         Li3,
121                         zetaderiv,
122                         Li,
123                         S,
124                         H,
125                         lgamma,
126                         tgamma,
127                         beta,
128                         factorial,
129                         binomial,
130                         Order,
131                         NFUNCTIONS
132                 };
133                 auto it = registered_functions_hack::get_registered_functions().begin();
134                 unsigned serial = 0;
135                 for ( ; serial<NFUNCTIONS; ++it, ++serial ) {
136                         reader.insert({{it->get_name(), it->get_nparams()},
137                                        reader_func(serial)});
138                 }
139                 initialized = true;
140         }
141         return reader;
142 }
143
144 } // namespace GiNaC