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