]> www.ginac.de Git - ginac.git/commitdiff
parser: map input strings onto arbitrary expressions (not only symbols).
authorAlexei Sheplyakov <varg@theor.jinr.ru>
Sat, 13 Sep 2008 00:30:22 +0000 (04:30 +0400)
committerAlexei Sheplyakov <varg@theor.jinr.ru>
Fri, 19 Sep 2008 09:15:49 +0000 (13:15 +0400)
So it's possible to make abbreviations, e.g.

symbol x;
symtab table;
table["x"] = x + log(x) + 1;
parser reader(table);
ex e = reader("1 + x^2 + 5*x^3");

ginac/parser/parse_context.cpp
ginac/parser/parse_context.hpp

index e4956ba74d4510d234beeee023722b2c7240a2f9..1dd767778f9581cea6ad0b6e8790d400b38733c8 100644 (file)
@@ -4,23 +4,28 @@
 namespace GiNaC
 {
 
-const symbol&
+symbol
 find_or_insert_symbol(const std::string& name, symtab& syms, const bool strict)
 {
        symtab::const_iterator p = syms.find(name);
-       if (p != syms.end())
-               return p->second.first;
+       if (p != syms.end()) {
+               if (is_a<symbol>(p->second))
+                       return ex_to<symbol>(p->second);
+               else
+                       throw std::invalid_argument(
+                               std::string("find_or_insert_symbol: name \"")
+                               + name + "\" does not correspond to a symbol");
+       }
+
 
        if (strict)
                throw std::invalid_argument(
                                std::string("find_or_insert_symbol: symbol \"") 
                                + name + "\" not found");
 
-       // false means this symbol was created by parser 
-       const std::pair<symbol, bool> tmp = std::make_pair(symbol(name), false);
-
-       symtab::iterator i = syms.insert(symtab::value_type(name, tmp)).first;
-       return i->second.first;
+       const symbol sy(name);
+       syms[name] = sy;
+       return sy;
 }
 
 }
index d0a3d55870839b627a981ef113f97a51e82291a7..efcb6c620f309f0413f9f4e1279164eb4eda1c09 100644 (file)
@@ -11,14 +11,11 @@ namespace GiNaC
 {
 
 /**
- * Establishes correspondence between the strings and symbols.
+ * Establishes correspondence between the strings and expressions.
  * The parser will create missing symbols (if not instructed otherwise,
  * in which case it fails if the expression contains unknown symbols).
- * The .second element of pair helps to distinguish between the user
- * supplied symbols and parser generated ones. The .first is the symbol
- * itself
  */
-typedef std::map<std::string, std::pair<symbol, bool> > symtab;
+typedef std::map<std::string, ex> symtab;
 
 /**
  * Find the symbol with the @a name in the symbol table @a syms.
@@ -26,7 +23,7 @@ typedef std::map<std::string, std::pair<symbol, bool> > symtab;
  * If symbol is missing and @a strict = false, insert it, otherwise
  * throw an exception.
  */
-extern const symbol&
+extern symbol
 find_or_insert_symbol(const std::string& name, symtab& syms,
                      const bool strict);