From cfe6e9424d016b19fe09ad618b8713910a46c507 Mon Sep 17 00:00:00 2001 From: Christian Bauer Date: Wed, 5 Apr 2000 19:30:47 +0000 Subject: [PATCH] - changed comment starter from # to // - implemented #b, #o, #x and #nR prefixes for binary, octal, hexadecimal, and base-n integers - added iprint() command for decimal, octal, and hexadecimal output of integers - updated man page --- ginsh/ginsh.1 | 22 +++++++++++++++++++--- ginsh/ginsh_lexer.ll | 9 ++++++--- ginsh/ginsh_parser.yy | 18 ++++++++++++++++-- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/ginsh/ginsh.1 b/ginsh/ginsh.1 index 53e5bcdb..b84aca96 100644 --- a/ginsh/ginsh.1 +++ b/ginsh/ginsh.1 @@ -36,9 +36,12 @@ expressions on one line. Whitespace (spaces, tabs, newlines) can be applied freely between tokens. To quit ginsh, enter .BR quit " or " exit , or type an EOF (Ctrl-D) at the prompt. +.SS COMMENTS +Anything following a double slash +.RB ( // ) +up to the end of the line is treated as a comment and ignored. .SS NUMBERS -ginsh accepts numbers in all formats accepted by CLN (the Class Library for -Numbers, that is the foundation of GiNaC's numerics). This includes arbitrary +ginsh accepts numbers in the usual decimal notations. This includes arbitrary precision integers and rationals as well as floating point numbers in standard or scientific notation (e.g. .BR 1.2E6 ). @@ -46,6 +49,11 @@ The general rule is that if a number contains a decimal point .RB ( . ), it is an (inexact) floating point number; otherwise it is an (exact) integer or rational. +Integers can be specified in binary, octal, hexadecimal or arbitrary (2-36) base +by prefixing them with +.BR #b ", " #o ", " #x ", or " +.BI # n R +, respectively. .SS SYMBOLS Symbols are made up of a string of alphanumeric characters and the underscore .RB ( _ ), @@ -82,7 +90,7 @@ Archimedes' Constant .B Catalan Catalan's Constant .TP -.B gamma +.B Euler Euler-Mascheroni Constant .TP .B I @@ -369,6 +377,14 @@ will print a dump of GiNaC's internal representation for the given .IR expression . This is useful for debugging and for learning about GiNaC internals. .PP +The command +.RS +.BI iprint( expression ); +.RE +prints the given +.I expression +(which must evaluate to an integer) in decimal, octal, and hexadecimal representations. +.PP Finally, the shell escape .RS .B ! diff --git a/ginsh/ginsh_lexer.ll b/ginsh/ginsh_lexer.ll index 25040353..d27d0fe2 100644 --- a/ginsh/ginsh_lexer.ll +++ b/ginsh/ginsh_lexer.ll @@ -55,7 +55,7 @@ AN [0-9a-zA-Z_] %% [ \t\n]+ /* skip whitespace */ \\$ /* skip line continuations */ -"#".* /* skip comments starting with "#" */ +"//".* /* skip comments starting with "//" */ ^"!".* system(yytext + 1); /* execute shell command */ /* special values */ @@ -70,8 +70,7 @@ Digits yylval = (long)Digits; return T_DIGITS; quit|exit return T_QUIT; warranty return T_WARRANTY; print return T_PRINT; -read return T_READ; -write return T_WRITE; +iprint return T_IPRINT; time return T_TIME; xyzzy return T_XYZZY; inventory return T_INVENTORY; @@ -95,6 +94,10 @@ score return T_SCORE; /* numbers */ {D}+ | +"#"{D}+"R"{AN}+ | +"#b"([01])+ | +"#o"[0-7]+ | +"#x"[0-9a-fA-F]+ | {D}+"."{D}*({E})? | {D}*"."{D}+({E})? | {D}+{E} yylval = numeric(yytext); return T_NUMBER; diff --git a/ginsh/ginsh_parser.yy b/ginsh/ginsh_parser.yy index 8538ad0b..52a134f5 100644 --- a/ginsh/ginsh_parser.yy +++ b/ginsh/ginsh_parser.yy @@ -87,7 +87,7 @@ static void print_help_topics(void); %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 '=' @@ -137,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 << endl; + } catch (exception &e) { + cerr << e.what() << endl; + YYERROR; + } + } | '?' T_SYMBOL {print_help(ex_to_symbol($2).getname());} | '?' '?' {print_help_topics();} | T_QUIT {YYACCEPT;} @@ -270,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) { -- 2.44.0