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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2982
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "error.h" |
|
29 #include "oct-obj.h" |
|
30 #include "ov.h" |
|
31 #include "pt-cmd.h" |
|
32 #include "pt-exp.h" |
|
33 #include "pt-select.h" |
|
34 #include "pt-stmt.h" |
|
35 #include "pt-walk.h" |
3724
|
36 #include "Cell.h" |
|
37 #include "ov-typeinfo.h" |
2982
|
38 |
|
39 // If clauses. |
|
40 |
|
41 tree_if_clause::~tree_if_clause (void) |
|
42 { |
|
43 delete expr; |
|
44 delete list; |
3665
|
45 delete lead_comm; |
2982
|
46 } |
|
47 |
|
48 int |
|
49 tree_if_clause::eval (void) |
|
50 { |
|
51 if (is_else_clause () || expr->is_logically_true ("if")) |
|
52 { |
|
53 if (list) |
|
54 list->eval (); |
|
55 |
|
56 return 1; |
|
57 } |
|
58 |
|
59 return 0; |
|
60 } |
|
61 |
|
62 void |
|
63 tree_if_clause::accept (tree_walker& tw) |
|
64 { |
|
65 tw.visit_if_clause (*this); |
|
66 } |
|
67 |
|
68 // List of if commands. |
|
69 |
|
70 void |
|
71 tree_if_command_list::eval (void) |
|
72 { |
4219
|
73 for (iterator p = begin (); p != end (); p++) |
2982
|
74 { |
4219
|
75 tree_if_clause *t = *p; |
2982
|
76 |
|
77 if (t->eval () || error_state) |
|
78 break; |
|
79 } |
|
80 } |
|
81 |
|
82 void |
|
83 tree_if_command_list::accept (tree_walker& tw) |
|
84 { |
|
85 tw.visit_if_command_list (*this); |
|
86 } |
|
87 |
|
88 // If. |
|
89 |
|
90 tree_if_command::~tree_if_command (void) |
|
91 { |
|
92 delete list; |
3665
|
93 delete lead_comm; |
|
94 delete trail_comm; |
2982
|
95 } |
|
96 |
|
97 void |
|
98 tree_if_command::eval (void) |
|
99 { |
|
100 if (list) |
|
101 list->eval (); |
|
102 |
3965
|
103 if (error_state) |
2982
|
104 ::error ("evaluating if command near line %d, column %d", |
|
105 line (), column ()); |
|
106 } |
|
107 |
|
108 void |
|
109 tree_if_command::accept (tree_walker& tw) |
|
110 { |
|
111 tw.visit_if_command (*this); |
|
112 } |
|
113 |
|
114 // Switch cases. |
|
115 |
|
116 tree_switch_case::~tree_switch_case (void) |
|
117 { |
|
118 delete label; |
|
119 delete list; |
3665
|
120 delete lead_comm; |
2982
|
121 } |
|
122 |
3724
|
123 |
|
124 // Compare two octave values, returning true if equal, false if not |
|
125 // XXX FIXME XXX --- should be member or friend of octave_value class. |
|
126 |
|
127 static bool |
|
128 equal (const octave_value& val, const octave_value& test) |
2982
|
129 { |
|
130 bool retval = false; |
|
131 |
3724
|
132 // If there is no op_eq for these types, we can't compare values. |
|
133 |
4223
|
134 if (val.rows () == test.rows () && val.columns () == test.columns ()) |
3724
|
135 { |
|
136 octave_value tmp = do_binary_op (octave_value::op_eq, val, test); |
|
137 |
|
138 if (! error_state && tmp.is_defined ()) |
|
139 retval = tmp.is_true (); |
|
140 } |
|
141 |
|
142 return retval; |
|
143 } |
|
144 |
|
145 bool |
|
146 tree_switch_case::label_matches (const octave_value& val) |
|
147 { |
2982
|
148 octave_value label_value = label->rvalue (); |
|
149 |
3724
|
150 if (! error_state && label_value.is_defined() ) |
2982
|
151 { |
3724
|
152 if (label_value.is_cell ()) |
2982
|
153 { |
3724
|
154 Cell cell (label_value.cell_value ()); |
|
155 |
5275
|
156 for (octave_idx_type i = 0; i < cell.rows (); i++) |
3724
|
157 { |
5275
|
158 for (octave_idx_type j = 0; j < cell.columns (); j++) |
3724
|
159 { |
|
160 bool match = equal (val, cell(i,j)); |
2982
|
161 |
3724
|
162 if (error_state) |
|
163 { |
|
164 eval_error (); |
|
165 return false; |
|
166 } |
|
167 else if (match) |
|
168 return true; |
|
169 } |
|
170 } |
|
171 } |
|
172 else |
|
173 { |
|
174 bool match = equal (val, label_value); |
|
175 |
|
176 if (error_state) |
2982
|
177 { |
3724
|
178 eval_error (); |
|
179 return false; |
2982
|
180 } |
|
181 else |
3724
|
182 return match; |
2982
|
183 } |
|
184 } |
|
185 else |
|
186 eval_error (); |
|
187 |
3724
|
188 return false; |
2982
|
189 } |
|
190 |
|
191 int |
|
192 tree_switch_case::eval (const octave_value& val) |
|
193 { |
|
194 int retval = 0; |
|
195 |
|
196 if (is_default_case () || label_matches (val)) |
|
197 { |
|
198 if (list) |
|
199 list->eval (); |
|
200 |
|
201 retval = 1; |
|
202 } |
|
203 |
|
204 return retval; |
|
205 } |
|
206 |
|
207 void |
|
208 tree_switch_case::eval_error (void) |
|
209 { |
|
210 ::error ("evaluating switch case label"); |
|
211 } |
|
212 |
|
213 void |
|
214 tree_switch_case::accept (tree_walker& tw) |
|
215 { |
|
216 tw.visit_switch_case (*this); |
|
217 } |
|
218 |
|
219 // List of switch cases. |
|
220 |
|
221 void |
|
222 tree_switch_case_list::eval (const octave_value& val) |
|
223 { |
4219
|
224 for (iterator p = begin (); p != end (); p++) |
2982
|
225 { |
4219
|
226 tree_switch_case *t = *p; |
2982
|
227 |
|
228 if (t->eval (val) || error_state) |
|
229 break; |
|
230 } |
|
231 } |
|
232 |
|
233 void |
|
234 tree_switch_case_list::accept (tree_walker& tw) |
|
235 { |
|
236 tw.visit_switch_case_list (*this); |
|
237 } |
|
238 |
|
239 // Switch. |
|
240 |
|
241 tree_switch_command::~tree_switch_command (void) |
|
242 { |
|
243 delete expr; |
|
244 delete list; |
3665
|
245 delete lead_comm; |
|
246 delete trail_comm; |
2982
|
247 } |
|
248 |
|
249 void |
|
250 tree_switch_command::eval (void) |
|
251 { |
|
252 if (expr) |
|
253 { |
|
254 octave_value val = expr->rvalue (); |
|
255 |
|
256 if (! error_state) |
|
257 { |
|
258 if (list) |
|
259 list->eval (val); |
|
260 |
|
261 if (error_state) |
|
262 eval_error (); |
|
263 } |
|
264 else |
|
265 eval_error (); |
|
266 } |
|
267 else |
|
268 ::error ("missing value in switch command near line %d, column %d", |
|
269 line (), column ()); |
|
270 } |
|
271 |
|
272 void |
|
273 tree_switch_command::eval_error (void) |
|
274 { |
|
275 ::error ("evaluating switch command near line %d, column %d", |
|
276 line (), column ()); |
|
277 } |
|
278 |
|
279 void |
|
280 tree_switch_command::accept (tree_walker& tw) |
|
281 { |
|
282 tw.visit_switch_command (*this); |
|
283 } |
|
284 |
|
285 /* |
|
286 ;;; Local Variables: *** |
|
287 ;;; mode: C++ *** |
|
288 ;;; End: *** |
|
289 */ |