]> www.ginac.de Git - ginac.git/blob - ginac/parser/parser.cpp
Fixed memory leak.
[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 /// identifier_expr:  identifier |  identifier '(' expression* ')'
36 ex parser::parse_identifier_expr()
37 {
38         std::string name = scanner->str;
39         get_next_tok();  // eat identifier.
40
41         if (token != '(') // symbol
42                 return find_or_insert_symbol(name, syms, strict);
43
44         // function/ctor call.
45         get_next_tok();  // eat (
46         exvector args;
47         if (token != ')') {
48                 while (true) {
49                         ex e = parse_expression();
50                         args.push_back(e);
51
52                         if (token == ')')
53                                 break;
54
55                         if (token != ',')
56                                 Parse_error("expected ')' or ',' in argument list");
57
58                         get_next_tok();
59                 }
60         }
61         // Eat the ')'.
62         get_next_tok();
63         prototype the_prototype = make_pair(name, args.size());
64         prototype_table::const_iterator reader = funcs.find(the_prototype);
65         if (reader == funcs.end()) {
66                 Parse_error_("no function \"" << name << "\" with " <<
67                              args.size() << " arguments");
68         }
69         // dirty hack to distinguish between serial numbers of functions and real
70         // pointers.
71         GiNaC::function* f = NULL;
72         try {
73                 f = new GiNaC::function(reinterpret_cast<unsigned>(reader->second), args);
74         }
75         catch ( std::runtime_error ) {
76                 if ( f ) delete f;
77                 ex ret = reader->second(args);
78                 return ret;
79         }
80         return f->setflag(status_flags::dynallocated);
81 }
82
83 /// paren_expr:  '(' expression ')'
84 ex parser::parse_paren_expr()
85 {
86         get_next_tok();  // eat (.
87         ex e = parse_expression();
88
89         if (token != ')')
90                 Parse_error("expected ')'");
91         get_next_tok();  // eat ).
92         return e;
93 }
94
95 extern numeric* _num_1_p;
96 extern ex _ex0;
97
98 /// unary_expr: [+-] expression
99 ex parser::parse_unary_expr()
100 {
101         // Unlike most other parse_* method this one does NOT consume
102         // current token so parse_binop_rhs() knows what kind of operator
103         // is being parsed.
104         
105         // There are different kinds of expressions which need to be handled:
106         // -a+b 
107         // -(a) 
108         // +a
109         // +(a)
110         // Delegete the work to parse_binop_rhs(), otherwise we end up
111         // duplicating it here. 
112         ex lhs = _ex0; // silly trick
113         ex e = parse_binop_rhs(0, lhs);
114         return e;
115 }
116
117 /// primary: identifier_expr | number_expr | paren_expr | unary_expr 
118 ex parser::parse_primary() 
119 {
120         switch (token) {
121                 case lexer::token_type::identifier:
122                          return parse_identifier_expr();
123                 case lexer::token_type::number:
124                          return parse_number_expr();
125                 case '(': 
126                          return parse_paren_expr();
127                 case '-':
128                 case '+':
129                          return parse_unary_expr();
130                 case lexer::token_type::literal:
131                          return parse_literal_expr();
132                 case lexer::token_type::eof:
133                 default:
134                          Parse_error("unexpected token");
135         }
136 }
137
138 /// expression ::= primary binoprhs
139 ex parser::parse_expression() 
140 {
141         ex lhs = parse_primary();
142         ex res = parse_binop_rhs(0, lhs);
143         return res;
144 }
145
146 /// number_expr: number
147 ex parser::parse_number_expr()
148 {
149         ex n = numeric(scanner->str.c_str());
150         get_next_tok(); // consume the number
151         return n;
152 }
153
154 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
155 ex parser::parse_literal_expr()
156 {
157         get_next_tok(); // consume the literal
158         if (scanner->str == "I")
159                 return I;
160         else if (scanner->str == "Pi")
161                 return Pi;
162         else if (scanner->str == "Euler")
163                 return Euler;
164         else if (scanner->str == "Catalan")
165                 return Catalan;
166         bug("unknown literal: \"" << scanner->str << "\"");
167 }
168
169 ex parser::operator()(std::istream& input)
170 {
171         scanner->switch_input(&input);
172         get_next_tok();
173         ex ret = parse_expression();
174         // parse_expression() stops if it encounters an unknown token.
175         // This is not a bug: since the parser is recursive checking
176         // whether the next token is valid is responsibility of the caller.
177         // Hence make sure nothing is left in the stream:
178         if (token != lexer::token_type::eof)
179                 Parse_error("expected EOF");
180
181         return ret;
182 }
183
184 ex parser::operator()(const std::string& input)
185 {
186         std::istringstream is(input);
187         ex ret = operator()(is);
188         return ret;
189 }
190
191 int parser::get_next_tok()
192 {
193         token = scanner->gettok();
194         return token;
195 }
196
197 parser::parser(const symtab& syms_, const bool strict_,
198                const prototype_table& funcs_) : strict(strict_),
199         funcs(funcs_), syms(syms_)
200 {
201         scanner = new lexer();
202 }
203
204 parser::~parser()
205 {
206         delete scanner;
207 }
208
209 } // namespace GiNaC