From eaa0370ac188561e02195a377b252b4caf5ab57a Mon Sep 17 00:00:00 2001 From: Alexei Sheplyakov Date: Tue, 9 Sep 2008 10:39:26 +0400 Subject: [PATCH] ginsh: use exmap for storing assigned symbols. C++ already have associative arrays, there's no need to re-invent them. --- ginsh/ginsh.1.in | 4 ++-- ginsh/ginsh_parser.yy | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/ginsh/ginsh.1.in b/ginsh/ginsh.1.in index af18f4e7..90a60fc1 100644 --- a/ginsh/ginsh.1.in +++ b/ginsh/ginsh.1.in @@ -397,8 +397,8 @@ detail here. Please refer to the GiNaC documentation. .BI transpose( matrix ) \- transpose of a matrix .br -.BI unassign( symbol ) -\- unassign an assigned symbol +.BI unassign( 'symbol' ) +\- unassign an assigned symbol (mind the quotes, please!) .br .BI unit( expression ", " symbol ) \- unit part of a polynomial diff --git a/ginsh/ginsh_parser.yy b/ginsh/ginsh_parser.yy index b8c6875a..0594d948 100644 --- a/ginsh/ginsh_parser.yy +++ b/ginsh/ginsh_parser.yy @@ -64,6 +64,8 @@ static const char *orig_basic_word_break_characters; // Expression stack for %, %% and %%% static void push(const ex &e); static ex exstack[3]; +// Assigned symbols +static exmap assigned_symbol_table; // Start and end time for the time() function #ifdef HAVE_RUSAGE @@ -237,7 +239,13 @@ line : ';' ; exp : T_NUMBER {$$ = $1;} - | T_SYMBOL {$$ = $1.eval();} + | T_SYMBOL { + exmap::const_iterator i = assigned_symbol_table.find($1); + if (i == assigned_symbol_table.end()) + $$ = $1; + else + $$ = i->second.eval(); + } | '\'' T_SYMBOL '\'' {$$ = $2;} | T_LITERAL {$$ = $1;} | T_DIGITS {$$ = $1;} @@ -253,7 +261,7 @@ exp : T_NUMBER {$$ = $1;} } } | T_DIGITS '=' T_NUMBER {$$ = $3; Digits = ex_to($3).to_int();} - | T_SYMBOL '=' exp {$$ = $3; const_cast(ex_to($1)).assign($3);} + | T_SYMBOL '=' exp {$$ = $3; assigned_symbol_table[$1] = $3; } | exp T_EQUAL exp {$$ = $1 == $3;} | exp T_NOTEQ exp {$$ = $1 != $3;} | exp '<' exp {$$ = $1 < $3;} @@ -567,7 +575,9 @@ static ex f_transpose(const exprseq &e) static ex f_unassign(const exprseq &e) { CHECK_ARG(0, symbol, unassign); - const_cast(ex_to(e[0])).unassign(); + exmap::iterator i = assigned_symbol_table.find(e[0]); + if (i != assigned_symbol_table.end()) + assigned_symbol_table.erase(i); return e[0]; } @@ -900,6 +910,7 @@ int main(int argc, char **argv) // Print banner in interactive mode if (isatty(0)) greeting(); + assigned_symbol_table = exmap(); // Init function table insert_fcns(builtin_fcns); -- 2.44.0