577
|
1 /* |
|
2 |
1827
|
3 Copyright (C) 1996 John W. Eaton |
577
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
577
|
20 |
|
21 */ |
|
22 |
1297
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
577
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
577
|
29 #endif |
|
30 |
1350
|
31 #include <iostream.h> |
|
32 |
|
33 #ifdef HAVE_UNISTD_H |
2442
|
34 #ifdef HAVE_SYS_TYPES_H |
577
|
35 #include <sys/types.h> |
2442
|
36 #endif |
577
|
37 #include <unistd.h> |
|
38 #endif |
|
39 |
|
40 #include "error.h" |
2530
|
41 #include "input.h" |
1352
|
42 #include "oct-obj.h" |
1742
|
43 #include "pager.h" |
1670
|
44 #include "toplev.h" |
1742
|
45 #include "pt-cmd.h" |
|
46 #include "pt-exp.h" |
|
47 #include "pt-fcn.h" |
|
48 #include "pt-fvc.h" |
|
49 #include "pt-misc.h" |
|
50 #include "pt-mvr.h" |
2124
|
51 #include "pt-walk.h" |
|
52 #include "pt-pr-code.h" |
2371
|
53 #include "ov.h" |
2204
|
54 #include "variables.h" |
577
|
55 |
1058
|
56 // Nonzero means we're breaking out of a loop or function body. |
577
|
57 extern int breaking; |
|
58 |
|
59 // Nonzero means we're jumping to the end of a loop. |
|
60 extern int continuing; |
|
61 |
|
62 // Nonzero means we're returning from a function. |
|
63 extern int returning; |
|
64 |
|
65 // A list of commands to be executed. |
|
66 |
|
67 tree_statement::~tree_statement (void) |
|
68 { |
2124
|
69 delete cmd; |
|
70 delete expr; |
577
|
71 } |
|
72 |
1742
|
73 int |
|
74 tree_statement::line (void) |
|
75 { |
2124
|
76 return cmd ? cmd->line () : (expr ? expr->line () : -1); |
1742
|
77 } |
|
78 |
|
79 int |
|
80 tree_statement::column (void) |
|
81 { |
2124
|
82 return cmd ? cmd->column () : (expr ? expr->column () : -1); |
1742
|
83 } |
|
84 |
581
|
85 void |
1827
|
86 tree_statement::maybe_echo_code (bool in_function_body) |
1588
|
87 { |
|
88 if (in_function_body |
2204
|
89 && (Vecho_executing_commands & ECHO_FUNCTIONS)) |
2124
|
90 { |
2530
|
91 tree_print_code tpc (octave_stdout, Vps4); |
2124
|
92 |
|
93 accept (tpc); |
|
94 } |
1588
|
95 } |
|
96 |
|
97 void |
2124
|
98 tree_statement::accept (tree_walker& tw) |
581
|
99 { |
2124
|
100 tw.visit_statement (*this); |
581
|
101 } |
|
102 |
2086
|
103 octave_value |
1827
|
104 tree_statement_list::eval (bool print) |
577
|
105 { |
1827
|
106 bool pf; |
2086
|
107 octave_value retval; |
577
|
108 |
|
109 if (error_state) |
|
110 return retval; |
|
111 |
|
112 for (Pix p = first (); p != 0; next (p)) |
|
113 { |
|
114 tree_statement *elt = this->operator () (p); |
|
115 |
1827
|
116 if (! print) |
|
117 pf = false; |
577
|
118 else |
|
119 pf = elt->print_flag; |
|
120 |
2124
|
121 tree_command *cmd = elt->command (); |
|
122 tree_expression *expr = elt->expression (); |
577
|
123 |
|
124 if (cmd || expr) |
|
125 { |
1588
|
126 elt->maybe_echo_code (function_body); |
|
127 |
577
|
128 if (cmd) |
|
129 cmd->eval (); |
|
130 else |
|
131 retval = expr->eval (pf); |
|
132 |
|
133 if (error_state) |
2086
|
134 return octave_value (); |
577
|
135 |
|
136 if (breaking || continuing) |
|
137 break; |
|
138 |
|
139 if (returning) |
|
140 break; |
|
141 } |
|
142 else |
2086
|
143 retval = octave_value (); |
577
|
144 } |
|
145 return retval; |
|
146 } |
|
147 |
2086
|
148 octave_value_list |
1827
|
149 tree_statement_list::eval (bool print, int nargout) |
672
|
150 { |
2086
|
151 octave_value_list retval; |
672
|
152 |
|
153 if (nargout > 1) |
|
154 { |
1827
|
155 bool pf; |
672
|
156 |
|
157 if (error_state) |
|
158 return retval; |
|
159 |
|
160 for (Pix p = first (); p != 0; next (p)) |
|
161 { |
|
162 tree_statement *elt = this->operator () (p); |
|
163 |
1827
|
164 if (! print) |
|
165 pf = false; |
672
|
166 else |
|
167 pf = elt->print_flag; |
|
168 |
2124
|
169 tree_command *cmd = elt->command (); |
|
170 tree_expression *expr = elt->expression (); |
672
|
171 |
|
172 if (cmd || expr) |
|
173 { |
1588
|
174 elt->maybe_echo_code (function_body); |
|
175 |
672
|
176 if (cmd) |
|
177 cmd->eval (); |
|
178 else |
|
179 { |
|
180 if (expr->is_multi_val_ret_expression ()) |
|
181 { |
2086
|
182 octave_value_list args; |
672
|
183 tree_multi_val_ret *t = (tree_multi_val_ret *) expr; |
|
184 retval = t->eval (pf, nargout, args); |
|
185 } |
|
186 else |
|
187 retval = expr->eval (pf); |
|
188 } |
|
189 |
|
190 if (error_state) |
2086
|
191 return octave_value (); |
672
|
192 |
|
193 if (breaking || continuing) |
|
194 break; |
|
195 |
|
196 if (returning) |
|
197 break; |
|
198 } |
|
199 else |
2086
|
200 retval = octave_value_list (); |
672
|
201 } |
|
202 return retval; |
|
203 } |
|
204 else |
|
205 retval = eval (print); |
|
206 |
|
207 return retval; |
|
208 } |
|
209 |
581
|
210 void |
2124
|
211 tree_statement_list::accept (tree_walker& tw) |
581
|
212 { |
2124
|
213 tw.visit_statement_list (*this); |
581
|
214 } |
|
215 |
2086
|
216 octave_value_list |
577
|
217 tree_argument_list::convert_to_const_vector (void) |
|
218 { |
712
|
219 int len = length (); |
577
|
220 |
1358
|
221 // XXX FIXME XXX -- would be nice to know in advance how largs args |
|
222 // needs to be even when we have a list containing an all_va_args |
|
223 // token. |
922
|
224 |
2086
|
225 octave_value_list args; |
577
|
226 args.resize (len); |
|
227 |
|
228 Pix p = first (); |
922
|
229 int j = 0; |
712
|
230 for (int k = 0; k < len; k++) |
577
|
231 { |
|
232 tree_expression *elt = this->operator () (p); |
|
233 if (elt) |
|
234 { |
2086
|
235 octave_value tmp = elt->eval (false); |
577
|
236 if (error_state) |
|
237 { |
|
238 ::error ("evaluating argument list element number %d", k); |
2086
|
239 args = octave_value_list (); |
577
|
240 break; |
|
241 } |
922
|
242 else |
|
243 { |
|
244 if (tmp.is_all_va_args ()) |
|
245 { |
|
246 if (curr_function) |
|
247 { |
2086
|
248 octave_value_list tva; |
922
|
249 tva = curr_function->octave_all_va_args (); |
|
250 int n = tva.length (); |
|
251 for (int i = 0; i < n; i++) |
|
252 args(j++) = tva(i); |
|
253 } |
|
254 else |
|
255 { |
|
256 ::error ("all_va_args is only valid inside functions"); |
2086
|
257 args = octave_value_list (); |
922
|
258 break; |
|
259 } |
|
260 } |
|
261 else |
|
262 args(j++) = tmp; |
|
263 } |
577
|
264 next (p); |
|
265 } |
|
266 else |
|
267 { |
2086
|
268 args(j++) = octave_value (); |
577
|
269 break; |
|
270 } |
|
271 } |
712
|
272 |
927
|
273 args.resize (j); |
|
274 |
577
|
275 return args; |
|
276 } |
|
277 |
581
|
278 void |
2124
|
279 tree_argument_list::accept (tree_walker& tw) |
581
|
280 { |
2124
|
281 tw.visit_argument_list (*this); |
581
|
282 } |
|
283 |
577
|
284 // Parameter lists. |
|
285 |
1742
|
286 tree_parameter_list::~tree_parameter_list (void) |
|
287 { |
|
288 while (! empty ()) |
|
289 { |
|
290 tree_identifier *t = remove_front (); |
|
291 delete t; |
|
292 } |
|
293 } |
|
294 |
577
|
295 void |
|
296 tree_parameter_list::mark_as_formal_parameters (void) |
|
297 { |
|
298 for (Pix p = first (); p != 0; next (p)) |
|
299 { |
|
300 tree_identifier *elt = this->operator () (p); |
|
301 elt->mark_as_formal_parameter (); |
|
302 } |
|
303 } |
|
304 |
|
305 void |
2086
|
306 tree_parameter_list::initialize_undefined_elements (octave_value& val) |
1093
|
307 { |
|
308 for (Pix p = first (); p != 0; next (p)) |
|
309 { |
|
310 tree_identifier *elt = this->operator () (p); |
|
311 if (! elt->is_defined ()) |
2371
|
312 { |
|
313 octave_variable_reference tmp (elt); |
|
314 tmp.assign (val); |
|
315 } |
1093
|
316 } |
|
317 } |
|
318 |
|
319 void |
2086
|
320 tree_parameter_list::define_from_arg_vector (const octave_value_list& args) |
577
|
321 { |
712
|
322 int nargin = args.length (); |
|
323 |
|
324 if (nargin <= 0) |
577
|
325 return; |
|
326 |
712
|
327 int expected_nargin = length (); |
577
|
328 |
|
329 Pix p = first (); |
|
330 |
712
|
331 for (int i = 0; i < expected_nargin; i++) |
577
|
332 { |
|
333 tree_identifier *elt = this->operator () (p); |
|
334 |
2371
|
335 tree_constant *tmp = 0; |
577
|
336 |
|
337 if (i < nargin) |
|
338 { |
620
|
339 if (args(i).is_defined () && args(i).is_magic_colon ()) |
577
|
340 { |
|
341 ::error ("invalid use of colon in function argument list"); |
|
342 return; |
|
343 } |
2371
|
344 tmp = new tree_constant (args (i)); |
577
|
345 } |
|
346 |
|
347 elt->define (tmp); |
|
348 |
|
349 next (p); |
|
350 } |
|
351 } |
|
352 |
2086
|
353 octave_value_list |
723
|
354 tree_parameter_list::convert_to_const_vector (tree_va_return_list *vr_list) |
577
|
355 { |
|
356 int nout = length (); |
|
357 |
723
|
358 if (vr_list) |
|
359 nout += vr_list->length (); |
|
360 |
2086
|
361 octave_value_list retval; |
577
|
362 retval.resize (nout); |
|
363 |
|
364 int i = 0; |
|
365 |
|
366 for (Pix p = first (); p != 0; next (p)) |
|
367 { |
|
368 tree_identifier *elt = this->operator () (p); |
|
369 |
|
370 if (elt->is_defined ()) |
1827
|
371 retval(i) = elt->eval (false); |
577
|
372 |
|
373 i++; |
|
374 } |
|
375 |
723
|
376 if (vr_list) |
|
377 { |
1321
|
378 for (Pix p = vr_list->first (); p != 0; vr_list->next (p)) |
723
|
379 { |
|
380 retval(i) = vr_list->operator () (p); |
|
381 i++; |
|
382 } |
|
383 } |
|
384 |
577
|
385 return retval; |
|
386 } |
|
387 |
1827
|
388 bool |
577
|
389 tree_parameter_list::is_defined (void) |
|
390 { |
1827
|
391 bool status = true; |
577
|
392 |
|
393 for (Pix p = first (); p != 0; next (p)) |
|
394 { |
|
395 tree_identifier *elt = this->operator () (p); |
|
396 |
|
397 if (! elt->is_defined ()) |
|
398 { |
1827
|
399 status = false; |
577
|
400 break; |
|
401 } |
|
402 } |
|
403 |
|
404 return status; |
|
405 } |
|
406 |
|
407 void |
2124
|
408 tree_parameter_list::accept (tree_walker& tw) |
581
|
409 { |
2124
|
410 tw.visit_parameter_list (*this); |
581
|
411 } |
|
412 |
|
413 // Return lists. |
|
414 |
1742
|
415 tree_return_list::~tree_return_list (void) |
|
416 { |
|
417 while (! empty ()) |
|
418 { |
|
419 tree_index_expression *t = remove_front (); |
|
420 delete t; |
|
421 } |
|
422 } |
|
423 |
581
|
424 void |
2124
|
425 tree_return_list::accept (tree_walker& tw) |
581
|
426 { |
2124
|
427 tw.visit_return_list (*this); |
581
|
428 } |
|
429 |
|
430 // Global. |
|
431 |
1742
|
432 tree_global::~tree_global (void) |
|
433 { |
2124
|
434 delete id; |
|
435 delete ass_expr; |
1742
|
436 } |
|
437 |
581
|
438 void |
577
|
439 tree_global::eval (void) |
|
440 { |
2124
|
441 if (id) |
577
|
442 { |
2124
|
443 id->link_to_global (); |
577
|
444 } |
2124
|
445 else if (ass_expr) |
577
|
446 { |
2124
|
447 tree_identifier *idnt = 0; |
|
448 |
|
449 if (ass_expr->left_hand_side_is_identifier_only () |
|
450 && (idnt = ass_expr->left_hand_side_id ())) |
745
|
451 { |
2124
|
452 idnt->link_to_global (); |
|
453 ass_expr->eval (false); |
745
|
454 } |
|
455 else |
|
456 error ("global: unable to make individual structure elements global"); |
577
|
457 } |
|
458 } |
|
459 |
|
460 void |
2124
|
461 tree_global::accept (tree_walker& tw) |
581
|
462 { |
2124
|
463 tw.visit_global (*this); |
581
|
464 } |
|
465 |
|
466 // Global initializer lists. |
|
467 |
|
468 void |
577
|
469 tree_global_init_list::eval (void) |
|
470 { |
|
471 for (Pix p = first (); p != 0; next (p)) |
|
472 { |
|
473 tree_global *t = this->operator () (p); |
|
474 t->eval (); |
|
475 } |
|
476 } |
|
477 |
581
|
478 void |
2124
|
479 tree_global_init_list::accept (tree_walker& tw) |
581
|
480 { |
2124
|
481 tw.visit_global_init_list (*this); |
581
|
482 } |
|
483 |
|
484 // If. |
|
485 |
1742
|
486 tree_if_clause::~tree_if_clause (void) |
|
487 { |
|
488 delete expr; |
|
489 delete list; |
|
490 } |
|
491 |
577
|
492 int |
|
493 tree_if_clause::eval (void) |
|
494 { |
1491
|
495 if (is_else_clause () || expr->is_logically_true ("if")) |
577
|
496 { |
|
497 if (list) |
1827
|
498 list->eval (true); |
577
|
499 |
|
500 return 1; |
|
501 } |
|
502 |
|
503 return 0; |
|
504 } |
|
505 |
|
506 void |
2124
|
507 tree_if_clause::accept (tree_walker& tw) |
581
|
508 { |
2124
|
509 tw.visit_if_clause (*this); |
581
|
510 } |
|
511 |
|
512 // List of if commands. |
|
513 |
|
514 void |
577
|
515 tree_if_command_list::eval (void) |
|
516 { |
|
517 for (Pix p = first (); p != 0; next (p)) |
|
518 { |
|
519 tree_if_clause *t = this->operator () (p); |
|
520 |
|
521 if (t->eval () || error_state) |
|
522 break; |
|
523 } |
|
524 } |
|
525 |
581
|
526 void |
2124
|
527 tree_if_command_list::accept (tree_walker& tw) |
581
|
528 { |
2124
|
529 tw.visit_if_command_list (*this); |
581
|
530 } |
|
531 |
577
|
532 /* |
|
533 ;;; Local Variables: *** |
|
534 ;;; mode: C++ *** |
|
535 ;;; End: *** |
|
536 */ |