]> www.ginac.de Git - ginac.git/blob - ginac/parser/parser_compat.cpp
Use the new parser in the ex(const string&, lst&) ctor.
[ginac.git] / ginac / parser / parser_compat.cpp
1 /// @file parser_compat.cpp Parser interface compatible with the old
2 ///       (bison/flex based) parser.
3 #include "ex.h"
4 #include "idx.h"
5 #include "lst.h"
6 #include "parser.hpp"
7 #include <string>
8 #include <iostream>
9
10 namespace GiNaC
11 {
12 static symtab make_symtab(const ex& l);
13
14 ptr<basic> ex::construct_from_string_and_lst(const std::string &s, const ex &l)
15 {
16         static const bool strict = true;
17         symtab syms = make_symtab(l);
18         parser reader(syms, strict); 
19         ex parsed_ex = reader(s);
20         return parsed_ex.bp;
21 }
22
23 static std::string get_symbol_name(const ex & s);
24
25 static symtab make_symtab(const ex& l)
26 {
27         symtab syms;
28         if (is_exactly_a<lst>(l)) {
29                 for (std::size_t i = 0; i < l.nops(); i++) {
30                         const ex &o = l.op(i);
31                         if (is_a<symbol>(o) || (is_a<idx>(o) && is_a<symbol>(o.op(0))))
32                                 syms[get_symbol_name(o)] = o;
33                 }
34         }
35         return syms;
36 }
37
38 static std::string get_symbol_name(const ex & s)
39 {
40         if (is_a<symbol>(s))
41                 return ex_to<symbol>(s).get_name();
42         else if (is_a<idx>(s) && is_a<symbol>(s.op(0)))
43                 return ex_to<symbol>(s.op(0)).get_name();
44         else
45                 throw (std::invalid_argument("get_symbol_name(): unexpected expression type"));
46 }
47
48 }
49