X-Git-Url: https://ginac.de/ginac.git//ginac.git?a=blobdiff_plain;f=ginac%2Fparser%2Fparser.cpp;h=6eef040dbe24fb8ba523cfaeb44eb10ba80abba7;hb=63f3e977f92d51ea173382a9b7c4c3b18bda7b8e;hp=cfa313b4f7ba08659f512e1a3c8607b85001430d;hpb=14aeeca161c7cc2d145b0778ac234341068efef1;p=ginac.git diff --git a/ginac/parser/parser.cpp b/ginac/parser/parser.cpp index cfa313b4..6eef040d 100644 --- a/ginac/parser/parser.cpp +++ b/ginac/parser/parser.cpp @@ -3,7 +3,7 @@ * Implementation of GiNaC's parser. */ /* - * GiNaC Copyright (C) 1999-2009 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2018 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 @@ -21,17 +21,49 @@ */ #include "parser.h" +#include "lst.h" #include "lexer.h" #include "debug.h" #include "mul.h" #include "constant.h" #include "function.h" +#include // for uintptr_t #include #include 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() { @@ -61,24 +93,15 @@ ex parser::parse_identifier_expr() // Eat the ')'. get_next_tok(); prototype the_prototype = make_pair(name, args.size()); - prototype_table::const_iterator reader = funcs.find(the_prototype); + auto reader = funcs.find(the_prototype); if (reader == funcs.end()) { Parse_error_("no function \"" << name << "\" with " << args.size() << " arguments"); } - // dirty hack to distinguish between serial numbers of functions and real - // pointers. - GiNaC::function* f = NULL; - try { - unsigned serial = (unsigned)(unsigned long)(void *)(reader->second); - f = new GiNaC::function(serial, args); - } - catch ( std::runtime_error ) { - if ( f ) delete f; - ex ret = reader->second(args); - return ret; - } - return f->setflag(status_flags::dynallocated); + // 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; } /// paren_expr: '(' expression ')' @@ -93,8 +116,35 @@ ex parser::parse_paren_expr() return e; } -extern numeric* _num_1_p; -extern ex _ex0; +/// lst_expr: '{' expression { ',' expression } '}' +ex parser::parse_lst_expr() +{ + get_next_tok(); // eat {. + + lst list; + if (token != '}') { + while (true) { + ex e = parse_expression(); // expression(); + list.append(e); + + if (token == '}') { + break; + } + + if (token != ',') { + Parse_error("expected '}'"); + } + + get_next_tok(); // eat ','. + } + } + // Eat the '}'. + get_next_tok(); + + return list; +} + +extern const ex _ex0; /// unary_expr: [+-] expression ex parser::parse_unary_expr() @@ -108,7 +158,7 @@ ex parser::parse_unary_expr() // -(a) // +a // +(a) - // Delegete the work to parse_binop_rhs(), otherwise we end up + // Delegate the work to parse_binop_rhs(), otherwise we end up // duplicating it here. ex lhs = _ex0; // silly trick ex e = parse_binop_rhs(0, lhs); @@ -125,6 +175,8 @@ ex parser::parse_primary() return parse_number_expr(); case '(': return parse_paren_expr(); + case '{': + return parse_lst_expr(); case '-': case '+': return parse_unary_expr();