]> www.ginac.de Git - ginac.git/blob - ginsh/ginsh.y
Initial revision
[ginac.git] / ginsh / ginsh.y
1 /*
2  *  ginsh.y - GiNaC Interactive Shell, input grammar definition
3  *
4  *  This file must be processed with yacc/bison
5  */
6
7
8 /*
9  *  Definitions
10  */
11
12 %{
13 #include "config.h"
14
15 #include <sys/resource.h>
16
17 #if HAVE_UNISTD_H
18 #include <sys/types.h>
19 #include <unistd.h>
20 #endif
21
22 #if STDC_HEADERS
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #endif
27
28 extern "C" {
29 #include <readline/readline.h>
30 #include <readline/history.h>
31 }
32 #include <map>
33 #include <string>
34 #include <stdexcept>
35
36 #include <ginac.h>
37
38 #include "ginsh.h"
39
40 // Original readline settings
41 static int orig_completion_append_character;
42 static char *orig_basic_word_break_characters;
43
44 // Expression stack for ", "" and """
45 static void push(const ex &e);
46 static ex exstack[3];
47
48 // Start and end time for the time() function
49 static struct rusage start_time, end_time;
50
51 // Table of functions (a multimap, because one function may appear with different
52 // numbers of parameters)
53 typedef ex (*fcnp)(const exprseq &e);
54 typedef ex (*fcnp2)(const exprseq &e, int serial);
55
56 struct fcn_desc {
57         fcn_desc() : p(NULL), num_params(0) {}
58         fcn_desc(fcnp func, int num) : p(func), num_params(num), is_ginac(false) {}
59         fcn_desc(fcnp2 func, int num, int ser) : p((fcnp)func), num_params(num), is_ginac(true), serial(ser) {}
60
61         fcnp p;         // Pointer to function
62         int num_params; // Number of parameters (0 = arbitrary)
63         bool is_ginac;  // Flag: function is GiNaC function
64         int serial;     // GiNaC function serial number (if is_ginac == true)
65 };
66
67 typedef multimap<string, fcn_desc> fcn_tab;
68 static fcn_tab fcns;
69
70 static fcn_tab::const_iterator find_function(const ex &sym, int req_params);
71
72 static ex lst2matrix(const ex &l);
73 %}
74
75 /* Tokens (T_LITERAL means a literal value returned by the parser, but not
76    of class numeric or symbol (e.g. a constant or the FAIL object)) */
77 %token T_NUMBER T_SYMBOL T_LITERAL T_DIGITS T_QUOTE T_QUOTE2 T_QUOTE3
78 %token T_EQUAL T_NOTEQ T_LESSEQ T_GREATEREQ T_MATRIX_BEGIN T_MATRIX_END
79
80 %token T_QUIT T_PRINT T_TIME T_XYZZY T_INVENTORY T_LOOK T_SCORE
81
82 /* Operator precedence and associativity */
83 %right '='
84 %left T_EQUAL T_NOTEQ
85 %left '<' '>' T_LESSEQ T_GREATEREQ
86 %left '+' '-'
87 %left '*' '/' '%'
88 %nonassoc NEG
89 %right '^'
90 %nonassoc '!'
91
92 %start input
93
94
95 /*
96  *  Grammar rules
97  */
98
99 %%
100 input   : /* empty */
101         | input line
102         ;
103
104 line    : ';'
105         | exp ';'
106                 {
107                         try {
108                                 cout << $1 << endl;
109                                 push($1);
110                         } catch (exception &e) {
111                                 cerr << e.what() << endl;
112                                 YYERROR;
113                         }
114                 }
115         | T_PRINT '(' exp ')' ';'
116                 {
117                         try {
118                                 $3.printtree(cout);
119                         } catch (exception &e) {
120                                 cerr << e.what() << endl;
121                                 YYERROR;
122                         }
123                 }
124         | T_QUIT                {YYACCEPT;}
125         | T_XYZZY               {cout << "Nothing happens.\n";}
126         | T_INVENTORY           {cout << "You're not carrying anything.\n";}
127         | T_LOOK                {cout << "You're in a twisty little maze of passages, all alike.\n";}
128         | T_SCORE
129                 {
130                         cout << "If you were to quit now, you would score ";
131                         cout << (syms.size() > 350 ? 350 : syms.size());
132                         cout << " out of a possible 350.\n";
133                 }
134         | error ';'             {yyclearin; yyerrok;}
135         ;
136
137 exp     : T_NUMBER              {$$ = $1;}
138         | T_SYMBOL              {$$ = $1.eval();}
139         | '\'' T_SYMBOL '\''    {$$ = $2;}
140         | T_LITERAL             {$$ = $1;}
141         | T_DIGITS              {$$ = $1;}
142         | T_QUOTE               {$$ = exstack[0];}
143         | T_QUOTE2              {$$ = exstack[1];}
144         | T_QUOTE3              {$$ = exstack[2];}
145         | T_TIME {getrusage(RUSAGE_SELF, &start_time);} '(' exp ')'
146                 {
147                         getrusage(RUSAGE_SELF, &end_time);
148                         $$ = (end_time.ru_utime.tv_sec - start_time.ru_utime.tv_sec) +
149                              (end_time.ru_stime.tv_sec - start_time.ru_stime.tv_sec) +
150                              double(end_time.ru_utime.tv_usec - start_time.ru_utime.tv_usec) / 1e6 +
151                              double(end_time.ru_stime.tv_usec - start_time.ru_stime.tv_usec) / 1e6;
152                 }
153         | T_SYMBOL '(' exprseq ')'
154                 {
155                         fcn_tab::const_iterator i = find_function($1, $3.nops());
156                         if (i->second.is_ginac) {
157                                 $$ = ((fcnp2)(i->second.p))(static_cast<const exprseq &>(*($3.bp)), i->second.serial);
158                         } else {
159                                 $$ = (i->second.p)(static_cast<const exprseq &>(*($3.bp)));
160                         }
161                 }
162         | T_DIGITS '=' T_NUMBER
163                 {$$ = $3; Digits = ex_to_numeric($3).to_int();}
164         | T_SYMBOL '=' exp
165                 {$$ = $3; const_cast<symbol *>(&ex_to_symbol($1))->assign($3);}
166         | exp T_EQUAL exp       {$$ = $1 == $3;}
167         | exp T_NOTEQ exp       {$$ = $1 != $3;}
168         | exp '<' exp           {$$ = $1 < $3;}
169         | exp T_LESSEQ exp      {$$ = $1 <= $3;}
170         | exp '>' exp           {$$ = $1 > $3;}
171         | exp T_GREATEREQ exp   {$$ = $1 >= $3;}
172         | exp '+' exp           {$$ = $1 + $3;}
173         | exp '-' exp           {$$ = $1 - $3;}
174         | exp '*' exp           {$$ = $1 * $3;}
175         | exp '/' exp           {$$ = $1 / $3;}
176         | exp '%' exp           {$$ = $1 % $3;}
177         | '-' exp %prec NEG     {$$ = -$2;}
178         | '+' exp %prec NEG     {$$ = $2;}
179         | exp '^' exp           {$$ = power($1, $3);}
180         | exp '!'               {$$ = factorial($1);}
181         | '(' exp ')'           {$$ = $2;}
182         | '[' list_or_empty ']' {$$ = $2;}
183         | T_MATRIX_BEGIN matrix T_MATRIX_END    {$$ = lst2matrix($2);}
184         ;
185
186 exprseq : exp                   {$$ = exprseq($1);}
187         | exprseq ',' exp       {exprseq es(static_cast<exprseq &>(*($1.bp))); $$ = es.append($3);}
188         ;
189
190 list_or_empty: /* empty */      {$$ = *new lst;}
191         | list                  {$$ = $1;}
192         ;
193
194 list    : exp                   {$$ = lst($1);}
195         | list ',' exp          {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($3);}
196         ;
197
198 matrix  : T_MATRIX_BEGIN row T_MATRIX_END               {$$ = lst($2);}
199         | matrix ',' T_MATRIX_BEGIN row T_MATRIX_END    {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($4);}
200         ;
201
202 row     : exp                   {$$ = lst($1);}
203         | row ',' exp           {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($3);}
204         ;
205
206
207 /*
208  *  Routines
209  */
210
211 %%
212 const int GINSH_VERSION = 0;
213 const int GINSH_REVISION = 3;
214
215 // Error print routine
216 int yyerror(char *s)
217 {
218         cerr << s << " at " << yytext << endl;
219         return 0;
220 }
221
222 // Push expression "e" onto the expression stack (for ", "" and """)
223 static void push(const ex &e)
224 {
225         exstack[2] = exstack[1];
226         exstack[1] = exstack[0];
227         exstack[0] = e;
228 }
229
230
231 /*
232  *  Built-in functions
233  */
234
235 static ex f_beta(const exprseq &e) {return gamma(e[0])*gamma(e[1])/gamma(e[0]+e[1]);}
236 static ex f_denom(const exprseq &e) {return e[0].denom();}
237 static ex f_eval1(const exprseq &e) {return e[0].eval();}
238 static ex f_evalf1(const exprseq &e) {return e[0].evalf();}
239 static ex f_expand(const exprseq &e) {return e[0].expand();}
240 static ex f_gcd(const exprseq &e) {return gcd(e[0], e[1]);}
241 static ex f_lcm(const exprseq &e) {return lcm(e[0], e[1]);}
242 static ex f_lsolve(const exprseq &e) {return lsolve(e[0], e[1]);}
243 static ex f_nops(const exprseq &e) {return e[0].nops();}
244 static ex f_normal1(const exprseq &e) {return e[0].normal();}
245 static ex f_numer(const exprseq &e) {return e[0].numer();}
246 static ex f_power(const exprseq &e) {return power(e[0], e[1]);}
247 static ex f_sqrt(const exprseq &e) {return sqrt(e[0]);}
248 static ex f_subs2(const exprseq &e) {return e[0].subs(e[1]);}
249
250 #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))
251
252 static ex f_charpoly(const exprseq &e)
253 {
254         CHECK_ARG(0, matrix, charpoly);
255         CHECK_ARG(1, symbol, charpoly);
256         return ex_to_matrix(e[0]).charpoly(ex_to_symbol(e[1]));
257 }
258
259 static ex f_coeff(const exprseq &e)
260 {
261         CHECK_ARG(1, symbol, coeff);
262         CHECK_ARG(2, numeric, coeff);
263         return e[0].coeff(ex_to_symbol(e[1]), ex_to_numeric(e[2]).to_int());
264 }
265
266 static ex f_collect(const exprseq &e)
267 {
268         CHECK_ARG(1, symbol, collect);
269         return e[0].collect(ex_to_symbol(e[1]));
270 }
271
272 static ex f_content(const exprseq &e)
273 {
274         CHECK_ARG(1, symbol, content);
275         return e[0].content(ex_to_symbol(e[1]));
276 }
277
278 static ex f_degree(const exprseq &e)
279 {
280         CHECK_ARG(1, symbol, degree);
281         return e[0].degree(ex_to_symbol(e[1]));
282 }
283
284 static ex f_determinant(const exprseq &e)
285 {
286         CHECK_ARG(0, matrix, determinant);
287         return ex_to_matrix(e[0]).determinant();
288 }
289
290 static ex f_diag(const exprseq &e)
291 {
292         int dim = e.nops();
293         matrix &m = *new matrix(dim, dim);
294         for (int i=0; i<dim; i++)
295                 m.set(i, i, e.op(i));
296         return m;
297 }
298
299 static ex f_diff2(const exprseq &e)
300 {
301         CHECK_ARG(1, symbol, diff);
302         return e[0].diff(ex_to_symbol(e[1]));
303 }
304
305 static ex f_diff3(const exprseq &e)
306 {
307         CHECK_ARG(1, symbol, diff);
308         CHECK_ARG(2, numeric, diff);
309         return e[0].diff(ex_to_symbol(e[1]), ex_to_numeric(e[2]).to_int());
310 }
311
312 static ex f_divide(const exprseq &e)
313 {
314         ex q;
315         if (divide(e[0], e[1], q))
316                 return q;
317         else
318                 return *new fail();
319 }
320
321 static ex f_eval2(const exprseq &e)
322 {
323         CHECK_ARG(1, numeric, eval);
324         return e[0].eval(ex_to_numeric(e[1]).to_int());
325 }
326
327 static ex f_evalf2(const exprseq &e)
328 {
329         CHECK_ARG(1, numeric, evalf);
330         return e[0].evalf(ex_to_numeric(e[1]).to_int());
331 }
332
333 static ex f_has(const exprseq &e)
334 {
335         return e[0].has(e[1]) ? exONE() : exZERO();
336 }
337
338 static ex f_inverse(const exprseq &e)
339 {
340         CHECK_ARG(0, matrix, inverse);
341         return ex_to_matrix(e[0]).inverse();
342 }
343
344 static ex f_is(const exprseq &e)
345 {
346         CHECK_ARG(0, relational, is);
347         return (bool)ex_to_relational(e[0]) ? exONE() : exZERO();
348 }
349
350 static ex f_lcoeff(const exprseq &e)
351 {
352         CHECK_ARG(1, symbol, lcoeff);
353         return e[0].lcoeff(ex_to_symbol(e[1]));
354 }
355
356 static ex f_ldegree(const exprseq &e)
357 {
358         CHECK_ARG(1, symbol, ldegree);
359         return e[0].ldegree(ex_to_symbol(e[1]));
360 }
361
362 static ex f_normal2(const exprseq &e)
363 {
364         CHECK_ARG(1, numeric, normal);
365         return e[0].normal(ex_to_numeric(e[1]).to_int());
366 }
367
368 static ex f_op(const exprseq &e)
369 {
370         CHECK_ARG(1, numeric, op);
371         int n = ex_to_numeric(e[1]).to_int();
372         if (n < 0 || n >= e[0].nops())
373                 throw(std::out_of_range("second argument to op() is out of range"));
374         return e[0].op(n);
375 }
376
377 static ex f_prem(const exprseq &e)
378 {
379         CHECK_ARG(2, symbol, prem);
380         return prem(e[0], e[1], ex_to_symbol(e[2]));
381 }
382
383 static ex f_primpart(const exprseq &e)
384 {
385         CHECK_ARG(1, symbol, primpart);
386         return e[0].primpart(ex_to_symbol(e[1]));
387 }
388
389 static ex f_quo(const exprseq &e)
390 {
391         CHECK_ARG(2, symbol, quo);
392         return quo(e[0], e[1], ex_to_symbol(e[2]));
393 }
394
395 static ex f_rem(const exprseq &e)
396 {
397         CHECK_ARG(2, symbol, rem);
398         return rem(e[0], e[1], ex_to_symbol(e[2]));
399 }
400
401 static ex f_series2(const exprseq &e)
402 {
403         CHECK_ARG(1, symbol, series);
404         return e[0].series(ex_to_symbol(e[1]), exZERO());
405 }
406
407 static ex f_series3(const exprseq &e)
408 {
409         CHECK_ARG(1, symbol, series);
410         return e[0].series(ex_to_symbol(e[1]), e[2]);
411 }
412
413 static ex f_series4(const exprseq &e)
414 {
415         CHECK_ARG(1, symbol, series);
416         CHECK_ARG(3, numeric, series);
417         return e[0].series(ex_to_symbol(e[1]), e[2], ex_to_numeric(e[3]).to_int());
418 }
419
420 static ex f_sqrfree(const exprseq &e)
421 {
422         CHECK_ARG(1, symbol, sqrfree);
423         return sqrfree(e[0], ex_to_symbol(e[1]));
424 }
425
426 static ex f_subs3(const exprseq &e)
427 {
428         CHECK_ARG(1, lst, subs);
429         CHECK_ARG(2, lst, subs);
430         return e[0].subs(ex_to_lst(e[1]), ex_to_lst(e[2]));
431 }
432
433 static ex f_tcoeff(const exprseq &e)
434 {
435         CHECK_ARG(1, symbol, tcoeff);
436         return e[0].tcoeff(ex_to_symbol(e[1]));
437 }
438
439 static ex f_trace(const exprseq &e)
440 {
441         CHECK_ARG(0, matrix, trace);
442         return ex_to_matrix(e[0]).trace();
443 }
444
445 static ex f_transpose(const exprseq &e)
446 {
447         CHECK_ARG(0, matrix, transpose);
448         return ex_to_matrix(e[0]).transpose();
449 }
450
451 static ex f_unassign(const exprseq &e)
452 {
453         CHECK_ARG(0, symbol, unassign);
454         (const_cast<symbol *>(&ex_to_symbol(e[0])))->unassign();
455         return e[0];
456 }
457
458 static ex f_unit(const exprseq &e)
459 {
460         CHECK_ARG(1, symbol, unit);
461         return e[0].unit(ex_to_symbol(e[1]));
462 }
463
464 static ex f_dummy(const exprseq &e)
465 {
466         throw(std::logic_error("dummy function called (shouldn't happen)"));
467 }
468
469
470 /*
471  *  Add all registered GiNaC functions to ginsh
472  */
473
474 static ex f_ginac_function(const exprseq &es, int serial)
475 {
476         return function(serial, es).eval(1);
477 }
478
479 void ginsh_get_ginac_functions(void)
480 {
481         vector<registered_function_info>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
482         unsigned serial = 0;
483         while (i != end) {
484                 fcns.insert(make_pair(i->name, fcn_desc(f_ginac_function, i->nparams, serial)));
485                 i++;
486                 serial++;
487         }
488 }
489
490
491 /*
492  *  Find a function given a name and number of parameters. Throw exceptions on error.
493  */
494
495 static fcn_tab::const_iterator find_function(const ex &sym, int req_params)
496 {
497         const string &name = ex_to_symbol(sym).getname();
498         typedef fcn_tab::const_iterator I;
499         pair<I, I> b = fcns.equal_range(name);
500         if (b.first == b.second)
501                 throw(std::logic_error("unknown function '" + name + "'"));
502         else {
503                 for (I i=b.first; i!=b.second; i++)
504                         if ((i->second.num_params == 0) || (i->second.num_params == req_params))
505                                 return i;
506         }
507         throw(std::logic_error("invalid number of arguments to " + name + "()"));
508 }
509
510
511 /*
512  *  Convert list of lists to matrix
513  */
514
515 static ex lst2matrix(const ex &l)
516 {
517         if (!is_ex_of_type(l, lst))
518                 throw(std::logic_error("internal error: argument to lst2matrix() is not a list"));
519
520         // Find number of rows and columns
521         int rows = l.nops(), cols = 0, i, j;
522         for (i=0; i<rows; i++)
523                 if (l.op(i).nops() > cols)
524                         cols = l.op(i).nops();
525
526         // Allocate and fill matrix
527         matrix &m = *new matrix(rows, cols);
528         for (i=0; i<rows; i++)
529                 for (j=0; j<cols; j++)
530                         if (l.op(i).nops() > j)
531                                 m.set(i, j, l.op(i).op(j));
532                         else
533                                 m.set(i, j, exZERO());
534         return m;
535 }
536
537
538 /*
539  *  Function name completion functions for readline
540  */
541
542 static char *fcn_generator(char *text, int state)
543 {
544         static int len;                         // Length of word to complete
545         static fcn_tab::const_iterator index;   // Iterator to function being currently considered
546
547         // If this is a new word to complete, initialize now
548         if (state == 0) {
549                 index = fcns.begin();
550                 len = strlen(text);
551         }
552
553         // Return the next function which partially matches
554         while (index != fcns.end()) {
555                 const char *fcn_name = index->first.c_str();
556                 index++;
557                 if (strncmp(fcn_name, text, len) == 0)
558                         return strdup(fcn_name);
559         }
560         return NULL;
561 }
562
563 static char **fcn_completion(char *text, int start, int end)
564 {
565         if (rl_line_buffer[0] == '!') {
566                 // For shell commands, revert back to filename completion
567                 rl_completion_append_character = orig_completion_append_character;
568                 rl_basic_word_break_characters = orig_basic_word_break_characters;
569                 return completion_matches(text, filename_completion_function);
570         } else {
571                 // Otherwise, complete function names
572                 rl_completion_append_character = '(';
573                 rl_basic_word_break_characters = " \t\n\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~";
574                 return completion_matches(text, fcn_generator);
575         }
576 }
577
578
579 /*
580  *  Main program
581  */
582
583 int main(int argc, char **argv)
584 {
585         // Print banner in interactive mode
586         if (isatty(0)) {
587                 cout << "ginsh - GiNaC Interactive Shell V" << GINSH_VERSION << "." << GINSH_REVISION << endl;
588                 cout << "Copyright (C) 1999 Johannes Gutenberg Universitaet Mainz, Germany\n";
589                 cout << "This is free software, and you are welcome to redistribute it\n";
590                 cout << "under certain conditions; see the file COPYING for details.\n"; 
591         }
592
593         // Init table of built-in functions
594         fcns.insert(make_pair(string("beta"), fcn_desc(f_beta, 2)));
595         fcns.insert(make_pair(string("charpoly"), fcn_desc(f_charpoly, 2)));
596         fcns.insert(make_pair(string("coeff"), fcn_desc(f_coeff, 3)));
597         fcns.insert(make_pair(string("collect"), fcn_desc(f_collect, 2)));
598         fcns.insert(make_pair(string("content"), fcn_desc(f_content, 2)));
599         fcns.insert(make_pair(string("degree"), fcn_desc(f_degree, 2)));
600         fcns.insert(make_pair(string("denom"), fcn_desc(f_denom, 1)));
601         fcns.insert(make_pair(string("determinant"), fcn_desc(f_determinant, 1)));
602         fcns.insert(make_pair(string("diag"), fcn_desc(f_diag, 0)));
603         fcns.insert(make_pair(string("diff"), fcn_desc(f_diff2, 2)));
604         fcns.insert(make_pair(string("diff"), fcn_desc(f_diff3, 3)));
605         fcns.insert(make_pair(string("divide"), fcn_desc(f_divide, 2)));
606         fcns.insert(make_pair(string("eval"), fcn_desc(f_eval1, 1)));
607         fcns.insert(make_pair(string("eval"), fcn_desc(f_eval2, 2)));
608         fcns.insert(make_pair(string("evalf"), fcn_desc(f_evalf1, 1)));
609         fcns.insert(make_pair(string("evalf"), fcn_desc(f_evalf2, 2)));
610         fcns.insert(make_pair(string("expand"), fcn_desc(f_expand, 1)));
611         fcns.insert(make_pair(string("gcd"), fcn_desc(f_gcd, 2)));
612         fcns.insert(make_pair(string("has"), fcn_desc(f_has, 2)));
613         fcns.insert(make_pair(string("inverse"), fcn_desc(f_inverse, 1)));
614         fcns.insert(make_pair(string("is"), fcn_desc(f_is, 1)));
615         fcns.insert(make_pair(string("lcm"), fcn_desc(f_lcm, 2)));
616         fcns.insert(make_pair(string("lcoeff"), fcn_desc(f_lcoeff, 2)));
617         fcns.insert(make_pair(string("ldegree"), fcn_desc(f_ldegree, 2)));
618         fcns.insert(make_pair(string("lsolve"), fcn_desc(f_lsolve, 2)));
619         fcns.insert(make_pair(string("nops"), fcn_desc(f_nops, 1)));
620         fcns.insert(make_pair(string("normal"), fcn_desc(f_normal1, 1)));
621         fcns.insert(make_pair(string("normal"), fcn_desc(f_normal2, 2)));
622         fcns.insert(make_pair(string("numer"), fcn_desc(f_numer, 1)));
623         fcns.insert(make_pair(string("op"), fcn_desc(f_op, 2)));
624         fcns.insert(make_pair(string("power"), fcn_desc(f_power, 2)));
625         fcns.insert(make_pair(string("prem"), fcn_desc(f_prem, 3)));
626         fcns.insert(make_pair(string("primpart"), fcn_desc(f_primpart, 2)));
627         fcns.insert(make_pair(string("quo"), fcn_desc(f_quo, 3)));
628         fcns.insert(make_pair(string("rem"), fcn_desc(f_rem, 3)));
629         fcns.insert(make_pair(string("series"), fcn_desc(f_series2, 2)));
630         fcns.insert(make_pair(string("series"), fcn_desc(f_series3, 3)));
631         fcns.insert(make_pair(string("series"), fcn_desc(f_series4, 4)));
632         fcns.insert(make_pair(string("sqrfree"), fcn_desc(f_sqrfree, 2)));
633         fcns.insert(make_pair(string("sqrt"), fcn_desc(f_sqrt, 1)));
634         fcns.insert(make_pair(string("subs"), fcn_desc(f_subs2, 2)));
635         fcns.insert(make_pair(string("subs"), fcn_desc(f_subs3, 3)));
636         fcns.insert(make_pair(string("tcoeff"), fcn_desc(f_tcoeff, 2)));
637         fcns.insert(make_pair(string("time"), fcn_desc(f_dummy, 0)));
638         fcns.insert(make_pair(string("trace"), fcn_desc(f_trace, 1)));
639         fcns.insert(make_pair(string("transpose"), fcn_desc(f_transpose, 1)));
640         fcns.insert(make_pair(string("unassign"), fcn_desc(f_unassign, 1)));
641         fcns.insert(make_pair(string("unit"), fcn_desc(f_unit, 2)));
642         ginsh_get_ginac_functions();
643
644         // Init readline completer
645         rl_readline_name = argv[0];
646         rl_attempted_completion_function = (CPPFunction *)fcn_completion;
647         orig_completion_append_character = rl_completion_append_character;
648         orig_basic_word_break_characters = rl_basic_word_break_characters;
649
650         // Parse input, catch all remaining exceptions
651         int result;
652 again:  try {
653                 result = yyparse();
654         } catch (exception &e) {
655                 cerr << e.what() << endl;
656                 goto again;
657         }
658         return result;
659 }