comparison libinterp/parse-tree/oct-parse.yy @ 31394:7781b1e77406

use separate class for braindead shortcircuit evaluation Change adapted from patch #10238 by Petter Tomner <tomner@kth.se>. * parse.h, oct-parse.yy (base_parser::maybe_convert_to_braindead_shortcircuit): New function. (if_cmd_list1, elseif_clause, loop_command): Use it to convert expressions that could be evaluated using Matlab-style short-circuit rules to be tree_braindead_shortcircuit_binary_expression objects instead of simple tree_binary_expression objects. * pt-exp.h (tree_expression::mark_braindead_shortcircuit): Delete. * pt-binop.h (tree_binary_expression::mark_braindead_shortcircuit): Delete. * pt-binop.h, pt-binop.cc (tree_braindead_shortcircuit_binary_expression): New class to evaluate Matlab-style short-circuit expressions. * pt-binop.h, pt-binop.cc (tree_binary_expression::preserve_operands): New function. (tree_binary_expression::m_preserve_operands): New private data member. (tree_binary_expression::m_eligible_for_braindead_shortcircuit): Delete data member. (tree_binary_expression::is_eligible_for_braindead_shortcircuit): Delete function. (tree_binary_expression::mark_braindead_shortcircuit): Delete function. (tree_binary_expression::~tree_binary_expression): Don't delete m_lhs or m_rhs if m_preserve_operands is true. (tree_binary_expression::evaluate): Don't check for or process Matlab-style short-circuit expressions here.
author John W. Eaton <jwe@octave.org>
date Thu, 03 Nov 2022 17:50:24 -0400
parents 670a0d878af1
children ef9c804676b9
comparison
equal deleted inserted replaced
31393:50f57a2be778 31394:7781b1e77406
1098 1098
1099 if_cmd_list1 : expression stmt_begin opt_sep opt_list 1099 if_cmd_list1 : expression stmt_begin opt_sep opt_list
1100 { 1100 {
1101 OCTAVE_YYUSE ($3); 1101 OCTAVE_YYUSE ($3);
1102 1102
1103 $1->mark_braindead_shortcircuit (); 1103 parser.maybe_convert_to_braindead_shortcircuit ($1);
1104 1104
1105 $$ = parser.start_if_command ($1, $4); 1105 $$ = parser.start_if_command ($1, $4);
1106 } 1106 }
1107 | if_cmd_list1 elseif_clause 1107 | if_cmd_list1 elseif_clause
1108 { $$ = parser.append_if_clause ($1, $2); } 1108 { $$ = parser.append_if_clause ($1, $2); }
1110 1110
1111 elseif_clause : ELSEIF stash_comment opt_sep expression stmt_begin opt_sep opt_list 1111 elseif_clause : ELSEIF stash_comment opt_sep expression stmt_begin opt_sep opt_list
1112 { 1112 {
1113 OCTAVE_YYUSE ($3, $6); 1113 OCTAVE_YYUSE ($3, $6);
1114 1114
1115 $4->mark_braindead_shortcircuit (); 1115 parser.maybe_convert_to_braindead_shortcircuit ($4);
1116 1116
1117 $$ = parser.make_elseif_clause ($1, $4, $7, $2); 1117 $$ = parser.make_elseif_clause ($1, $4, $7, $2);
1118 } 1118 }
1119 ; 1119 ;
1120 1120
1180 1180
1181 loop_command : WHILE stash_comment expression stmt_begin opt_sep opt_list END 1181 loop_command : WHILE stash_comment expression stmt_begin opt_sep opt_list END
1182 { 1182 {
1183 OCTAVE_YYUSE ($5); 1183 OCTAVE_YYUSE ($5);
1184 1184
1185 $3->mark_braindead_shortcircuit (); 1185 parser.maybe_convert_to_braindead_shortcircuit ($3);
1186 1186
1187 if (! ($$ = parser.make_while_command ($1, $3, $6, $7, $2))) 1187 if (! ($$ = parser.make_while_command ($1, $3, $6, $7, $2)))
1188 { 1188 {
1189 // make_while_command deleted $3 and $6. 1189 // make_while_command deleted $3 and $6.
1190 YYABORT; 1190 YYABORT;
3136 3136
3137 int l = tok_val->line (); 3137 int l = tok_val->line ();
3138 int c = tok_val->column (); 3138 int c = tok_val->column ();
3139 3139
3140 return maybe_compound_binary_expression (op1, op2, l, c, t); 3140 return maybe_compound_binary_expression (op1, op2, l, c, t);
3141 }
3142
3143 void
3144 base_parser::maybe_convert_to_braindead_shortcircuit (tree_expression*& expr)
3145 {
3146 if (expr->is_binary_expression ())
3147 {
3148 tree_binary_expression *binexp
3149 = dynamic_cast<tree_binary_expression *> (expr);
3150
3151 tree_expression *lhs = binexp->lhs ();
3152 tree_expression *rhs = binexp->rhs ();
3153
3154 maybe_convert_to_braindead_shortcircuit (lhs);
3155 maybe_convert_to_braindead_shortcircuit (rhs);
3156
3157 octave_value::binary_op op_type = binexp->op_type ();
3158 if (op_type == octave_value::op_el_and
3159 || op_type == octave_value::op_el_or)
3160 {
3161 binexp->preserve_operands ();
3162
3163 int line = expr->line ();
3164 int column = expr->column ();
3165
3166 delete expr;
3167
3168 expr = new tree_braindead_shortcircuit_binary_expression
3169 (lhs, rhs, line, column, op_type);
3170 }
3171 }
3141 } 3172 }
3142 3173
3143 // Build a boolean expression. 3174 // Build a boolean expression.
3144 3175
3145 tree_expression * 3176 tree_expression *