comparison libinterp/parse-tree/pt-binop.cc @ 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 c3ed2ad87aca
children e88a07dec498
comparison
equal deleted inserted replaced
31393:50f57a2be778 31394:7781b1e77406
67 } 67 }
68 68
69 octave_value 69 octave_value
70 tree_binary_expression::evaluate (tree_evaluator& tw, int) 70 tree_binary_expression::evaluate (tree_evaluator& tw, int)
71 { 71 {
72 octave_value val;
73
74 if (is_eligible_for_braindead_shortcircuit ())
75 {
76 if (m_lhs)
77 {
78 octave_value a = m_lhs->evaluate (tw);
79
80 if (a.ndims () == 2 && a.rows () == 1 && a.columns () == 1)
81 {
82 bool result = false;
83
84 bool a_true = a.is_true ();
85
86 if (a_true)
87 {
88 if (m_etype == octave_value::op_el_or)
89 {
90 matlab_style_short_circuit_warning ("|");
91 return octave_value (true);
92 }
93 }
94 else
95 {
96 if (m_etype == octave_value::op_el_and)
97 {
98 matlab_style_short_circuit_warning ("&");
99 return octave_value (false);
100 }
101 }
102
103 if (m_rhs)
104 {
105 octave_value b = m_rhs->evaluate (tw);
106
107 result = b.is_true ();
108 }
109
110 return octave_value (result);
111 }
112 }
113 }
114
115 if (m_lhs) 72 if (m_lhs)
116 { 73 {
117 octave_value a = m_lhs->evaluate (tw); 74 octave_value a = m_lhs->evaluate (tw);
118 75
119 if (a.is_defined () && m_rhs) 76 if (a.is_defined () && m_rhs)
133 90
134 interpreter& interp = tw.get_interpreter (); 91 interpreter& interp = tw.get_interpreter ();
135 92
136 type_info& ti = interp.get_type_info (); 93 type_info& ti = interp.get_type_info ();
137 94
138 val = binary_op (ti, m_etype, a, b); 95 return binary_op (ti, m_etype, a, b);
139 } 96 }
140 } 97 }
141 } 98 }
142 99
143 return val; 100 return octave_value ();
101 }
102
103 tree_expression *
104 tree_braindead_shortcircuit_binary_expression::dup (symbol_scope& scope) const
105 {
106 tree_braindead_shortcircuit_binary_expression *new_be
107 = new tree_braindead_shortcircuit_binary_expression
108 (m_lhs ? m_lhs->dup (scope) : nullptr,
109 m_rhs ? m_rhs->dup (scope) : nullptr,
110 line (), column (), op_type ());
111
112 new_be->copy_base (*this);
113
114 return new_be;
115 }
116
117 octave_value
118 tree_braindead_shortcircuit_binary_expression::evaluate (tree_evaluator& tw,
119 int)
120 {
121 if (m_lhs)
122 {
123 octave_value a = m_lhs->evaluate (tw);
124
125 if (a.ndims () == 2 && a.rows () == 1 && a.columns () == 1)
126 {
127 bool result = false;
128
129 bool a_true = a.is_true ();
130
131 octave_value::binary_op oper_type = op_type ();
132
133 if (a_true)
134 {
135 if (oper_type == octave_value::op_el_or)
136 {
137 matlab_style_short_circuit_warning ("|");
138 return octave_value (true);
139 }
140 }
141 else
142 {
143 if (oper_type == octave_value::op_el_and)
144 {
145 matlab_style_short_circuit_warning ("&");
146 return octave_value (false);
147 }
148 }
149
150 if (m_rhs)
151 {
152 octave_value b = m_rhs->evaluate (tw);
153
154 result = b.is_true ();
155 }
156
157 return octave_value (result);
158 }
159 }
160
161 return octave_value ();
144 } 162 }
145 163
146 // Boolean expressions. 164 // Boolean expressions.
147 165
148 std::string 166 std::string