]> www.ginac.de Git - ginac.git/blobdiff - ginsh/ginsh_parser.yy
- fixed a small oops: iprint() didn't reset the number output format to decimal
[ginac.git] / ginsh / ginsh_parser.yy
index 65a41f5a33a18917e56c0ea6bae303f463ad66f7..7e9049f761bcd3e55e3d747c8f6cbf7fb9b2d0f4 100644 (file)
@@ -1,8 +1,9 @@
 /** @file ginsh_parser.yy
  *
  *  Input grammar definition for ginsh.
- *  This file must be processed with yacc/bison.
- *
+ *  This file must be processed with yacc/bison. */
+
+/*
  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -39,6 +40,8 @@
 
 #include "ginsh.h"
 
+#define YYERROR_VERBOSE 1
+
 // Original readline settings
 static int orig_completion_append_character;
 static char *orig_basic_word_break_characters;
@@ -77,8 +80,6 @@ static help_tab help;
 
 static void print_help(const string &topic);
 static void print_help_topics(void);
-
-static ex lst2matrix(const ex &l);
 %}
 
 /* Tokens (T_LITERAL means a literal value returned by the parser, but not
@@ -86,7 +87,7 @@ static ex lst2matrix(const ex &l);
 %token T_NUMBER T_SYMBOL T_LITERAL T_DIGITS T_QUOTE T_QUOTE2 T_QUOTE3
 %token T_EQUAL T_NOTEQ T_LESSEQ T_GREATEREQ T_MATRIX_BEGIN T_MATRIX_END
 
-%token T_QUIT T_WARRANTY T_PRINT T_READ T_WRITE T_TIME T_XYZZY T_INVENTORY T_LOOK T_SCORE
+%token T_QUIT T_WARRANTY T_PRINT T_IPRINT T_TIME T_XYZZY T_INVENTORY T_LOOK T_SCORE
 
 /* Operator precedence and associativity */
 %right '='
@@ -136,6 +137,20 @@ line       : ';'
                        YYERROR;
                }
        }
+       | T_IPRINT '(' exp ')' ';' {
+               try {
+                       ex e = $3;
+                       if (!e.info(info_flags::integer))
+                               throw (std::invalid_argument("argument to iprint() must be an integer"));
+                       long i = ex_to_numeric(e).to_long();
+                       cout << i << endl;
+                       cout << "#o" << oct << i << endl;
+                       cout << "#x" << hex << i << dec << endl;
+               } catch (exception &e) {
+                       cerr << e.what() << endl;
+                       YYERROR;
+               }
+       }
        | '?' T_SYMBOL          {print_help(ex_to_symbol($2).getname());}
        | '?' '?'               {print_help_topics();}
        | T_QUIT                {YYACCEPT;}
@@ -206,7 +221,7 @@ exp : T_NUMBER              {$$ = $1;}
        | exp '!'               {$$ = factorial($1);}
        | '(' exp ')'           {$$ = $2;}
        | '[' list_or_empty ']' {$$ = $2;}
-       | T_MATRIX_BEGIN matrix T_MATRIX_END    {$$ = lst2matrix($2);}
+       | T_MATRIX_BEGIN matrix T_MATRIX_END    {$$ = lst_to_matrix($2);}
        ;
 
 exprseq        : exp                   {$$ = exprseq($1);}
@@ -269,7 +284,7 @@ static ex f_pow(const exprseq &e) {return pow(e[0], e[1]);}
 static ex f_sqrt(const exprseq &e) {return sqrt(e[0]);}
 static ex f_subs2(const exprseq &e) {return e[0].subs(e[1]);}
 
-#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))
+#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))
 
 static ex f_charpoly(const exprseq &e)
 {
@@ -653,33 +668,6 @@ static void print_help_topics(void)
 }
 
 
-/*
- *  Convert list of lists to matrix
- */
-
-static ex lst2matrix(const ex &l)
-{
-       if (!is_ex_of_type(l, lst))
-               throw(std::logic_error("internal error: argument to lst2matrix() is not a list"));
-
-       // Find number of rows and columns
-       unsigned rows = l.nops(), cols = 0, i, j;
-       for (i=0; i<rows; i++)
-               if (l.op(i).nops() > cols)
-                       cols = l.op(i).nops();
-
-       // Allocate and fill matrix
-       matrix &m = *new matrix(rows, cols);
-       for (i=0; i<rows; i++)
-               for (j=0; j<cols; j++)
-                       if (l.op(i).nops() > j)
-                               m.set(i, j, l.op(i).op(j));
-                       else
-                               m.set(i, j, ex(0));
-       return m;
-}
-
-
 /*
  *  Function name completion functions for readline
  */
@@ -733,6 +721,7 @@ void greeting(void)
 /*
  *  Main program
  */
+
 int main(int argc, char **argv)
 {
        // Print banner in interactive mode