From: Alexei Sheplyakov Date: Sat, 13 Sep 2008 00:30:22 +0000 (+0400) Subject: parser: map input strings onto arbitrary expressions (not only symbols). X-Git-Tag: release_1-5-0~59^2~12 X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=commitdiff_plain;h=58a84c0adac7cbaa63a50c06815aed9c7a0bcdcc;hp=949508429ea9850b8ec36d862f4e4d291773c313 parser: map input strings onto arbitrary expressions (not only symbols). 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"); --- diff --git a/ginac/parser/parse_context.cpp b/ginac/parser/parse_context.cpp index e4956ba7..1dd76777 100644 --- a/ginac/parser/parse_context.cpp +++ b/ginac/parser/parse_context.cpp @@ -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(p->second)) + return ex_to(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 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; } } diff --git a/ginac/parser/parse_context.hpp b/ginac/parser/parse_context.hpp index d0a3d558..efcb6c62 100644 --- a/ginac/parser/parse_context.hpp +++ b/ginac/parser/parse_context.hpp @@ -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 > symtab; +typedef std::map symtab; /** * Find the symbol with the @a name in the symbol table @a syms. @@ -26,7 +23,7 @@ typedef std::map > 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);