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