X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fparser%2Fparser.cpp;h=992b151079c60aee8daaf6a632c4cbff1f5486a3;hp=6411ba1416e740a85705ceee3153815b33e9364f;hb=4ec372a00b1a13e6374ce880cbd68c2e14114dad;hpb=1261c54df6548cf558405a118b2134805f63376d diff --git a/ginac/parser/parser.cpp b/ginac/parser/parser.cpp index 6411ba14..992b1510 100644 --- a/ginac/parser/parser.cpp +++ b/ginac/parser/parser.cpp @@ -1,13 +1,72 @@ -#include -#include -#include "parser.hpp" -#include "lexer.hpp" -#include "debug.hpp" +/** @file parser.cpp + * + * Implementation of GiNaC's parser. */ + +/* + * GiNaC Copyright (C) 1999-2011 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" +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDINT_H +#include // for uintptr_t +#endif +#include +#include + +namespace GiNaC { -namespace GiNaC +// +// Find out if ptr is a pointer to a function or a specially crafted integer. +// It's possible to distinguish between these because functions are aligned. +// Returns true if ptr is a pointer and false otherwise. +static bool decode_serial(unsigned& serial, const reader_func ptr) { + uintptr_t u = (uintptr_t)(void *)ptr; + if (u & 1) { + u >>= 1; + serial = (unsigned)u; + return false; + } + return true; +} + +// Figures out if ptr is a pointer to function or a serial of GiNaC function. +// In the former case calls that function, in the latter case constructs +// GiNaC function with corresponding serial and arguments. +static ex dispatch_reader_fcn(const reader_func ptr, const exvector& args) +{ + unsigned serial = 0; // dear gcc, could you please shut up? + bool is_ptr = decode_serial(serial, ptr); + if (is_ptr) + return ptr(args); + else + return function(serial, args); +} +// + /// identifier_expr: identifier | identifier '(' expression* ')' ex parser::parse_identifier_expr() @@ -43,7 +102,9 @@ ex parser::parse_identifier_expr() Parse_error_("no function \"" << name << "\" with " << args.size() << " arguments"); } - ex ret = reader->second(args); + // reader->second might be a pointer to a C++ function or a specially + // crafted serial of a GiNaC::function. + ex ret = dispatch_reader_fcn(reader->second, args); return ret; } @@ -59,8 +120,7 @@ ex parser::parse_paren_expr() return e; } -extern numeric* _num_1_p; -extern ex _ex0; +extern const ex _ex0; /// unary_expr: [+-] expression ex parser::parse_unary_expr()