]> www.ginac.de Git - ginac.git/blobdiff - ginac/parser/parser.cpp
Fixed memory leak.
[ginac.git] / ginac / parser / parser.cpp
index b0d156e8a46b7b2f1ee508777bf72e13e47f4477..6ed364ffd5a155ae0bb5abeec567a8f17e3ffbaf 100644 (file)
@@ -1,13 +1,36 @@
-#include <stdexcept>
-#include <sstream>
+/** @file parser.cpp
+ *
+ *  Implementation of GiNaC's parser. */
+
+/*
+ *  GiNaC Copyright (C) 1999-2009 Johannes Gutenberg University Mainz, Germany
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
 #include "parser.h"
 #include "lexer.h"
 #include "debug.h"
 #include "mul.h"
 #include "constant.h"
+#include "function.h"
 
-namespace GiNaC 
-{
+#include <sstream>
+#include <stdexcept>
+
+namespace GiNaC {
 
 /// identifier_expr:  identifier |  identifier '(' expression* ')'
 ex parser::parse_identifier_expr()
@@ -43,8 +66,18 @@ ex parser::parse_identifier_expr()
                Parse_error_("no function \"" << name << "\" with " <<
                             args.size() << " arguments");
        }
-       ex ret = reader->second(args);
-       return ret;
+       // dirty hack to distinguish between serial numbers of functions and real
+       // pointers.
+       GiNaC::function* f = NULL;
+       try {
+               f = new GiNaC::function(reinterpret_cast<unsigned>(reader->second), args);
+       }
+       catch ( std::runtime_error ) {
+               if ( f ) delete f;
+               ex ret = reader->second(args);
+               return ret;
+       }
+       return f->setflag(status_flags::dynallocated);
 }
 
 /// paren_expr:  '(' expression ')'