Mercurial > hg > octave-terminal
changeset 2533:987e934bbb8c
[project @ 1996-11-19 18:21:23 by jwe]
author | jwe |
---|---|
date | Tue, 19 Nov 1996 18:21:32 +0000 |
parents | 0e5eb97cb137 |
children | c0770352f562 |
files | src/ChangeLog src/parse.y |
diffstat | 2 files changed, 196 insertions(+), 57 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,45 @@ +Tue Nov 19 12:01:13 1996 John W. Eaton <jwe@bevo.che.wisc.edu> + + * parse.y (fold): New functions for constant folding for binary + and unary expressions. Keep track of original text even when + transformations occur. + (make_binary_op, make_boolean_op, make_unary_op): Use them. + (finish_colon_expression, finish_matrix): Keep track of original + text even when transformations occur. + + * help.cc (Ftype): Don't mess with Vps4. + Handle new option `-transformed'. + + * pt-const.h, pt-const.cc (tree_constant::print): + New arg, pr_orig_text. + + * pt-exp.h, pt-exp.cc (tree_colon_expression::is_range_constant): + Delete. + + * pt-exp-base.h (tree_expression::original_text): New virtual function. + pt-exp-base.cc (tree_expression::original_text): Default version. + + * pt-pr-code.h (tree_print_code::print_original_text, + tree_print_code::prefix): New fields. + * pt-pr-code.cc (tree_print_code::visit_constant): Pass + print_original_text to tree_constant::print(). + (tree_print_code::indent): Use prefix instead of Vps4. + * pt-fcn.cc (tree_function::print_function_header, + tree_function::print_function_trailer): Pass Vps4 to + tree_print_code_constructor for prefix. + * pt-misc.cc (tree_statement::maybe_echo_code): Pass Vps4 to + tree_print_code_constructor for prefix. + + * pt-mat.h, pt-mat.cc (tree_matrix::all_elements_are_constant): + Rename from is_matrix_constant. + (tree_matrix_row::all_elements_are_constant): Likewise. + Change all callers. + +Mon Nov 18 14:13:32 1996 John W. Eaton <jwe@bevo.che.wisc.edu> + + * Makefile.in (install-inc): Try harder to create the link from + include/octave to include/octave-VERSION. + Sun Nov 17 14:14:48 1996 John W. Eaton <jwe@bevo.che.wisc.edu> * Makefile.in (parse.cc): Expect 13 shift/reduce conflicts.
--- a/src/parse.y +++ b/src/parse.y @@ -56,6 +56,7 @@ #include "pt-exp.h" #include "pt-misc.h" #include "pt-plot.h" +#include "pt-pr-code.h" #include "symtab.h" #include "token.h" #include "utils.h" @@ -707,8 +708,6 @@ expression : simple_expr { $$ = $1; } - | colon_expr - { $$ = finish_colon_expression ($1); } | NUM '=' expression { yyerror ("invalid assignment to a number"); @@ -742,6 +741,8 @@ { $$ = $1; } | variable { $$ = $1; } + | colon_expr + { $$ = finish_colon_expression ($1); } | matrix { $$ = $1; } | '[' ']' @@ -1363,6 +1364,91 @@ return new tree_plot_command (list, range, tok->pttype ()); } +static tree_expression * +fold (tree_binary_expression *e) +{ + tree_expression *retval = 0; + + tree_expression *op1 = e->lhs (); + tree_expression *op2 = e->rhs (); + + if (op1->is_constant () && op2->is_constant ()) + { + octave_value tmp = e->eval (0); + + 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; + } + else + delete e; + } + else + retval = e; + + return retval; +} + +static tree_expression * +fold (tree_unary_expression *e) +{ + tree_expression *retval = 0; + + tree_expression *op1 = e->operand (); + + if (op1->is_constant ()) + { + octave_value tmp = e->eval (0); + + 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; + } + else + delete e; + } + else + retval = e; + + return retval; +} + // Finish building a range. static tree_expression * @@ -1370,14 +1456,39 @@ { tree_expression *retval = 0; - if (e->is_range_constant ()) + tree_expression *base = e->base (); + tree_expression *limit = e->limit (); + tree_expression *incr = e->increment (); + + if (base->is_constant () && limit->is_constant () + && (! incr || (incr && incr->is_constant ()))) { octave_value tmp = e->eval (0); - delete e; + if (! error_state) + { + tree_constant *tc_retval = new tree_constant (tmp); + + ostrstream buf; + + tree_print_code tpc (buf); + + e->accept (tpc); + + buf << ends; - if (! error_state) - retval = new tree_constant (tmp); + char *s = buf.str (); + + tc_retval->stash_original_text (s); + + delete [] s; + + delete e; + + retval = tc_retval; + } + else + delete e; } else retval = e; @@ -1398,8 +1509,10 @@ switch (op) { case NUM: - retval = new tree_constant (tok_val->number (), l, c); - retval->stash_original_text (tok_val->text_rep ()); + { + retval = new tree_constant (tok_val->number (), l, c); + retval->stash_original_text (tok_val->text_rep ()); + } break; case IMAG_NUM: @@ -1428,8 +1541,6 @@ make_binary_op (int op, tree_expression *op1, token *tok_val, tree_expression *op2) { - tree_expression *retval = 0; - tree_binary_expression::type t; switch (op) @@ -1514,20 +1625,10 @@ int l = tok_val->line (); int c = tok_val->column (); - retval = new tree_binary_expression (op1, op2, l, c, t); - - if (op1->is_constant () && op2->is_constant ()) - { - octave_value tmp = retval->eval (0); + tree_binary_expression *e + = new tree_binary_expression (op1, op2, l, c, t); - delete retval; - retval = 0; - - if (! error_state) - retval = new tree_constant (tmp); - } - - return retval; + return fold (e); } // Build a boolean expression. @@ -1536,8 +1637,6 @@ make_boolean_op (int op, tree_expression *op1, token *tok_val, tree_expression *op2) { - tree_expression *retval = 0; - tree_boolean_expression::type t; switch (op) @@ -1558,20 +1657,10 @@ int l = tok_val->line (); int c = tok_val->column (); - retval = new tree_boolean_expression (op1, op2, l, c, t); - - if (op1->is_constant () && op2->is_constant ()) - { - octave_value tmp = retval->eval (0); + tree_boolean_expression *e + = new tree_boolean_expression (op1, op2, l, c, t); - delete retval; - retval = 0; - - if (! error_state) - retval = new tree_constant (tmp); - } - - return retval; + return fold (e); } // Build a prefix expression. @@ -1635,8 +1724,6 @@ static tree_expression * make_unary_op (int op, tree_expression *op1, token *tok_val) { - tree_expression *retval = 0; - tree_unary_expression::type t; switch (op) @@ -1665,20 +1752,10 @@ int l = tok_val->line (); int c = tok_val->column (); - retval = new tree_unary_expression (op1, l, c, t); - - if (op1->is_constant ()) - { - octave_value tmp = retval->eval (0); + tree_unary_expression *e + = new tree_unary_expression (op1, l, c, t); - delete retval; - retval = 0; - - if (! error_state) - retval = new tree_constant (tmp); - } - - return retval; + return fold (e); } // Build an unwind-protect command. @@ -2080,14 +2157,34 @@ lexer_flags.maybe_screwed_again--; - if (m->is_matrix_constant ()) + if (m->all_elements_are_constant ()) { octave_value tmp = m->eval (0); - delete m; + if (! error_state) + { + tree_constant *tc_retval = new tree_constant (tmp); + + ostrstream buf; + + tree_print_code tpc (buf); + + m->accept (tpc); + + buf << ends; - if (! error_state) - retval = new tree_constant (tmp); + char *s = buf.str (); + + tc_retval->stash_original_text (s); + + delete [] s; + + delete m; + + retval = tc_retval; + } + else + delete m; } else retval = m;