]> www.ginac.de Git - ginac.git/blob - ginac/parser/parser.cpp
Fix the compliation error *for real* ... and restore performance
[ginac.git] / ginac / parser / parser.cpp
1 /** @file parser.cpp
2  *
3  *  Implementation of GiNaC's parser. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2009 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #include "parser.h"
24 #include "lexer.h"
25 #include "debug.h"
26 #include "mul.h"
27 #include "constant.h"
28 #include "function.h"
29
30 #include <sstream>
31 #include <stdexcept>
32
33 namespace GiNaC {
34
35 // <KLUDGE>
36 // Find out if ptr is a pointer to a function or a specially crafted integer.
37 // It's possible to distinguish between these because functions are aligned.
38 // Returns true if ptr is a pointer and false otherwise.
39 static bool decode_serial(unsigned& serial, const reader_func ptr)
40 {
41         uintptr_t u = (uintptr_t)(void *)ptr;
42         if (u & 1) {
43                 u >>= 1;
44                 serial = (unsigned)u;
45                 return false;
46         }
47         return true;
48 }
49
50 // Figures out if ptr is a pointer to function or a serial of GiNaC function.
51 // In the former case calls that function, in the latter case constructs
52 // GiNaC function with corresponding serial and arguments.
53 static ex dispatch_reader_fcn(const reader_func ptr, const exvector& args)
54 {
55         unsigned serial = 0; // dear gcc, could you please shut up?
56         bool is_ptr = decode_serial(serial, ptr);
57         if (is_ptr)
58                 return ptr(args);
59         else
60                 return function(serial, args);
61 }
62 // </KLUDGE>
63
64
65 /// identifier_expr:  identifier |  identifier '(' expression* ')'
66 ex parser::parse_identifier_expr()
67 {
68         std::string name = scanner->str;
69         get_next_tok();  // eat identifier.
70
71         if (token != '(') // symbol
72                 return find_or_insert_symbol(name, syms, strict);
73
74         // function/ctor call.
75         get_next_tok();  // eat (
76         exvector args;
77         if (token != ')') {
78                 while (true) {
79                         ex e = parse_expression();
80                         args.push_back(e);
81
82                         if (token == ')')
83                                 break;
84
85                         if (token != ',')
86                                 Parse_error("expected ')' or ',' in argument list");
87
88                         get_next_tok();
89                 }
90         }
91         // Eat the ')'.
92         get_next_tok();
93         prototype the_prototype = make_pair(name, args.size());
94         prototype_table::const_iterator reader = funcs.find(the_prototype);
95         if (reader == funcs.end()) {
96                 Parse_error_("no function \"" << name << "\" with " <<
97                              args.size() << " arguments");
98         }
99         // reader->second might be a pointer to a C++ function or a specially
100         // crafted serial of a GiNaC::function.
101         ex ret = dispatch_reader_fcn(reader->second, args);
102         return ret;
103 }
104
105 /// paren_expr:  '(' expression ')'
106 ex parser::parse_paren_expr()
107 {
108         get_next_tok();  // eat (.
109         ex e = parse_expression();
110
111         if (token != ')')
112                 Parse_error("expected ')'");
113         get_next_tok();  // eat ).
114         return e;
115 }
116
117 extern numeric* _num_1_p;
118 extern ex _ex0;
119
120 /// unary_expr: [+-] expression
121 ex parser::parse_unary_expr()
122 {
123         // Unlike most other parse_* method this one does NOT consume
124         // current token so parse_binop_rhs() knows what kind of operator
125         // is being parsed.
126         
127         // There are different kinds of expressions which need to be handled:
128         // -a+b 
129         // -(a) 
130         // +a
131         // +(a)
132         // Delegete the work to parse_binop_rhs(), otherwise we end up
133         // duplicating it here. 
134         ex lhs = _ex0; // silly trick
135         ex e = parse_binop_rhs(0, lhs);
136         return e;
137 }
138
139 /// primary: identifier_expr | number_expr | paren_expr | unary_expr 
140 ex parser::parse_primary() 
141 {
142         switch (token) {
143                 case lexer::token_type::identifier:
144                          return parse_identifier_expr();
145                 case lexer::token_type::number:
146                          return parse_number_expr();
147                 case '(': 
148                          return parse_paren_expr();
149                 case '-':
150                 case '+':
151                          return parse_unary_expr();
152                 case lexer::token_type::literal:
153                          return parse_literal_expr();
154                 case lexer::token_type::eof:
155                 default:
156                          Parse_error("unexpected token");
157         }
158 }
159
160 /// expression ::= primary binoprhs
161 ex parser::parse_expression() 
162 {
163         ex lhs = parse_primary();
164         ex res = parse_binop_rhs(0, lhs);
165         return res;
166 }
167
168 /// number_expr: number
169 ex parser::parse_number_expr()
170 {
171         ex n = numeric(scanner->str.c_str());
172         get_next_tok(); // consume the number
173         return n;
174 }
175
176 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
177 ex parser::parse_literal_expr()
178 {
179         get_next_tok(); // consume the literal
180         if (scanner->str == "I")
181                 return I;
182         else if (scanner->str == "Pi")
183                 return Pi;
184         else if (scanner->str == "Euler")
185                 return Euler;
186         else if (scanner->str == "Catalan")
187                 return Catalan;
188         bug("unknown literal: \"" << scanner->str << "\"");
189 }
190
191 ex parser::operator()(std::istream& input)
192 {
193         scanner->switch_input(&input);
194         get_next_tok();
195         ex ret = parse_expression();
196         // parse_expression() stops if it encounters an unknown token.
197         // This is not a bug: since the parser is recursive checking
198         // whether the next token is valid is responsibility of the caller.
199         // Hence make sure nothing is left in the stream:
200         if (token != lexer::token_type::eof)
201                 Parse_error("expected EOF");
202
203         return ret;
204 }
205
206 ex parser::operator()(const std::string& input)
207 {
208         std::istringstream is(input);
209         ex ret = operator()(is);
210         return ret;
211 }
212
213 int parser::get_next_tok()
214 {
215         token = scanner->gettok();
216         return token;
217 }
218
219 parser::parser(const symtab& syms_, const bool strict_,
220                const prototype_table& funcs_) : strict(strict_),
221         funcs(funcs_), syms(syms_)
222 {
223         scanner = new lexer();
224 }
225
226 parser::~parser()
227 {
228         delete scanner;
229 }
230
231 } // namespace GiNaC