1 /** @file default_reader.cpp
3 * Implementation of the default and builtin readers (part of GiNaC's parser).
7 * GiNaC Copyright (C) 1999-2017 Johannes Gutenberg University Mainz, Germany
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.
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.
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
24 #include "parse_context.h"
27 #include "operators.h"
30 #include <cstdint> // for uintptr_t
35 static ex sqrt_reader(const exvector& ev)
37 return GiNaC::sqrt(ev[0]);
40 static ex pow_reader(const exvector& ev)
42 return GiNaC::pow(ev[0], ev[1]);
45 static ex power_reader(const exvector& ev)
47 return GiNaC::power(ev[0], ev[1]);
50 static ex lst_reader(const exvector& ev)
52 return GiNaC::lst(ev.begin(), ev.end());
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
62 static const std::vector<function_options>& get_registered_functions()
64 return function::registered_functions();
67 registered_functions_hack();
68 registered_functions_hack(const registered_functions_hack&);
69 registered_functions_hack& operator=(const registered_functions_hack&);
72 // Encode an integer into a pointer to a function. Since functions
73 // are aligned (the minimal alignment depends on CPU architecture)
74 // we can distinguish between pointers and integers.
75 static reader_func encode_serial_as_reader_func(unsigned serial)
77 uintptr_t u = (uintptr_t)serial;
78 u = (u << 1) | (uintptr_t)1;
79 reader_func ptr = (reader_func)((void *)u);
83 const prototype_table& get_default_reader()
86 static bool initialized = false;
87 static prototype_table reader;
90 reader[make_pair("sqrt", 1)] = sqrt_reader;
91 reader[make_pair("pow", 2)] = pow_reader;
92 reader[make_pair("power", 2)] = power_reader;
93 reader[make_pair("lst", 0)] = lst_reader;
95 for (auto & it : registered_functions_hack::get_registered_functions()) {
96 prototype proto = make_pair(it.get_name(), it.get_nparams());
97 reader[proto] = encode_serial_as_reader_func(serial);
105 const prototype_table& get_builtin_reader()
107 using std::make_pair;
108 static bool initialized = false;
109 static prototype_table reader;
112 reader[make_pair("sqrt", 1)] = sqrt_reader;
113 reader[make_pair("pow", 2)] = pow_reader;
114 reader[make_pair("power", 2)] = power_reader;
115 reader[make_pair("lst", 0)] = lst_reader;
146 auto it = registered_functions_hack::get_registered_functions().begin();
148 for ( ; serial<NFUNCTIONS; ++it, ++serial ) {
149 prototype proto = make_pair(it->get_name(), it->get_nparams());
150 reader[proto] = encode_serial_as_reader_func(serial);