]> www.ginac.de Git - ginac.git/blob - ginac/parser/parser.cpp
[bugfix] Always #include <lst.h> before using lst. Fixes build error on MinGW.
[ginac.git] / ginac / parser / parser.cpp
1 /** @file parser.cpp
2  *
3  *  Implementation of GiNaC's parser. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2011 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 "lst.h"
25 #include "lexer.h"
26 #include "debug.h"
27 #include "mul.h"
28 #include "constant.h"
29 #include "function.h"
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #ifdef HAVE_STDINT_H
35 #include <stdint.h> // for uintptr_t
36 #endif
37 #include <sstream>
38 #include <stdexcept>
39
40 namespace GiNaC {
41
42 // <KLUDGE>
43 // Find out if ptr is a pointer to a function or a specially crafted integer.
44 // It's possible to distinguish between these because functions are aligned.
45 // Returns true if ptr is a pointer and false otherwise.
46 static bool decode_serial(unsigned& serial, const reader_func ptr)
47 {
48         uintptr_t u = (uintptr_t)(void *)ptr;
49         if (u & 1) {
50                 u >>= 1;
51                 serial = (unsigned)u;
52                 return false;
53         }
54         return true;
55 }
56
57 // Figures out if ptr is a pointer to function or a serial of GiNaC function.
58 // In the former case calls that function, in the latter case constructs
59 // GiNaC function with corresponding serial and arguments.
60 static ex dispatch_reader_fcn(const reader_func ptr, const exvector& args)
61 {
62         unsigned serial = 0; // dear gcc, could you please shut up?
63         bool is_ptr = decode_serial(serial, ptr);
64         if (is_ptr)
65                 return ptr(args);
66         else
67                 return function(serial, args);
68 }
69 // </KLUDGE>
70
71
72 /// identifier_expr:  identifier |  identifier '(' expression* ')'
73 ex parser::parse_identifier_expr()
74 {
75         std::string name = scanner->str;
76         get_next_tok();  // eat identifier.
77
78         if (token != '(') // symbol
79                 return find_or_insert_symbol(name, syms, strict);
80
81         // function/ctor call.
82         get_next_tok();  // eat (
83         exvector args;
84         if (token != ')') {
85                 while (true) {
86                         ex e = parse_expression();
87                         args.push_back(e);
88
89                         if (token == ')')
90                                 break;
91
92                         if (token != ',')
93                                 Parse_error("expected ')' or ',' in argument list");
94
95                         get_next_tok();
96                 }
97         }
98         // Eat the ')'.
99         get_next_tok();
100         prototype the_prototype = make_pair(name, args.size());
101         prototype_table::const_iterator reader = funcs.find(the_prototype);
102         if (reader == funcs.end()) {
103                 Parse_error_("no function \"" << name << "\" with " <<
104                              args.size() << " arguments");
105         }
106         // reader->second might be a pointer to a C++ function or a specially
107         // crafted serial of a GiNaC::function.
108         ex ret = dispatch_reader_fcn(reader->second, args);
109         return ret;
110 }
111
112 /// paren_expr:  '(' expression ')'
113 ex parser::parse_paren_expr()
114 {
115         get_next_tok();  // eat (.
116         ex e = parse_expression();
117
118         if (token != ')')
119                 Parse_error("expected ')'");
120         get_next_tok();  // eat ).
121         return e;
122 }
123
124 /// lst_expr:  '{' expression { ',' expression } '}'
125 ex parser::parse_lst_expr()
126 {
127         get_next_tok();  // eat {.
128
129         lst list;
130         if (token != '}') {
131                 while (true) {
132                         ex e = parse_expression(); // expression();
133                         list.append(e);
134
135                         if (token == '}') {
136                                 break;
137                         }
138
139                         if (token != ',') {
140                                 Parse_error("expected '}'");
141                         }
142
143                         get_next_tok();  // eat ','.
144                 }
145         }
146         // Eat the '}'.
147         get_next_tok();
148
149         return list;
150 }
151
152 extern const ex _ex0;
153
154 /// unary_expr: [+-] expression
155 ex parser::parse_unary_expr()
156 {
157         // Unlike most other parse_* method this one does NOT consume
158         // current token so parse_binop_rhs() knows what kind of operator
159         // is being parsed.
160         
161         // There are different kinds of expressions which need to be handled:
162         // -a+b 
163         // -(a) 
164         // +a
165         // +(a)
166         // Delegete the work to parse_binop_rhs(), otherwise we end up
167         // duplicating it here. 
168         ex lhs = _ex0; // silly trick
169         ex e = parse_binop_rhs(0, lhs);
170         return e;
171 }
172
173 /// primary: identifier_expr | number_expr | paren_expr | unary_expr 
174 ex parser::parse_primary() 
175 {
176         switch (token) {
177                 case lexer::token_type::identifier:
178                          return parse_identifier_expr();
179                 case lexer::token_type::number:
180                          return parse_number_expr();
181                 case '(': 
182                          return parse_paren_expr();
183                 case '{': 
184                          return parse_lst_expr();
185                 case '-':
186                 case '+':
187                          return parse_unary_expr();
188                 case lexer::token_type::literal:
189                          return parse_literal_expr();
190                 case lexer::token_type::eof:
191                 default:
192                          Parse_error("unexpected token");
193         }
194 }
195
196 /// expression ::= primary binoprhs
197 ex parser::parse_expression() 
198 {
199         ex lhs = parse_primary();
200         ex res = parse_binop_rhs(0, lhs);
201         return res;
202 }
203
204 /// number_expr: number
205 ex parser::parse_number_expr()
206 {
207         ex n = numeric(scanner->str.c_str());
208         get_next_tok(); // consume the number
209         return n;
210 }
211
212 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
213 ex parser::parse_literal_expr()
214 {
215         get_next_tok(); // consume the literal
216         if (scanner->str == "I")
217                 return I;
218         else if (scanner->str == "Pi")
219                 return Pi;
220         else if (scanner->str == "Euler")
221                 return Euler;
222         else if (scanner->str == "Catalan")
223                 return Catalan;
224         bug("unknown literal: \"" << scanner->str << "\"");
225 }
226
227 ex parser::operator()(std::istream& input)
228 {
229         scanner->switch_input(&input);
230         get_next_tok();
231         ex ret = parse_expression();
232         // parse_expression() stops if it encounters an unknown token.
233         // This is not a bug: since the parser is recursive checking
234         // whether the next token is valid is responsibility of the caller.
235         // Hence make sure nothing is left in the stream:
236         if (token != lexer::token_type::eof)
237                 Parse_error("expected EOF");
238
239         return ret;
240 }
241
242 ex parser::operator()(const std::string& input)
243 {
244         std::istringstream is(input);
245         ex ret = operator()(is);
246         return ret;
247 }
248
249 int parser::get_next_tok()
250 {
251         token = scanner->gettok();
252         return token;
253 }
254
255 parser::parser(const symtab& syms_, const bool strict_,
256                const prototype_table& funcs_) : strict(strict_),
257         funcs(funcs_), syms(syms_)
258 {
259         scanner = new lexer();
260 }
261
262 parser::~parser()
263 {
264         delete scanner;
265 }
266
267 } // namespace GiNaC