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