577
|
1 // tree-misc.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #if defined (__GNUG__) |
|
29 #pragma implementation |
|
30 #endif |
|
31 |
|
32 #include <sys/types.h> |
|
33 #ifdef HAVE_UNISTD_H |
|
34 #include <unistd.h> |
|
35 #endif |
|
36 |
|
37 #include "error.h" |
|
38 #include "tree.h" |
|
39 #include "tree-misc.h" |
|
40 #include "tree-expr.h" |
|
41 #include "tree-const.h" |
|
42 #include "user-prefs.h" |
|
43 #include "oct-obj.h" |
|
44 |
|
45 // Nonzero means we're breaking out of a loop. |
|
46 extern int breaking; |
|
47 |
|
48 // Nonzero means we're jumping to the end of a loop. |
|
49 extern int continuing; |
|
50 |
|
51 // Nonzero means we're returning from a function. |
|
52 extern int returning; |
|
53 |
|
54 // A list of commands to be executed. |
|
55 |
|
56 tree_statement::~tree_statement (void) |
|
57 { |
|
58 delete command; |
|
59 delete expression; |
|
60 } |
|
61 |
|
62 tree_constant |
|
63 tree_statement_list::eval (int print) |
|
64 { |
|
65 int pf; |
|
66 tree_constant retval; |
|
67 |
|
68 if (error_state) |
|
69 return retval; |
|
70 |
|
71 for (Pix p = first (); p != 0; next (p)) |
|
72 { |
|
73 tree_statement *elt = this->operator () (p); |
|
74 |
|
75 if (print == 0) |
|
76 pf = 0; |
|
77 else |
|
78 pf = elt->print_flag; |
|
79 |
|
80 tree_command *cmd = elt->command; |
|
81 tree_expression *expr = elt->expression; |
|
82 |
|
83 if (cmd || expr) |
|
84 { |
|
85 if (cmd) |
|
86 cmd->eval (); |
|
87 else |
|
88 retval = expr->eval (pf); |
|
89 |
|
90 if (error_state) |
|
91 return tree_constant (); |
|
92 |
|
93 if (breaking || continuing) |
|
94 break; |
|
95 |
|
96 if (returning) |
|
97 break; |
|
98 } |
|
99 else |
|
100 retval = tree_constant (); |
|
101 } |
|
102 return retval; |
|
103 } |
|
104 |
|
105 Octave_object |
|
106 tree_argument_list::convert_to_const_vector (void) |
|
107 { |
|
108 int len = length () + 1; |
|
109 |
|
110 Octave_object args; |
|
111 args.resize (len); |
|
112 |
|
113 // args[0] may eventually hold something useful, like the function |
|
114 // name. |
|
115 Pix p = first (); |
|
116 for (int k = 1; k < len; k++) |
|
117 { |
|
118 tree_expression *elt = this->operator () (p); |
|
119 if (elt) |
|
120 { |
|
121 args(k) = elt->eval (0); |
|
122 if (error_state) |
|
123 { |
|
124 ::error ("evaluating argument list element number %d", k); |
|
125 break; |
|
126 } |
|
127 next (p); |
|
128 } |
|
129 else |
|
130 { |
|
131 args(k) = tree_constant (); |
|
132 break; |
|
133 } |
|
134 } |
|
135 return args; |
|
136 } |
|
137 |
|
138 // Parameter lists. |
|
139 |
|
140 void |
|
141 tree_parameter_list::mark_as_formal_parameters (void) |
|
142 { |
|
143 for (Pix p = first (); p != 0; next (p)) |
|
144 { |
|
145 tree_identifier *elt = this->operator () (p); |
|
146 elt->mark_as_formal_parameter (); |
|
147 } |
|
148 } |
|
149 |
|
150 void |
|
151 tree_parameter_list::define_from_arg_vector (const Octave_object& args) |
|
152 { |
|
153 if (args.length () <= 0) |
|
154 return; |
|
155 |
|
156 int nargin = args.length (); |
|
157 |
|
158 int expected_nargin = length () + 1; |
|
159 |
|
160 Pix p = first (); |
|
161 |
|
162 for (int i = 1; i < expected_nargin; i++) |
|
163 { |
|
164 tree_identifier *elt = this->operator () (p); |
|
165 |
|
166 tree_constant *tmp = 0; |
|
167 |
|
168 if (i < nargin) |
|
169 { |
|
170 if (args(i).is_defined () |
|
171 && (args(i).const_type () == tree_constant_rep::magic_colon)) |
|
172 { |
|
173 ::error ("invalid use of colon in function argument list"); |
|
174 return; |
|
175 } |
|
176 tmp = new tree_constant (args(i)); |
|
177 } |
|
178 |
|
179 elt->define (tmp); |
|
180 |
|
181 next (p); |
|
182 } |
|
183 } |
|
184 |
|
185 Octave_object |
|
186 tree_parameter_list::convert_to_const_vector (void) |
|
187 { |
|
188 int nout = length (); |
|
189 |
|
190 Octave_object retval; |
|
191 retval.resize (nout); |
|
192 |
|
193 int i = 0; |
|
194 |
|
195 for (Pix p = first (); p != 0; next (p)) |
|
196 { |
|
197 tree_identifier *elt = this->operator () (p); |
|
198 |
|
199 if (elt->is_defined ()) |
|
200 retval(i) = elt->eval (0); |
|
201 |
|
202 i++; |
|
203 } |
|
204 |
|
205 return retval; |
|
206 } |
|
207 |
|
208 int |
|
209 tree_parameter_list::is_defined (void) |
|
210 { |
|
211 int status = 1; |
|
212 |
|
213 for (Pix p = first (); p != 0; next (p)) |
|
214 { |
|
215 tree_identifier *elt = this->operator () (p); |
|
216 |
|
217 if (! elt->is_defined ()) |
|
218 { |
|
219 status = 0; |
|
220 break; |
|
221 } |
|
222 } |
|
223 |
|
224 return status; |
|
225 } |
|
226 |
|
227 void |
|
228 tree_global::eval (void) |
|
229 { |
|
230 if (ident) |
|
231 { |
|
232 ident->link_to_global (); |
|
233 } |
|
234 else if (assign_expr) |
|
235 { |
|
236 tree_identifier *id = assign_expr->left_hand_side (); |
|
237 |
|
238 if (id) |
|
239 id->link_to_global (); |
|
240 |
|
241 assign_expr->eval (0); |
|
242 } |
|
243 } |
|
244 |
|
245 void |
|
246 tree_global_init_list::eval (void) |
|
247 { |
|
248 for (Pix p = first (); p != 0; next (p)) |
|
249 { |
|
250 tree_global *t = this->operator () (p); |
|
251 t->eval (); |
|
252 } |
|
253 } |
|
254 |
|
255 int |
|
256 tree_if_clause::eval (void) |
|
257 { |
|
258 if (expr) |
|
259 { |
|
260 tree_constant t1 = expr->eval (0); |
|
261 |
|
262 if (error_state || t1.is_undefined ()) |
|
263 return 0; |
|
264 |
|
265 if (t1.rows () == 0 || t1.columns () == 0) |
|
266 { |
|
267 int flag = user_pref.propagate_empty_matrices; |
|
268 if (flag < 0) |
|
269 warning ("if: empty matrix used in conditional"); |
|
270 else if (flag == 0) |
|
271 { |
|
272 ::error ("if: empty matrix used in conditional"); |
|
273 return 0; |
|
274 } |
|
275 t1 = tree_constant (0.0); |
|
276 } |
|
277 else if (! t1.is_scalar_type ()) |
|
278 { |
|
279 tree_constant t2 = t1.all (); |
|
280 t1 = t2.all (); |
|
281 } |
|
282 |
|
283 int expr_value = 0; |
|
284 tree_constant_rep::constant_type t = t1.const_type (); |
|
285 if (t == tree_constant_rep::scalar_constant) |
|
286 expr_value = (int) t1.double_value (); |
|
287 else if (t == tree_constant_rep::complex_scalar_constant) |
|
288 expr_value = t1.complex_value () != 0.0; |
|
289 else |
|
290 panic_impossible (); |
|
291 |
|
292 if (expr_value) |
|
293 { |
|
294 if (list) |
|
295 list->eval (1); |
|
296 |
|
297 return 1; |
|
298 } |
|
299 } |
|
300 else |
|
301 { |
|
302 if (list) |
|
303 list->eval (1); |
|
304 |
|
305 return 1; |
|
306 } |
|
307 |
|
308 return 0; |
|
309 } |
|
310 |
|
311 void |
|
312 tree_if_command_list::eval (void) |
|
313 { |
|
314 for (Pix p = first (); p != 0; next (p)) |
|
315 { |
|
316 tree_if_clause *t = this->operator () (p); |
|
317 |
|
318 if (t->eval () || error_state) |
|
319 break; |
|
320 } |
|
321 } |
|
322 |
|
323 /* |
|
324 ;;; Local Variables: *** |
|
325 ;;; mode: C++ *** |
|
326 ;;; page-delimiter: "^/\\*" *** |
|
327 ;;; End: *** |
|
328 */ |