From bug-octave-request at bevo dot che dot wisc dot edu Tue Dec 16 10:47:48 2003 Subject: type renders [fn(value)] ambiguously From: "John W. Eaton" To: Paul Kienzle Cc: bug-octave at bevo dot che dot wisc dot edu Date: Tue, 16 Dec 2003 10:47:36 -0600 On 16-Dec-2003, Paul Kienzle wrote: | octave-2.1.52, Debian: | | octave:1> function y=f, y=[sin(2)]; end | octave:2> type f | f is a user-defined function: | | function y = f () | y = [sin (2)]; | endfunction | | If you then go on to say edit('f'), edit will | use 'type' to get the current function definition | and store it in the file. When running octave with the | traditional flags set, f will then give the following | error: | | octave:11> f | error: sin: too few arguments | error: evaluating assignment expression near line 25, column 5 | error: called from `f' in file `/home/pkienzle/octave/f.m' Please try the following patch. Thanks, jwe src/ChangeLog: 2003-12-16 John W. Eaton * pt-pr-code.cc (tree_print_code::visit_complex_for_command, tree_print_code::visit_octave_user_function_header, tree_print_code::visit_matrix, tree_print_code::visit_cell, tree_print_code::visit_multi_assignment): Keep track of nearest [, {, or ( nesting level. (tree_print_code::visit_index_expression): Likewise. Check nesting level to decide how to print index. (tree_print_code::reset): Also reset nesting level stack. * pt-pr-code.h (tree_print_code::nesting): New data member. (tree_print_code::tree_print_code): Initialize it. Index: src/pt-pr-code.cc =================================================================== RCS file: /usr/local/cvsroot/octave/src/pt-pr-code.cc,v retrieving revision 1.42 diff -u -r1.42 pt-pr-code.cc --- src/pt-pr-code.cc 20 Feb 2003 08:35:55 -0000 1.42 +++ src/pt-pr-code.cc 16 Dec 2003 16:46:40 -0000 at @ -227,12 +227,14 @@ indent (); os << "for ["; + nesting.push ('['); tree_argument_list *lhs = cmd.left_hand_side (); if (lhs) lhs->accept (*this); + nesting.pop (); os << "] = "; tree_expression *expr = cmd.control_expr (); at @ -305,7 +307,10 @@ int len = ret_list->length (); if (len > 1 || takes_var_return) - os << "["; + { + os << "["; + nesting.push ('['); + } ret_list->accept (*this); at @ -318,7 +323,10 @@ } if (len > 1 || takes_var_return) - os << "]"; + { + nesting.pop (); + os << "]"; + } os << " = "; } at @ -336,7 +344,10 @@ int len = param_list->length (); if (len > 0 || takes_varargs) - os << "("; + { + os << "("; + nesting.push ('('); + } param_list->accept (*this); at @ -350,6 +361,7 @@ if (len > 0 || takes_varargs) { + nesting.pop (); os << ")"; newline (); } at @ -494,20 +506,38 @@ { case '(': { - os << " ("; + char nc = nesting.top (); + if ((nc == '[' || nc == '{') && expr.paren_count () == 0) + os << "("; + else + os << " ("; + nesting.push ('('); + tree_argument_list *l = *p_arg_lists; if (l) l->accept (*this); + + nesting.pop (); os << ")"; } break; case '{': { - os << " {"; + char nc = nesting.top (); + if ((nc == '[' || nc == '{') && expr.paren_count () == 0) + os << "{"; + else + os << " {"; + // We only care about whitespace inside [] and {} when we + // are defining matrix and cell objects, not when indexing. + nesting.push ('('); + tree_argument_list *l = *p_arg_lists; if (l) l->accept (*this); + + nesting.pop (); os << "}"; } break; at @ -539,6 +569,7 @@ print_parens (lst, "("); os << "["; + nesting.push ('['); tree_matrix::iterator p = lst.begin (); at @ -555,6 +586,7 @@ } } + nesting.pop (); os << "]"; print_parens (lst, ")"); at @ -568,6 +600,7 @@ print_parens (lst, "("); os << "{"; + nesting.push ('{'); tree_cell::iterator p = lst.begin (); at @ -584,6 +617,7 @@ } } + nesting.pop (); os << "}"; print_parens (lst, ")"); at @ -603,12 +637,18 @@ int len = lhs->length (); if (len > 1) - os << "["; + { + os << "["; + nesting.push ('['); + } lhs->accept (*this); if (len > 1) - os << "]"; + { + nesting.pop (); + os << "]"; + } } os << " " << expr.oper () << " "; at @ -1272,6 +1312,8 @@ { beginning_of_line = true; curr_print_indent_level = 0; + while (nesting.top () != 'n') + nesting.pop (); } void Index: src/pt-pr-code.h =================================================================== RCS file: /usr/local/cvsroot/octave/src/pt-pr-code.h,v retrieving revision 1.28 diff -u -r1.28 pt-pr-code.h --- src/pt-pr-code.h 20 Feb 2003 08:35:55 -0000 1.28 +++ src/pt-pr-code.h 16 Dec 2003 16:46:40 -0000 at @ -27,6 +27,7 @@ #pragma interface #endif +#include #include #include "comment-list.h" at @ -44,8 +45,13 @@ tree_print_code (std::ostream& os_arg, const std::string& pfx = std::string (), bool pr_orig_txt = true) - : os (os_arg), prefix (pfx), print_original_text (pr_orig_txt), - curr_print_indent_level (0), beginning_of_line (true) { } + : os (os_arg), prefix (pfx), nesting (), + print_original_text (pr_orig_txt), + curr_print_indent_level (0), beginning_of_line (true) + { + // For "none". + nesting.push ('n'); + } ~tree_print_code (void) { } at @ -149,6 +155,8 @@ std::string prefix; + std::stack nesting; + bool print_original_text; // Current indentation. ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------