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 // If there is no op_eq for these types, we can't compare values. |
|
136 |
4223
|
137 if (val.rows () == test.rows () && val.columns () == test.columns ()) |
3724
|
138 { |
|
139 octave_value tmp = do_binary_op (octave_value::op_eq, val, test); |
|
140 |
|
141 if (! error_state && tmp.is_defined ()) |
|
142 retval = tmp.is_true (); |
|
143 } |
|
144 |
|
145 return retval; |
|
146 } |
|
147 |
|
148 bool |
|
149 tree_switch_case::label_matches (const octave_value& val) |
|
150 { |
2982
|
151 octave_value label_value = label->rvalue (); |
|
152 |
3724
|
153 if (! error_state && label_value.is_defined() ) |
2982
|
154 { |
3724
|
155 if (label_value.is_cell ()) |
2982
|
156 { |
3724
|
157 Cell cell (label_value.cell_value ()); |
|
158 |
|
159 for (int i = 0; i < cell.rows (); i++) |
|
160 { |
|
161 for (int j = 0; j < cell.columns (); j++) |
|
162 { |
|
163 bool match = equal (val, cell(i,j)); |
2982
|
164 |
3724
|
165 if (error_state) |
|
166 { |
|
167 eval_error (); |
|
168 return false; |
|
169 } |
|
170 else if (match) |
|
171 return true; |
|
172 } |
|
173 } |
|
174 } |
|
175 else |
|
176 { |
|
177 bool match = equal (val, label_value); |
|
178 |
|
179 if (error_state) |
2982
|
180 { |
3724
|
181 eval_error (); |
|
182 return false; |
2982
|
183 } |
|
184 else |
3724
|
185 return match; |
2982
|
186 } |
|
187 } |
|
188 else |
|
189 eval_error (); |
|
190 |
3724
|
191 return false; |
2982
|
192 } |
|
193 |
|
194 int |
|
195 tree_switch_case::eval (const octave_value& val) |
|
196 { |
|
197 int retval = 0; |
|
198 |
|
199 if (is_default_case () || label_matches (val)) |
|
200 { |
|
201 if (list) |
|
202 list->eval (); |
|
203 |
|
204 retval = 1; |
|
205 } |
|
206 |
|
207 return retval; |
|
208 } |
|
209 |
|
210 void |
|
211 tree_switch_case::eval_error (void) |
|
212 { |
|
213 ::error ("evaluating switch case label"); |
|
214 } |
|
215 |
|
216 void |
|
217 tree_switch_case::accept (tree_walker& tw) |
|
218 { |
|
219 tw.visit_switch_case (*this); |
|
220 } |
|
221 |
|
222 // List of switch cases. |
|
223 |
|
224 void |
|
225 tree_switch_case_list::eval (const octave_value& val) |
|
226 { |
4219
|
227 for (iterator p = begin (); p != end (); p++) |
2982
|
228 { |
4219
|
229 tree_switch_case *t = *p; |
2982
|
230 |
|
231 if (t->eval (val) || error_state) |
|
232 break; |
|
233 } |
|
234 } |
|
235 |
|
236 void |
|
237 tree_switch_case_list::accept (tree_walker& tw) |
|
238 { |
|
239 tw.visit_switch_case_list (*this); |
|
240 } |
|
241 |
|
242 // Switch. |
|
243 |
|
244 tree_switch_command::~tree_switch_command (void) |
|
245 { |
|
246 delete expr; |
|
247 delete list; |
3665
|
248 delete lead_comm; |
|
249 delete trail_comm; |
2982
|
250 } |
|
251 |
|
252 void |
|
253 tree_switch_command::eval (void) |
|
254 { |
|
255 if (expr) |
|
256 { |
|
257 octave_value val = expr->rvalue (); |
|
258 |
|
259 if (! error_state) |
|
260 { |
|
261 if (list) |
|
262 list->eval (val); |
|
263 |
|
264 if (error_state) |
|
265 eval_error (); |
|
266 } |
|
267 else |
|
268 eval_error (); |
|
269 } |
|
270 else |
|
271 ::error ("missing value in switch command near line %d, column %d", |
|
272 line (), column ()); |
|
273 } |
|
274 |
|
275 void |
|
276 tree_switch_command::eval_error (void) |
|
277 { |
|
278 ::error ("evaluating switch command near line %d, column %d", |
|
279 line (), column ()); |
|
280 } |
|
281 |
|
282 void |
|
283 tree_switch_command::accept (tree_walker& tw) |
|
284 { |
|
285 tw.visit_switch_command (*this); |
|
286 } |
|
287 |
|
288 /* |
|
289 ;;; Local Variables: *** |
|
290 ;;; mode: C++ *** |
|
291 ;;; End: *** |
|
292 */ |