]> www.ginac.de Git - ginac.git/blob - ginac/parser/parse_context.cpp
d9790276a0c273ea118e3d51e7367d3a219b5f02
[ginac.git] / ginac / parser / parse_context.cpp
1 /** @file parse_context.cpp
2  *
3  *  Implementation of the parser context. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2009 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #include "parse_context.h"
24
25 #include "function.h"
26
27 #include <sstream>
28 #include <stdexcept>
29
30 namespace GiNaC {
31
32 symbol
33 find_or_insert_symbol(const std::string& name, symtab& syms, const bool strict)
34 {
35         symtab::const_iterator p = syms.find(name);
36         if (p != syms.end()) {
37                 if (is_a<symbol>(p->second))
38                         return ex_to<symbol>(p->second);
39                 else
40                         throw std::invalid_argument(
41                                 std::string("find_or_insert_symbol: name \"")
42                                 + name + "\" does not correspond to a symbol");
43         }
44
45
46         if (strict)
47                 throw std::invalid_argument(
48                                 std::string("find_or_insert_symbol: symbol \"") 
49                                 + name + "\" not found");
50
51         const symbol sy(name);
52         syms[name] = sy;
53         return sy;
54 }
55
56 const prototype_table& get_default_reader(bool force_init)
57 {
58         using std::make_pair;
59         static bool initialized = false;
60         static prototype_table reader;
61         if ( !initialized || force_init ) {
62                 std::vector<function_options> flist = function::get_registered_functions();
63                 std::vector<function_options>::iterator i = flist.begin(), end = flist.end();
64                 for ( ; i != end; ++i ) {
65                         std::string name = i->get_name();
66                         unsigned narg = i->get_nparams();
67                         reader[make_pair(name, narg)] = function::find_function(name, narg);
68                 }
69                 initialized = true;
70         }
71         return reader;
72 }
73
74 } // namespace GiNaC