]> www.ginac.de Git - ginac.git/blob - ginac/parser/parser_compat.cpp
Happy new year!
[ginac.git] / ginac / parser / parser_compat.cpp
1 /** @file parser_compat.cpp
2  *
3  * Parser interface compatible with the old (bison/flex based) parser. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2015 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 "ex.h"
24 #include "idx.h"
25 #include "lst.h"
26 #include "parser.h"
27
28 #include <iostream>
29 #include <string>
30
31 namespace GiNaC {
32
33 static symtab make_symtab(const ex& l);
34
35 ptr<basic> ex::construct_from_string_and_lst(const std::string &s, const ex &l)
36 {
37         static const bool strict = true;
38         symtab syms = make_symtab(l);
39         parser reader(syms, strict); 
40         ex parsed_ex = reader(s);
41         return parsed_ex.bp;
42 }
43
44 static std::string get_symbol_name(const ex & s);
45
46 static symtab make_symtab(const ex& l)
47 {
48         symtab syms;
49         if (is_exactly_a<lst>(l)) {
50                 for (std::size_t i = 0; i < l.nops(); i++) {
51                         const ex &o = l.op(i);
52                         if (is_a<symbol>(o) || (is_a<idx>(o) && is_a<symbol>(o.op(0))))
53                                 syms[get_symbol_name(o)] = o;
54                 }
55         }
56         return syms;
57 }
58
59 static std::string get_symbol_name(const ex & s)
60 {
61         if (is_a<symbol>(s))
62                 return ex_to<symbol>(s).get_name();
63         else if (is_a<idx>(s) && is_a<symbol>(s.op(0)))
64                 return ex_to<symbol>(s.op(0)).get_name();
65         else
66                 throw (std::invalid_argument("get_symbol_name(): unexpected expression type"));
67 }
68
69 } // namespace GiNaC