2 * ginsh.y - GiNaC Interactive Shell, input grammar definition
4 * This file must be processed with yacc/bison
15 #include <sys/resource.h>
18 #include <sys/types.h>
27 #include <readline/readline.h>
28 #include <readline/history.h>
35 #include <ginac/ginac.h>
38 // Original readline settings
39 static int orig_completion_append_character;
40 static char *orig_basic_word_break_characters;
42 // Expression stack for ", "" and """
43 static void push(const ex &e);
46 // Start and end time for the time() function
47 static struct rusage start_time, end_time;
49 // Table of functions (a multimap, because one function may appear with different
50 // numbers of parameters)
51 typedef ex (*fcnp)(const exprseq &e);
52 typedef ex (*fcnp2)(const exprseq &e, int serial);
55 fcn_desc() : p(NULL), num_params(0) {}
56 fcn_desc(fcnp func, int num) : p(func), num_params(num), is_ginac(false) {}
57 fcn_desc(fcnp2 func, int num, int ser) : p((fcnp)func), num_params(num), is_ginac(true), serial(ser) {}
59 fcnp p; // Pointer to function
60 int num_params; // Number of parameters (0 = arbitrary)
61 bool is_ginac; // Flag: function is GiNaC function
62 int serial; // GiNaC function serial number (if is_ginac == true)
65 typedef multimap<string, fcn_desc> fcn_tab;
68 static fcn_tab::const_iterator find_function(const ex &sym, int req_params);
70 static ex lst2matrix(const ex &l);
73 /* Tokens (T_LITERAL means a literal value returned by the parser, but not
74 of class numeric or symbol (e.g. a constant or the FAIL object)) */
75 %token T_NUMBER T_SYMBOL T_LITERAL T_DIGITS T_QUOTE T_QUOTE2 T_QUOTE3
76 %token T_EQUAL T_NOTEQ T_LESSEQ T_GREATEREQ T_MATRIX_BEGIN T_MATRIX_END
78 %token T_QUIT T_PRINT T_TIME T_XYZZY T_INVENTORY T_LOOK T_SCORE
80 /* Operator precedence and associativity */
83 %left '<' '>' T_LESSEQ T_GREATEREQ
108 } catch (exception &e) {
109 cerr << e.what() << endl;
113 | T_PRINT '(' exp ')' ';'
117 } catch (exception &e) {
118 cerr << e.what() << endl;
123 | T_XYZZY {cout << "Nothing happens.\n";}
124 | T_INVENTORY {cout << "You're not carrying anything.\n";}
125 | T_LOOK {cout << "You're in a twisty little maze of passages, all alike.\n";}
128 cout << "If you were to quit now, you would score ";
129 cout << (syms.size() > 350 ? 350 : syms.size());
130 cout << " out of a possible 350.\n";
132 | error ';' {yyclearin; yyerrok;}
135 exp : T_NUMBER {$$ = $1;}
136 | T_SYMBOL {$$ = $1.eval();}
137 | '\'' T_SYMBOL '\'' {$$ = $2;}
138 | T_LITERAL {$$ = $1;}
139 | T_DIGITS {$$ = $1;}
140 | T_QUOTE {$$ = exstack[0];}
141 | T_QUOTE2 {$$ = exstack[1];}
142 | T_QUOTE3 {$$ = exstack[2];}
143 | T_TIME {getrusage(RUSAGE_SELF, &start_time);} '(' exp ')'
145 getrusage(RUSAGE_SELF, &end_time);
146 $$ = (end_time.ru_utime.tv_sec - start_time.ru_utime.tv_sec) +
147 (end_time.ru_stime.tv_sec - start_time.ru_stime.tv_sec) +
148 double(end_time.ru_utime.tv_usec - start_time.ru_utime.tv_usec) / 1e6 +
149 double(end_time.ru_stime.tv_usec - start_time.ru_stime.tv_usec) / 1e6;
151 | T_SYMBOL '(' exprseq ')'
153 fcn_tab::const_iterator i = find_function($1, $3.nops());
154 if (i->second.is_ginac) {
155 $$ = ((fcnp2)(i->second.p))(static_cast<const exprseq &>(*($3.bp)), i->second.serial);
157 $$ = (i->second.p)(static_cast<const exprseq &>(*($3.bp)));
160 | T_DIGITS '=' T_NUMBER
161 {$$ = $3; Digits = ex_to_numeric($3).to_int();}
163 {$$ = $3; const_cast<symbol *>(&ex_to_symbol($1))->assign($3);}
164 | exp T_EQUAL exp {$$ = $1 == $3;}
165 | exp T_NOTEQ exp {$$ = $1 != $3;}
166 | exp '<' exp {$$ = $1 < $3;}
167 | exp T_LESSEQ exp {$$ = $1 <= $3;}
168 | exp '>' exp {$$ = $1 > $3;}
169 | exp T_GREATEREQ exp {$$ = $1 >= $3;}
170 | exp '+' exp {$$ = $1 + $3;}
171 | exp '-' exp {$$ = $1 - $3;}
172 | exp '*' exp {$$ = $1 * $3;}
173 | exp '/' exp {$$ = $1 / $3;}
174 | exp '%' exp {$$ = $1 % $3;}
175 | '-' exp %prec NEG {$$ = -$2;}
176 | '+' exp %prec NEG {$$ = $2;}
177 | exp '^' exp {$$ = power($1, $3);}
178 | exp '!' {$$ = factorial($1);}
179 | '(' exp ')' {$$ = $2;}
180 | '[' list_or_empty ']' {$$ = $2;}
181 | T_MATRIX_BEGIN matrix T_MATRIX_END {$$ = lst2matrix($2);}
184 exprseq : exp {$$ = exprseq($1);}
185 | exprseq ',' exp {exprseq es(static_cast<exprseq &>(*($1.bp))); $$ = es.append($3);}
188 list_or_empty: /* empty */ {$$ = *new lst;}
192 list : exp {$$ = lst($1);}
193 | list ',' exp {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($3);}
196 matrix : T_MATRIX_BEGIN row T_MATRIX_END {$$ = lst($2);}
197 | matrix ',' T_MATRIX_BEGIN row T_MATRIX_END {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($4);}
200 row : exp {$$ = lst($1);}
201 | row ',' exp {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($3);}
210 const int GINSH_VERSION = 0;
211 const int GINSH_REVISION = 3;
213 // Error print routine
216 cerr << s << " at " << yytext << endl;
220 // Push expression "e" onto the expression stack (for ", "" and """)
221 static void push(const ex &e)
223 exstack[2] = exstack[1];
224 exstack[1] = exstack[0];
233 static ex f_beta(const exprseq &e) {return gamma(e[0])*gamma(e[1])/gamma(e[0]+e[1]);}
234 static ex f_denom(const exprseq &e) {return e[0].denom();}
235 static ex f_eval1(const exprseq &e) {return e[0].eval();}
236 static ex f_evalf1(const exprseq &e) {return e[0].evalf();}
237 static ex f_expand(const exprseq &e) {return e[0].expand();}
238 static ex f_gcd(const exprseq &e) {return gcd(e[0], e[1]);}
239 static ex f_lcm(const exprseq &e) {return lcm(e[0], e[1]);}
240 static ex f_lsolve(const exprseq &e) {return lsolve(e[0], e[1]);}
241 static ex f_nops(const exprseq &e) {return e[0].nops();}
242 static ex f_normal1(const exprseq &e) {return e[0].normal();}
243 static ex f_numer(const exprseq &e) {return e[0].numer();}
244 static ex f_power(const exprseq &e) {return power(e[0], e[1]);}
245 static ex f_sqrt(const exprseq &e) {return sqrt(e[0]);}
246 static ex f_subs2(const exprseq &e) {return e[0].subs(e[1]);}
248 #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))
250 static ex f_charpoly(const exprseq &e)
252 CHECK_ARG(0, matrix, charpoly);
253 CHECK_ARG(1, symbol, charpoly);
254 return ex_to_matrix(e[0]).charpoly(ex_to_symbol(e[1]));
257 static ex f_coeff(const exprseq &e)
259 CHECK_ARG(1, symbol, coeff);
260 CHECK_ARG(2, numeric, coeff);
261 return e[0].coeff(ex_to_symbol(e[1]), ex_to_numeric(e[2]).to_int());
264 static ex f_collect(const exprseq &e)
266 CHECK_ARG(1, symbol, collect);
267 return e[0].collect(ex_to_symbol(e[1]));
270 static ex f_content(const exprseq &e)
272 CHECK_ARG(1, symbol, content);
273 return e[0].content(ex_to_symbol(e[1]));
276 static ex f_degree(const exprseq &e)
278 CHECK_ARG(1, symbol, degree);
279 return e[0].degree(ex_to_symbol(e[1]));
282 static ex f_determinant(const exprseq &e)
284 CHECK_ARG(0, matrix, determinant);
285 return ex_to_matrix(e[0]).determinant();
288 static ex f_diag(const exprseq &e)
291 matrix &m = *new matrix(dim, dim);
292 for (int i=0; i<dim; i++)
293 m.set(i, i, e.op(i));
297 static ex f_diff2(const exprseq &e)
299 CHECK_ARG(1, symbol, diff);
300 return e[0].diff(ex_to_symbol(e[1]));
303 static ex f_diff3(const exprseq &e)
305 CHECK_ARG(1, symbol, diff);
306 CHECK_ARG(2, numeric, diff);
307 return e[0].diff(ex_to_symbol(e[1]), ex_to_numeric(e[2]).to_int());
310 static ex f_divide(const exprseq &e)
313 if (divide(e[0], e[1], q))
319 static ex f_eval2(const exprseq &e)
321 CHECK_ARG(1, numeric, eval);
322 return e[0].eval(ex_to_numeric(e[1]).to_int());
325 static ex f_evalf2(const exprseq &e)
327 CHECK_ARG(1, numeric, evalf);
328 return e[0].evalf(ex_to_numeric(e[1]).to_int());
331 static ex f_has(const exprseq &e)
333 return e[0].has(e[1]) ? exONE() : exZERO();
336 static ex f_inverse(const exprseq &e)
338 CHECK_ARG(0, matrix, inverse);
339 return ex_to_matrix(e[0]).inverse();
342 static ex f_is(const exprseq &e)
344 CHECK_ARG(0, relational, is);
345 return (bool)ex_to_relational(e[0]) ? exONE() : exZERO();
348 static ex f_lcoeff(const exprseq &e)
350 CHECK_ARG(1, symbol, lcoeff);
351 return e[0].lcoeff(ex_to_symbol(e[1]));
354 static ex f_ldegree(const exprseq &e)
356 CHECK_ARG(1, symbol, ldegree);
357 return e[0].ldegree(ex_to_symbol(e[1]));
360 static ex f_normal2(const exprseq &e)
362 CHECK_ARG(1, numeric, normal);
363 return e[0].normal(ex_to_numeric(e[1]).to_int());
366 static ex f_op(const exprseq &e)
368 CHECK_ARG(1, numeric, op);
369 int n = ex_to_numeric(e[1]).to_int();
370 if (n < 0 || n >= e[0].nops())
371 throw(std::out_of_range("second argument to op() is out of range"));
375 static ex f_prem(const exprseq &e)
377 CHECK_ARG(2, symbol, prem);
378 return prem(e[0], e[1], ex_to_symbol(e[2]));
381 static ex f_primpart(const exprseq &e)
383 CHECK_ARG(1, symbol, primpart);
384 return e[0].primpart(ex_to_symbol(e[1]));
387 static ex f_quo(const exprseq &e)
389 CHECK_ARG(2, symbol, quo);
390 return quo(e[0], e[1], ex_to_symbol(e[2]));
393 static ex f_rem(const exprseq &e)
395 CHECK_ARG(2, symbol, rem);
396 return rem(e[0], e[1], ex_to_symbol(e[2]));
399 static ex f_series2(const exprseq &e)
401 CHECK_ARG(1, symbol, series);
402 return e[0].series(ex_to_symbol(e[1]), exZERO());
405 static ex f_series3(const exprseq &e)
407 CHECK_ARG(1, symbol, series);
408 return e[0].series(ex_to_symbol(e[1]), e[2]);
411 static ex f_series4(const exprseq &e)
413 CHECK_ARG(1, symbol, series);
414 CHECK_ARG(3, numeric, series);
415 return e[0].series(ex_to_symbol(e[1]), e[2], ex_to_numeric(e[3]).to_int());
418 static ex f_sqrfree(const exprseq &e)
420 CHECK_ARG(1, symbol, sqrfree);
421 return sqrfree(e[0], ex_to_symbol(e[1]));
424 static ex f_subs3(const exprseq &e)
426 CHECK_ARG(1, lst, subs);
427 CHECK_ARG(2, lst, subs);
428 return e[0].subs(ex_to_lst(e[1]), ex_to_lst(e[2]));
431 static ex f_tcoeff(const exprseq &e)
433 CHECK_ARG(1, symbol, tcoeff);
434 return e[0].tcoeff(ex_to_symbol(e[1]));
437 static ex f_trace(const exprseq &e)
439 CHECK_ARG(0, matrix, trace);
440 return ex_to_matrix(e[0]).trace();
443 static ex f_transpose(const exprseq &e)
445 CHECK_ARG(0, matrix, transpose);
446 return ex_to_matrix(e[0]).transpose();
449 static ex f_unassign(const exprseq &e)
451 CHECK_ARG(0, symbol, unassign);
452 (const_cast<symbol *>(&ex_to_symbol(e[0])))->unassign();
456 static ex f_unit(const exprseq &e)
458 CHECK_ARG(1, symbol, unit);
459 return e[0].unit(ex_to_symbol(e[1]));
462 static ex f_dummy(const exprseq &e)
464 throw(std::logic_error("dummy function called (shouldn't happen)"));
469 * Add all registered GiNaC functions to ginsh
472 static ex f_ginac_function(const exprseq &es, int serial)
474 return function(serial, es).eval(1);
477 void ginsh_get_ginac_functions(void)
479 vector<registered_function_info>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
482 fcns.insert(make_pair(i->name, fcn_desc(f_ginac_function, i->nparams, serial)));
490 * Find a function given a name and number of parameters. Throw exceptions on error.
493 static fcn_tab::const_iterator find_function(const ex &sym, int req_params)
495 const string &name = ex_to_symbol(sym).getname();
496 typedef fcn_tab::const_iterator I;
497 pair<I, I> b = fcns.equal_range(name);
498 if (b.first == b.second)
499 throw(std::logic_error("unknown function '" + name + "'"));
501 for (I i=b.first; i!=b.second; i++)
502 if ((i->second.num_params == 0) || (i->second.num_params == req_params))
505 throw(std::logic_error("invalid number of arguments to " + name + "()"));
510 * Convert list of lists to matrix
513 static ex lst2matrix(const ex &l)
515 if (!is_ex_of_type(l, lst))
516 throw(std::logic_error("internal error: argument to lst2matrix() is not a list"));
518 // Find number of rows and columns
519 int rows = l.nops(), cols = 0, i, j;
520 for (i=0; i<rows; i++)
521 if (l.op(i).nops() > cols)
522 cols = l.op(i).nops();
524 // Allocate and fill matrix
525 matrix &m = *new matrix(rows, cols);
526 for (i=0; i<rows; i++)
527 for (j=0; j<cols; j++)
528 if (l.op(i).nops() > j)
529 m.set(i, j, l.op(i).op(j));
531 m.set(i, j, exZERO());
537 * Function name completion functions for readline
540 static char *fcn_generator(char *text, int state)
542 static int len; // Length of word to complete
543 static fcn_tab::const_iterator index; // Iterator to function being currently considered
545 // If this is a new word to complete, initialize now
547 index = fcns.begin();
551 // Return the next function which partially matches
552 while (index != fcns.end()) {
553 const char *fcn_name = index->first.c_str();
555 if (strncmp(fcn_name, text, len) == 0)
556 return strdup(fcn_name);
561 static char **fcn_completion(char *text, int start, int end)
563 if (rl_line_buffer[0] == '!') {
564 // For shell commands, revert back to filename completion
565 rl_completion_append_character = orig_completion_append_character;
566 rl_basic_word_break_characters = orig_basic_word_break_characters;
567 return completion_matches(text, filename_completion_function);
569 // Otherwise, complete function names
570 rl_completion_append_character = '(';
571 rl_basic_word_break_characters = " \t\n\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~";
572 return completion_matches(text, fcn_generator);
581 int main(int argc, char **argv)
583 // Print banner in interactive mode
585 cout << "ginsh - GiNaC Interactive Shell V" << GINSH_VERSION << "." << GINSH_REVISION << endl;
586 cout << "Copyright (C) 1999 Johannes Gutenberg Universitaet Mainz, Germany\n";
587 cout << "This is free software, and you are welcome to redistribute it\n";
588 cout << "under certain conditions; see the file COPYING for details.\n";
591 // Init table of built-in functions
592 fcns.insert(make_pair(string("beta"), fcn_desc(f_beta, 2)));
593 fcns.insert(make_pair(string("charpoly"), fcn_desc(f_charpoly, 2)));
594 fcns.insert(make_pair(string("coeff"), fcn_desc(f_coeff, 3)));
595 fcns.insert(make_pair(string("collect"), fcn_desc(f_collect, 2)));
596 fcns.insert(make_pair(string("content"), fcn_desc(f_content, 2)));
597 fcns.insert(make_pair(string("degree"), fcn_desc(f_degree, 2)));
598 fcns.insert(make_pair(string("denom"), fcn_desc(f_denom, 1)));
599 fcns.insert(make_pair(string("determinant"), fcn_desc(f_determinant, 1)));
600 fcns.insert(make_pair(string("diag"), fcn_desc(f_diag, 0)));
601 fcns.insert(make_pair(string("diff"), fcn_desc(f_diff2, 2)));
602 fcns.insert(make_pair(string("diff"), fcn_desc(f_diff3, 3)));
603 fcns.insert(make_pair(string("divide"), fcn_desc(f_divide, 2)));
604 fcns.insert(make_pair(string("eval"), fcn_desc(f_eval1, 1)));
605 fcns.insert(make_pair(string("eval"), fcn_desc(f_eval2, 2)));
606 fcns.insert(make_pair(string("evalf"), fcn_desc(f_evalf1, 1)));
607 fcns.insert(make_pair(string("evalf"), fcn_desc(f_evalf2, 2)));
608 fcns.insert(make_pair(string("expand"), fcn_desc(f_expand, 1)));
609 fcns.insert(make_pair(string("gcd"), fcn_desc(f_gcd, 2)));
610 fcns.insert(make_pair(string("has"), fcn_desc(f_has, 2)));
611 fcns.insert(make_pair(string("inverse"), fcn_desc(f_inverse, 1)));
612 fcns.insert(make_pair(string("is"), fcn_desc(f_is, 1)));
613 fcns.insert(make_pair(string("lcm"), fcn_desc(f_lcm, 2)));
614 fcns.insert(make_pair(string("lcoeff"), fcn_desc(f_lcoeff, 2)));
615 fcns.insert(make_pair(string("ldegree"), fcn_desc(f_ldegree, 2)));
616 fcns.insert(make_pair(string("lsolve"), fcn_desc(f_lsolve, 2)));
617 fcns.insert(make_pair(string("nops"), fcn_desc(f_nops, 1)));
618 fcns.insert(make_pair(string("normal"), fcn_desc(f_normal1, 1)));
619 fcns.insert(make_pair(string("normal"), fcn_desc(f_normal2, 2)));
620 fcns.insert(make_pair(string("numer"), fcn_desc(f_numer, 1)));
621 fcns.insert(make_pair(string("op"), fcn_desc(f_op, 2)));
622 fcns.insert(make_pair(string("power"), fcn_desc(f_power, 2)));
623 fcns.insert(make_pair(string("prem"), fcn_desc(f_prem, 3)));
624 fcns.insert(make_pair(string("primpart"), fcn_desc(f_primpart, 2)));
625 fcns.insert(make_pair(string("quo"), fcn_desc(f_quo, 3)));
626 fcns.insert(make_pair(string("rem"), fcn_desc(f_rem, 3)));
627 fcns.insert(make_pair(string("series"), fcn_desc(f_series2, 2)));
628 fcns.insert(make_pair(string("series"), fcn_desc(f_series3, 3)));
629 fcns.insert(make_pair(string("series"), fcn_desc(f_series4, 4)));
630 fcns.insert(make_pair(string("sqrfree"), fcn_desc(f_sqrfree, 2)));
631 fcns.insert(make_pair(string("sqrt"), fcn_desc(f_sqrt, 1)));
632 fcns.insert(make_pair(string("subs"), fcn_desc(f_subs2, 2)));
633 fcns.insert(make_pair(string("subs"), fcn_desc(f_subs3, 3)));
634 fcns.insert(make_pair(string("tcoeff"), fcn_desc(f_tcoeff, 2)));
635 fcns.insert(make_pair(string("time"), fcn_desc(f_dummy, 0)));
636 fcns.insert(make_pair(string("trace"), fcn_desc(f_trace, 1)));
637 fcns.insert(make_pair(string("transpose"), fcn_desc(f_transpose, 1)));
638 fcns.insert(make_pair(string("unassign"), fcn_desc(f_unassign, 1)));
639 fcns.insert(make_pair(string("unit"), fcn_desc(f_unit, 2)));
640 ginsh_get_ginac_functions();
642 // Init readline completer
643 rl_readline_name = argv[0];
644 rl_attempted_completion_function = (CPPFunction *)fcn_completion;
645 orig_completion_append_character = rl_completion_append_character;
646 orig_basic_word_break_characters = rl_basic_word_break_characters;
648 // Parse input, catch all remaining exceptions
652 } catch (exception &e) {
653 cerr << e.what() << endl;