1 /** @file ginsh_parser.yy
3 * Input grammar definition for ginsh.
4 * This file must be processed with yacc/bison.
6 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <sys/resource.h>
34 #include <sys/types.h>
43 #include <readline/readline.h>
44 #include <readline/history.h>
51 #include <ginac/ginac.h>
54 // Original readline settings
55 static int orig_completion_append_character;
56 static char *orig_basic_word_break_characters;
58 // Expression stack for ", "" and """
59 static void push(const ex &e);
62 // Start and end time for the time() function
63 static struct rusage start_time, end_time;
65 // Table of functions (a multimap, because one function may appear with different
66 // numbers of parameters)
67 typedef ex (*fcnp)(const exprseq &e);
68 typedef ex (*fcnp2)(const exprseq &e, int serial);
71 fcn_desc() : p(NULL), num_params(0) {}
72 fcn_desc(fcnp func, int num) : p(func), num_params(num), is_ginac(false) {}
73 fcn_desc(fcnp2 func, int num, int ser) : p((fcnp)func), num_params(num), is_ginac(true), serial(ser) {}
75 fcnp p; // Pointer to function
76 int num_params; // Number of parameters (0 = arbitrary)
77 bool is_ginac; // Flag: function is GiNaC function
78 int serial; // GiNaC function serial number (if is_ginac == true)
81 typedef multimap<string, fcn_desc> fcn_tab;
84 static fcn_tab::const_iterator find_function(const ex &sym, int req_params);
86 static ex lst2matrix(const ex &l);
89 /* Tokens (T_LITERAL means a literal value returned by the parser, but not
90 of class numeric or symbol (e.g. a constant or the FAIL object)) */
91 %token T_NUMBER T_SYMBOL T_LITERAL T_DIGITS T_QUOTE T_QUOTE2 T_QUOTE3
92 %token T_EQUAL T_NOTEQ T_LESSEQ T_GREATEREQ T_MATRIX_BEGIN T_MATRIX_END
94 %token T_QUIT T_PRINT T_TIME T_XYZZY T_INVENTORY T_LOOK T_SCORE
96 /* Operator precedence and associativity */
99 %left '<' '>' T_LESSEQ T_GREATEREQ
124 } catch (exception &e) {
125 cerr << e.what() << endl;
129 | T_PRINT '(' exp ')' ';'
133 } catch (exception &e) {
134 cerr << e.what() << endl;
139 | T_XYZZY {cout << "Nothing happens.\n";}
140 | T_INVENTORY {cout << "You're not carrying anything.\n";}
141 | T_LOOK {cout << "You're in a twisty little maze of passages, all alike.\n";}
144 cout << "If you were to quit now, you would score ";
145 cout << (syms.size() > 350 ? 350 : syms.size());
146 cout << " out of a possible 350.\n";
148 | error ';' {yyclearin; yyerrok;}
151 exp : T_NUMBER {$$ = $1;}
152 | T_SYMBOL {$$ = $1.eval();}
153 | '\'' T_SYMBOL '\'' {$$ = $2;}
154 | T_LITERAL {$$ = $1;}
155 | T_DIGITS {$$ = $1;}
156 | T_QUOTE {$$ = exstack[0];}
157 | T_QUOTE2 {$$ = exstack[1];}
158 | T_QUOTE3 {$$ = exstack[2];}
159 | T_TIME {getrusage(RUSAGE_SELF, &start_time);} '(' exp ')'
161 getrusage(RUSAGE_SELF, &end_time);
162 $$ = (end_time.ru_utime.tv_sec - start_time.ru_utime.tv_sec) +
163 (end_time.ru_stime.tv_sec - start_time.ru_stime.tv_sec) +
164 double(end_time.ru_utime.tv_usec - start_time.ru_utime.tv_usec) / 1e6 +
165 double(end_time.ru_stime.tv_usec - start_time.ru_stime.tv_usec) / 1e6;
167 | T_SYMBOL '(' exprseq ')'
169 fcn_tab::const_iterator i = find_function($1, $3.nops());
170 if (i->second.is_ginac) {
171 $$ = ((fcnp2)(i->second.p))(static_cast<const exprseq &>(*($3.bp)), i->second.serial);
173 $$ = (i->second.p)(static_cast<const exprseq &>(*($3.bp)));
176 | T_DIGITS '=' T_NUMBER
177 {$$ = $3; Digits = ex_to_numeric($3).to_int();}
179 {$$ = $3; const_cast<symbol *>(&ex_to_symbol($1))->assign($3);}
180 | exp T_EQUAL exp {$$ = $1 == $3;}
181 | exp T_NOTEQ exp {$$ = $1 != $3;}
182 | exp '<' exp {$$ = $1 < $3;}
183 | exp T_LESSEQ exp {$$ = $1 <= $3;}
184 | exp '>' exp {$$ = $1 > $3;}
185 | exp T_GREATEREQ exp {$$ = $1 >= $3;}
186 | exp '+' exp {$$ = $1 + $3;}
187 | exp '-' exp {$$ = $1 - $3;}
188 | exp '*' exp {$$ = $1 * $3;}
189 | exp '/' exp {$$ = $1 / $3;}
190 | exp '%' exp {$$ = $1 % $3;}
191 | '-' exp %prec NEG {$$ = -$2;}
192 | '+' exp %prec NEG {$$ = $2;}
193 | exp '^' exp {$$ = power($1, $3);}
194 | exp '!' {$$ = factorial($1);}
195 | '(' exp ')' {$$ = $2;}
196 | '[' list_or_empty ']' {$$ = $2;}
197 | T_MATRIX_BEGIN matrix T_MATRIX_END {$$ = lst2matrix($2);}
200 exprseq : exp {$$ = exprseq($1);}
201 | exprseq ',' exp {exprseq es(static_cast<exprseq &>(*($1.bp))); $$ = es.append($3);}
204 list_or_empty: /* empty */ {$$ = *new lst;}
208 list : exp {$$ = lst($1);}
209 | list ',' exp {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($3);}
212 matrix : T_MATRIX_BEGIN row T_MATRIX_END {$$ = lst($2);}
213 | matrix ',' T_MATRIX_BEGIN row T_MATRIX_END {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($4);}
216 row : exp {$$ = lst($1);}
217 | row ',' exp {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($3);}
226 // Error print routine
229 cerr << s << " at " << yytext << endl;
233 // Push expression "e" onto the expression stack (for ", "" and """)
234 static void push(const ex &e)
236 exstack[2] = exstack[1];
237 exstack[1] = exstack[0];
246 static ex f_beta(const exprseq &e) {return gamma(e[0])*gamma(e[1])/gamma(e[0]+e[1]);}
247 static ex f_denom(const exprseq &e) {return e[0].denom();}
248 static ex f_eval1(const exprseq &e) {return e[0].eval();}
249 static ex f_evalf1(const exprseq &e) {return e[0].evalf();}
250 static ex f_expand(const exprseq &e) {return e[0].expand();}
251 static ex f_gcd(const exprseq &e) {return gcd(e[0], e[1]);}
252 static ex f_lcm(const exprseq &e) {return lcm(e[0], e[1]);}
253 static ex f_lsolve(const exprseq &e) {return lsolve(e[0], e[1]);}
254 static ex f_nops(const exprseq &e) {return e[0].nops();}
255 static ex f_normal1(const exprseq &e) {return e[0].normal();}
256 static ex f_numer(const exprseq &e) {return e[0].numer();}
257 static ex f_power(const exprseq &e) {return power(e[0], e[1]);}
258 static ex f_sqrt(const exprseq &e) {return sqrt(e[0]);}
259 static ex f_subs2(const exprseq &e) {return e[0].subs(e[1]);}
261 #define CHECK_ARG(num, type, fcn) if (!is_ex_of_type(e[num], type)) throw(std::invalid_argument("argument " #num " to " #fcn " must be a " #type))
263 static ex f_charpoly(const exprseq &e)
265 CHECK_ARG(0, matrix, charpoly);
266 CHECK_ARG(1, symbol, charpoly);
267 return ex_to_matrix(e[0]).charpoly(ex_to_symbol(e[1]));
270 static ex f_coeff(const exprseq &e)
272 CHECK_ARG(1, symbol, coeff);
273 CHECK_ARG(2, numeric, coeff);
274 return e[0].coeff(ex_to_symbol(e[1]), ex_to_numeric(e[2]).to_int());
277 static ex f_collect(const exprseq &e)
279 CHECK_ARG(1, symbol, collect);
280 return e[0].collect(ex_to_symbol(e[1]));
283 static ex f_content(const exprseq &e)
285 CHECK_ARG(1, symbol, content);
286 return e[0].content(ex_to_symbol(e[1]));
289 static ex f_degree(const exprseq &e)
291 CHECK_ARG(1, symbol, degree);
292 return e[0].degree(ex_to_symbol(e[1]));
295 static ex f_determinant(const exprseq &e)
297 CHECK_ARG(0, matrix, determinant);
298 return ex_to_matrix(e[0]).determinant();
301 static ex f_diag(const exprseq &e)
304 matrix &m = *new matrix(dim, dim);
305 for (int i=0; i<dim; i++)
306 m.set(i, i, e.op(i));
310 static ex f_diff2(const exprseq &e)
312 CHECK_ARG(1, symbol, diff);
313 return e[0].diff(ex_to_symbol(e[1]));
316 static ex f_diff3(const exprseq &e)
318 CHECK_ARG(1, symbol, diff);
319 CHECK_ARG(2, numeric, diff);
320 return e[0].diff(ex_to_symbol(e[1]), ex_to_numeric(e[2]).to_int());
323 static ex f_divide(const exprseq &e)
326 if (divide(e[0], e[1], q))
332 static ex f_eval2(const exprseq &e)
334 CHECK_ARG(1, numeric, eval);
335 return e[0].eval(ex_to_numeric(e[1]).to_int());
338 static ex f_evalf2(const exprseq &e)
340 CHECK_ARG(1, numeric, evalf);
341 return e[0].evalf(ex_to_numeric(e[1]).to_int());
344 static ex f_has(const exprseq &e)
346 return e[0].has(e[1]) ? exONE() : exZERO();
349 static ex f_inverse(const exprseq &e)
351 CHECK_ARG(0, matrix, inverse);
352 return ex_to_matrix(e[0]).inverse();
355 static ex f_is(const exprseq &e)
357 CHECK_ARG(0, relational, is);
358 return (bool)ex_to_relational(e[0]) ? exONE() : exZERO();
361 static ex f_lcoeff(const exprseq &e)
363 CHECK_ARG(1, symbol, lcoeff);
364 return e[0].lcoeff(ex_to_symbol(e[1]));
367 static ex f_ldegree(const exprseq &e)
369 CHECK_ARG(1, symbol, ldegree);
370 return e[0].ldegree(ex_to_symbol(e[1]));
373 static ex f_normal2(const exprseq &e)
375 CHECK_ARG(1, numeric, normal);
376 return e[0].normal(ex_to_numeric(e[1]).to_int());
379 static ex f_op(const exprseq &e)
381 CHECK_ARG(1, numeric, op);
382 int n = ex_to_numeric(e[1]).to_int();
383 if (n < 0 || n >= e[0].nops())
384 throw(std::out_of_range("second argument to op() is out of range"));
388 static ex f_prem(const exprseq &e)
390 CHECK_ARG(2, symbol, prem);
391 return prem(e[0], e[1], ex_to_symbol(e[2]));
394 static ex f_primpart(const exprseq &e)
396 CHECK_ARG(1, symbol, primpart);
397 return e[0].primpart(ex_to_symbol(e[1]));
400 static ex f_quo(const exprseq &e)
402 CHECK_ARG(2, symbol, quo);
403 return quo(e[0], e[1], ex_to_symbol(e[2]));
406 static ex f_rem(const exprseq &e)
408 CHECK_ARG(2, symbol, rem);
409 return rem(e[0], e[1], ex_to_symbol(e[2]));
412 static ex f_series2(const exprseq &e)
414 CHECK_ARG(1, symbol, series);
415 return e[0].series(ex_to_symbol(e[1]), exZERO());
418 static ex f_series3(const exprseq &e)
420 CHECK_ARG(1, symbol, series);
421 return e[0].series(ex_to_symbol(e[1]), e[2]);
424 static ex f_series4(const exprseq &e)
426 CHECK_ARG(1, symbol, series);
427 CHECK_ARG(3, numeric, series);
428 return e[0].series(ex_to_symbol(e[1]), e[2], ex_to_numeric(e[3]).to_int());
431 static ex f_sqrfree(const exprseq &e)
433 CHECK_ARG(1, symbol, sqrfree);
434 return sqrfree(e[0], ex_to_symbol(e[1]));
437 static ex f_subs3(const exprseq &e)
439 CHECK_ARG(1, lst, subs);
440 CHECK_ARG(2, lst, subs);
441 return e[0].subs(ex_to_lst(e[1]), ex_to_lst(e[2]));
444 static ex f_tcoeff(const exprseq &e)
446 CHECK_ARG(1, symbol, tcoeff);
447 return e[0].tcoeff(ex_to_symbol(e[1]));
450 static ex f_trace(const exprseq &e)
452 CHECK_ARG(0, matrix, trace);
453 return ex_to_matrix(e[0]).trace();
456 static ex f_transpose(const exprseq &e)
458 CHECK_ARG(0, matrix, transpose);
459 return ex_to_matrix(e[0]).transpose();
462 static ex f_unassign(const exprseq &e)
464 CHECK_ARG(0, symbol, unassign);
465 (const_cast<symbol *>(&ex_to_symbol(e[0])))->unassign();
469 static ex f_unit(const exprseq &e)
471 CHECK_ARG(1, symbol, unit);
472 return e[0].unit(ex_to_symbol(e[1]));
475 static ex f_dummy(const exprseq &e)
477 throw(std::logic_error("dummy function called (shouldn't happen)"));
482 * Add all registered GiNaC functions to ginsh
485 static ex f_ginac_function(const exprseq &es, int serial)
487 return function(serial, es).eval(1);
490 void ginsh_get_ginac_functions(void)
492 vector<registered_function_info>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
495 fcns.insert(make_pair(i->name, fcn_desc(f_ginac_function, i->nparams, serial)));
503 * Find a function given a name and number of parameters. Throw exceptions on error.
506 static fcn_tab::const_iterator find_function(const ex &sym, int req_params)
508 const string &name = ex_to_symbol(sym).getname();
509 typedef fcn_tab::const_iterator I;
510 pair<I, I> b = fcns.equal_range(name);
511 if (b.first == b.second)
512 throw(std::logic_error("unknown function '" + name + "'"));
514 for (I i=b.first; i!=b.second; i++)
515 if ((i->second.num_params == 0) || (i->second.num_params == req_params))
518 throw(std::logic_error("invalid number of arguments to " + name + "()"));
523 * Convert list of lists to matrix
526 static ex lst2matrix(const ex &l)
528 if (!is_ex_of_type(l, lst))
529 throw(std::logic_error("internal error: argument to lst2matrix() is not a list"));
531 // Find number of rows and columns
532 int rows = l.nops(), cols = 0, i, j;
533 for (i=0; i<rows; i++)
534 if (l.op(i).nops() > cols)
535 cols = l.op(i).nops();
537 // Allocate and fill matrix
538 matrix &m = *new matrix(rows, cols);
539 for (i=0; i<rows; i++)
540 for (j=0; j<cols; j++)
541 if (l.op(i).nops() > j)
542 m.set(i, j, l.op(i).op(j));
544 m.set(i, j, exZERO());
550 * Function name completion functions for readline
553 static char *fcn_generator(char *text, int state)
555 static int len; // Length of word to complete
556 static fcn_tab::const_iterator index; // Iterator to function being currently considered
558 // If this is a new word to complete, initialize now
560 index = fcns.begin();
564 // Return the next function which partially matches
565 while (index != fcns.end()) {
566 const char *fcn_name = index->first.c_str();
568 if (strncmp(fcn_name, text, len) == 0)
569 return strdup(fcn_name);
574 static char **fcn_completion(char *text, int start, int end)
576 if (rl_line_buffer[0] == '!') {
577 // For shell commands, revert back to filename completion
578 rl_completion_append_character = orig_completion_append_character;
579 rl_basic_word_break_characters = orig_basic_word_break_characters;
580 return completion_matches(text, filename_completion_function);
582 // Otherwise, complete function names
583 rl_completion_append_character = '(';
584 rl_basic_word_break_characters = " \t\n\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~";
585 return completion_matches(text, fcn_generator);
594 int main(int argc, char **argv)
596 // Print banner in interactive mode
598 cout << "ginsh - GiNaC Interactive Shell (" << PACKAGE << " " << VERSION << ")\n";
599 cout << "Copyright (C) 1999 Johannes Gutenberg Universitaet Mainz, Germany\n";
600 cout << "This is free software, and you are welcome to redistribute it\n";
601 cout << "under certain conditions; see the file COPYING for details.\n";
604 // Init table of built-in functions
605 fcns.insert(make_pair(string("beta"), fcn_desc(f_beta, 2)));
606 fcns.insert(make_pair(string("charpoly"), fcn_desc(f_charpoly, 2)));
607 fcns.insert(make_pair(string("coeff"), fcn_desc(f_coeff, 3)));
608 fcns.insert(make_pair(string("collect"), fcn_desc(f_collect, 2)));
609 fcns.insert(make_pair(string("content"), fcn_desc(f_content, 2)));
610 fcns.insert(make_pair(string("degree"), fcn_desc(f_degree, 2)));
611 fcns.insert(make_pair(string("denom"), fcn_desc(f_denom, 1)));
612 fcns.insert(make_pair(string("determinant"), fcn_desc(f_determinant, 1)));
613 fcns.insert(make_pair(string("diag"), fcn_desc(f_diag, 0)));
614 fcns.insert(make_pair(string("diff"), fcn_desc(f_diff2, 2)));
615 fcns.insert(make_pair(string("diff"), fcn_desc(f_diff3, 3)));
616 fcns.insert(make_pair(string("divide"), fcn_desc(f_divide, 2)));
617 fcns.insert(make_pair(string("eval"), fcn_desc(f_eval1, 1)));
618 fcns.insert(make_pair(string("eval"), fcn_desc(f_eval2, 2)));
619 fcns.insert(make_pair(string("evalf"), fcn_desc(f_evalf1, 1)));
620 fcns.insert(make_pair(string("evalf"), fcn_desc(f_evalf2, 2)));
621 fcns.insert(make_pair(string("expand"), fcn_desc(f_expand, 1)));
622 fcns.insert(make_pair(string("gcd"), fcn_desc(f_gcd, 2)));
623 fcns.insert(make_pair(string("has"), fcn_desc(f_has, 2)));
624 fcns.insert(make_pair(string("inverse"), fcn_desc(f_inverse, 1)));
625 fcns.insert(make_pair(string("is"), fcn_desc(f_is, 1)));
626 fcns.insert(make_pair(string("lcm"), fcn_desc(f_lcm, 2)));
627 fcns.insert(make_pair(string("lcoeff"), fcn_desc(f_lcoeff, 2)));
628 fcns.insert(make_pair(string("ldegree"), fcn_desc(f_ldegree, 2)));
629 fcns.insert(make_pair(string("lsolve"), fcn_desc(f_lsolve, 2)));
630 fcns.insert(make_pair(string("nops"), fcn_desc(f_nops, 1)));
631 fcns.insert(make_pair(string("normal"), fcn_desc(f_normal1, 1)));
632 fcns.insert(make_pair(string("normal"), fcn_desc(f_normal2, 2)));
633 fcns.insert(make_pair(string("numer"), fcn_desc(f_numer, 1)));
634 fcns.insert(make_pair(string("op"), fcn_desc(f_op, 2)));
635 fcns.insert(make_pair(string("power"), fcn_desc(f_power, 2)));
636 fcns.insert(make_pair(string("prem"), fcn_desc(f_prem, 3)));
637 fcns.insert(make_pair(string("primpart"), fcn_desc(f_primpart, 2)));
638 fcns.insert(make_pair(string("quo"), fcn_desc(f_quo, 3)));
639 fcns.insert(make_pair(string("rem"), fcn_desc(f_rem, 3)));
640 fcns.insert(make_pair(string("series"), fcn_desc(f_series2, 2)));
641 fcns.insert(make_pair(string("series"), fcn_desc(f_series3, 3)));
642 fcns.insert(make_pair(string("series"), fcn_desc(f_series4, 4)));
643 fcns.insert(make_pair(string("sqrfree"), fcn_desc(f_sqrfree, 2)));
644 fcns.insert(make_pair(string("sqrt"), fcn_desc(f_sqrt, 1)));
645 fcns.insert(make_pair(string("subs"), fcn_desc(f_subs2, 2)));
646 fcns.insert(make_pair(string("subs"), fcn_desc(f_subs3, 3)));
647 fcns.insert(make_pair(string("tcoeff"), fcn_desc(f_tcoeff, 2)));
648 fcns.insert(make_pair(string("time"), fcn_desc(f_dummy, 0)));
649 fcns.insert(make_pair(string("trace"), fcn_desc(f_trace, 1)));
650 fcns.insert(make_pair(string("transpose"), fcn_desc(f_transpose, 1)));
651 fcns.insert(make_pair(string("unassign"), fcn_desc(f_unassign, 1)));
652 fcns.insert(make_pair(string("unit"), fcn_desc(f_unit, 2)));
653 ginsh_get_ginac_functions();
655 // Init readline completer
656 rl_readline_name = argv[0];
657 rl_attempted_completion_function = (CPPFunction *)fcn_completion;
658 orig_completion_append_character = rl_completion_append_character;
659 orig_basic_word_break_characters = rl_basic_word_break_characters;
661 // Parse input, catch all remaining exceptions
665 } catch (exception &e) {
666 cerr << e.what() << endl;