# HG changeset patch # User jwe # Date 939941548 0 # Node ID ad0d250ebd7e13726fe6915198b3ed138846d7df # Parent 029fac81ac659b35523687180ccaad99775c54a8 [project @ 1999-10-14 22:52:28 by jwe] diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +1999-10-14 John W. Eaton + + * parse.y (fold (tree_unary_expression *)): New function. + (make_prefix_op, make_postfix_op): Use it. + 1999-10-13 John W. Eaton * Makefile.in (oct-gperf.h): Ask for ANSI-C output from gperf diff --git a/src/parse.y b/src/parse.y --- a/src/parse.y +++ b/src/parse.y @@ -1556,7 +1556,7 @@ { tree_expression *retval = e; - unwind_protect::begin_frame ("fold"); + unwind_protect::begin_frame ("fold_binary_expression"); unwind_protect_int (error_state); @@ -1596,7 +1596,56 @@ } } - unwind_protect::run_frame ("fold"); + unwind_protect::run_frame ("fold_binary_expression"); + + return retval; +} + +static tree_expression * +fold (tree_unary_expression *e) +{ + tree_expression *retval = e; + + unwind_protect::begin_frame ("fold_unary_expression"); + + unwind_protect_int (error_state); + + unwind_protect_bool (buffer_error_messages); + buffer_error_messages = true; + + unwind_protect::add (clear_global_error_variable, 0); + + tree_expression *op = e->operand (); + + if (op->is_constant ()) + { + octave_value tmp = e->rvalue (); + + if (! error_state) + { + tree_constant *tc_retval = new tree_constant (tmp); + + ostrstream buf; + + tree_print_code tpc (buf); + + e->accept (tpc); + + buf << ends; + + char *s = buf.str (); + + tc_retval->stash_original_text (s); + + delete [] s; + + delete e; + + retval = tc_retval; + } + } + + unwind_protect::run_frame ("fold_unary_expression"); return retval; } @@ -1882,9 +1931,10 @@ int l = tok_val->line (); int c = tok_val->column (); - // XXX FIXME XXX -- what about constant folding here? - - return new tree_prefix_expression (op1, l, c, t); + tree_prefix_expression *e + = new tree_prefix_expression (op1, l, c, t); + + return fold (e); } // Build a postfix expression. @@ -1920,9 +1970,10 @@ int l = tok_val->line (); int c = tok_val->column (); - // XXX FIXME XXX -- what about constant folding here? - - return new tree_postfix_expression (op1, l, c, t); + tree_postfix_expression *e + = new tree_postfix_expression (op1, l, c, t); + + return fold (e); } // Build an unwind-protect command.