2982
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2982
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include "error.h" |
|
32 #include "oct-obj.h" |
|
33 #include "ov.h" |
|
34 #include "pt-cmd.h" |
|
35 #include "pt-exp.h" |
|
36 #include "pt-select.h" |
|
37 #include "pt-stmt.h" |
|
38 #include "pt-walk.h" |
3724
|
39 #include "Cell.h" |
|
40 #include "ov-typeinfo.h" |
2982
|
41 |
|
42 // If clauses. |
|
43 |
|
44 tree_if_clause::~tree_if_clause (void) |
|
45 { |
|
46 delete expr; |
|
47 delete list; |
3665
|
48 delete lead_comm; |
2982
|
49 } |
|
50 |
|
51 int |
|
52 tree_if_clause::eval (void) |
|
53 { |
|
54 if (is_else_clause () || expr->is_logically_true ("if")) |
|
55 { |
|
56 if (list) |
|
57 list->eval (); |
|
58 |
|
59 return 1; |
|
60 } |
|
61 |
|
62 return 0; |
|
63 } |
|
64 |
|
65 void |
|
66 tree_if_clause::accept (tree_walker& tw) |
|
67 { |
|
68 tw.visit_if_clause (*this); |
|
69 } |
|
70 |
|
71 // List of if commands. |
|
72 |
|
73 void |
|
74 tree_if_command_list::eval (void) |
|
75 { |
4219
|
76 for (iterator p = begin (); p != end (); p++) |
2982
|
77 { |
4219
|
78 tree_if_clause *t = *p; |
2982
|
79 |
|
80 if (t->eval () || error_state) |
|
81 break; |
|
82 } |
|
83 } |
|
84 |
|
85 void |
|
86 tree_if_command_list::accept (tree_walker& tw) |
|
87 { |
|
88 tw.visit_if_command_list (*this); |
|
89 } |
|
90 |
|
91 // If. |
|
92 |
|
93 tree_if_command::~tree_if_command (void) |
|
94 { |
|
95 delete list; |
3665
|
96 delete lead_comm; |
|
97 delete trail_comm; |
2982
|
98 } |
|
99 |
|
100 void |
|
101 tree_if_command::eval (void) |
|
102 { |
|
103 if (list) |
|
104 list->eval (); |
|
105 |
3965
|
106 if (error_state) |
2982
|
107 ::error ("evaluating if command near line %d, column %d", |
|
108 line (), column ()); |
|
109 } |
|
110 |
|
111 void |
|
112 tree_if_command::accept (tree_walker& tw) |
|
113 { |
|
114 tw.visit_if_command (*this); |
|
115 } |
|
116 |
|
117 // Switch cases. |
|
118 |
|
119 tree_switch_case::~tree_switch_case (void) |
|
120 { |
|
121 delete label; |
|
122 delete list; |
3665
|
123 delete lead_comm; |
2982
|
124 } |
|
125 |
3724
|
126 |
|
127 // Compare two octave values, returning true if equal, false if not |
|
128 // XXX FIXME XXX --- should be member or friend of octave_value class. |
|
129 |
|
130 static bool |
|
131 equal (const octave_value& val, const octave_value& test) |
2982
|
132 { |
|
133 bool retval = false; |
|
134 |
3724
|
135 int t1 = val.type_id (); |
|
136 int t2 = test.type_id (); |
|
137 |
|
138 binary_op_fcn f |
|
139 = octave_value_typeinfo::lookup_binary_op (octave_value::op_eq, t1, t2); |
|
140 |
|
141 // If there is no op_eq for these types, we can't compare values. |
|
142 |
|
143 if (f && val.rows () == test.rows () && val.columns () == test.columns ()) |
|
144 { |
|
145 octave_value tmp = do_binary_op (octave_value::op_eq, val, test); |
|
146 |
|
147 if (! error_state && tmp.is_defined ()) |
|
148 retval = tmp.is_true (); |
|
149 } |
|
150 |
|
151 return retval; |
|
152 } |
|
153 |
|
154 bool |
|
155 tree_switch_case::label_matches (const octave_value& val) |
|
156 { |
2982
|
157 octave_value label_value = label->rvalue (); |
|
158 |
3724
|
159 if (! error_state && label_value.is_defined() ) |
2982
|
160 { |
3724
|
161 if (label_value.is_cell ()) |
2982
|
162 { |
3724
|
163 Cell cell (label_value.cell_value ()); |
|
164 |
|
165 for (int i = 0; i < cell.rows (); i++) |
|
166 { |
|
167 for (int j = 0; j < cell.columns (); j++) |
|
168 { |
|
169 bool match = equal (val, cell(i,j)); |
2982
|
170 |
3724
|
171 if (error_state) |
|
172 { |
|
173 eval_error (); |
|
174 return false; |
|
175 } |
|
176 else if (match) |
|
177 return true; |
|
178 } |
|
179 } |
|
180 } |
|
181 else |
|
182 { |
|
183 bool match = equal (val, label_value); |
|
184 |
|
185 if (error_state) |
2982
|
186 { |
3724
|
187 eval_error (); |
|
188 return false; |
2982
|
189 } |
|
190 else |
3724
|
191 return match; |
2982
|
192 } |
|
193 } |
|
194 else |
|
195 eval_error (); |
|
196 |
3724
|
197 return false; |
2982
|
198 } |
|
199 |
|
200 int |
|
201 tree_switch_case::eval (const octave_value& val) |
|
202 { |
|
203 int retval = 0; |
|
204 |
|
205 if (is_default_case () || label_matches (val)) |
|
206 { |
|
207 if (list) |
|
208 list->eval (); |
|
209 |
|
210 retval = 1; |
|
211 } |
|
212 |
|
213 return retval; |
|
214 } |
|
215 |
|
216 void |
|
217 tree_switch_case::eval_error (void) |
|
218 { |
|
219 ::error ("evaluating switch case label"); |
|
220 } |
|
221 |
|
222 void |
|
223 tree_switch_case::accept (tree_walker& tw) |
|
224 { |
|
225 tw.visit_switch_case (*this); |
|
226 } |
|
227 |
|
228 // List of switch cases. |
|
229 |
|
230 void |
|
231 tree_switch_case_list::eval (const octave_value& val) |
|
232 { |
4219
|
233 for (iterator p = begin (); p != end (); p++) |
2982
|
234 { |
4219
|
235 tree_switch_case *t = *p; |
2982
|
236 |
|
237 if (t->eval (val) || error_state) |
|
238 break; |
|
239 } |
|
240 } |
|
241 |
|
242 void |
|
243 tree_switch_case_list::accept (tree_walker& tw) |
|
244 { |
|
245 tw.visit_switch_case_list (*this); |
|
246 } |
|
247 |
|
248 // Switch. |
|
249 |
|
250 tree_switch_command::~tree_switch_command (void) |
|
251 { |
|
252 delete expr; |
|
253 delete list; |
3665
|
254 delete lead_comm; |
|
255 delete trail_comm; |
2982
|
256 } |
|
257 |
|
258 void |
|
259 tree_switch_command::eval (void) |
|
260 { |
|
261 if (expr) |
|
262 { |
|
263 octave_value val = expr->rvalue (); |
|
264 |
|
265 if (! error_state) |
|
266 { |
|
267 if (list) |
|
268 list->eval (val); |
|
269 |
|
270 if (error_state) |
|
271 eval_error (); |
|
272 } |
|
273 else |
|
274 eval_error (); |
|
275 } |
|
276 else |
|
277 ::error ("missing value in switch command near line %d, column %d", |
|
278 line (), column ()); |
|
279 } |
|
280 |
|
281 void |
|
282 tree_switch_command::eval_error (void) |
|
283 { |
|
284 ::error ("evaluating switch command near line %d, column %d", |
|
285 line (), column ()); |
|
286 } |
|
287 |
|
288 void |
|
289 tree_switch_command::accept (tree_walker& tw) |
|
290 { |
|
291 tw.visit_switch_command (*this); |
|
292 } |
|
293 |
|
294 /* |
|
295 ;;; Local Variables: *** |
|
296 ;;; mode: C++ *** |
|
297 ;;; End: *** |
|
298 */ |