529
|
1 // tree-cmd.cc -*- C++ -*- |
494
|
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 <iostream.h> |
|
33 |
578
|
34 // Nonzero means we're breaking out of a loop. |
|
35 int breaking = 0; |
|
36 |
|
37 // Nonzero means we're jumping to the end of a loop. |
|
38 int continuing = 0; |
|
39 |
|
40 // Nonzero means we're returning from a function. Global because it |
|
41 // is also needed in tree-expr.cc. |
|
42 int returning = 0; |
|
43 |
494
|
44 #include "user-prefs.h" |
|
45 #include "variables.h" |
|
46 #include "symtab.h" |
|
47 #include "error.h" |
|
48 #include "gripes.h" |
584
|
49 #include "tree-base.h" |
578
|
50 #include "tree-expr.h" |
494
|
51 #include "tree-cmd.h" |
578
|
52 #include "tree-misc.h" |
494
|
53 #include "tree-const.h" |
|
54 |
|
55 // Decide if it's time to quit a for or while loop. |
|
56 static int |
|
57 quit_loop_now (void) |
|
58 { |
|
59 // Maybe handle `continue N' someday... |
|
60 |
|
61 if (continuing) |
|
62 continuing--; |
|
63 |
|
64 int quit = (returning || breaking || continuing); |
|
65 |
|
66 if (breaking) |
|
67 breaking--; |
|
68 |
|
69 return quit; |
|
70 } |
|
71 |
|
72 // But first, some extra functions used by the tree classes. |
|
73 |
|
74 // We seem to have no use for this now. Maybe it will be needed at |
|
75 // some future date, so here it is. |
|
76 #if 0 |
578
|
77 |
|
78 // Convert a linked list of trees to a vector of pointers to trees. |
|
79 |
494
|
80 static tree ** |
|
81 list_to_vector (tree *list, int& len) |
|
82 { |
|
83 len = list->length () + 1; |
|
84 |
|
85 tree **args = new tree * [len]; |
|
86 |
|
87 // args[0] may eventually hold something useful, like the function |
|
88 // name. |
|
89 tree *tmp_list = list; |
|
90 for (int k = 1; k < len; k++) |
|
91 { |
|
92 args[k] = tmp_list; |
|
93 tmp_list = tmp_list->next_elem (); |
|
94 } |
|
95 return args; |
|
96 } |
|
97 #endif |
|
98 |
578
|
99 // Global. |
494
|
100 |
578
|
101 tree_global_command::~tree_global_command (void) |
494
|
102 { |
578
|
103 delete init_list; |
494
|
104 } |
|
105 |
|
106 void |
578
|
107 tree_global_command::eval (void) |
494
|
108 { |
578
|
109 if (init_list) |
|
110 init_list->eval (); |
494
|
111 |
|
112 if (error_state > 0) |
|
113 ::error ("evaluating global command near line %d, column %d", |
|
114 line (), column ()); |
|
115 } |
|
116 |
581
|
117 void |
|
118 tree_global_command::print_code (ostream& os) |
|
119 { |
|
120 print_code_indent (os); |
|
121 |
|
122 os << "global "; |
|
123 |
|
124 if (init_list) |
|
125 init_list->print_code (os); |
|
126 } |
|
127 |
578
|
128 // While. |
494
|
129 |
|
130 tree_while_command::~tree_while_command (void) |
|
131 { |
|
132 delete expr; |
|
133 delete list; |
|
134 } |
|
135 |
578
|
136 void |
|
137 tree_while_command::eval (void) |
494
|
138 { |
|
139 if (error_state) |
578
|
140 return; |
494
|
141 |
|
142 for (;;) |
|
143 { |
|
144 int expr_value = 0; |
529
|
145 if (! expr) |
578
|
146 return; |
494
|
147 tree_constant t1 = expr->eval (0); |
|
148 |
|
149 if (error_state) |
|
150 { |
|
151 eval_error (); |
578
|
152 return; |
494
|
153 } |
|
154 |
|
155 if (t1.rows () == 0 || t1.columns () == 0) |
|
156 { |
|
157 int flag = user_pref.propagate_empty_matrices; |
|
158 if (flag < 0) |
|
159 warning ("while: empty matrix used in conditional"); |
|
160 else if (flag == 0) |
|
161 { |
|
162 ::error ("while: empty matrix used in conditional"); |
578
|
163 return; |
494
|
164 } |
|
165 t1 = tree_constant (0.0); |
|
166 } |
|
167 else if (! t1.is_scalar_type ()) |
|
168 { |
|
169 tree_constant t2 = t1.all (); |
|
170 t1 = t2.all (); |
|
171 } |
|
172 |
|
173 tree_constant_rep::constant_type t = t1.const_type (); |
|
174 if (t == tree_constant_rep::scalar_constant) |
|
175 expr_value = (int) t1.double_value (); |
|
176 else if (t == tree_constant_rep::complex_scalar_constant) |
|
177 expr_value = t1.complex_value () != 0.0; |
|
178 else |
|
179 panic_impossible (); |
|
180 |
|
181 if (expr_value) |
|
182 { |
529
|
183 if (list) |
494
|
184 { |
578
|
185 list->eval (1); |
494
|
186 if (error_state) |
|
187 { |
|
188 eval_error (); |
578
|
189 return; |
494
|
190 } |
|
191 } |
|
192 |
|
193 if (quit_loop_now ()) |
|
194 break; |
|
195 } |
|
196 else |
|
197 break; |
|
198 } |
|
199 } |
|
200 |
|
201 void |
|
202 tree_while_command::eval_error (void) |
|
203 { |
|
204 if (error_state > 0) |
|
205 ::error ("evaluating while command near line %d, column %d", |
|
206 line (), column ()); |
|
207 } |
|
208 |
581
|
209 void |
|
210 tree_while_command::print_code (ostream& os) |
|
211 { |
|
212 print_code_indent (os); |
|
213 |
|
214 os << "while "; |
|
215 |
|
216 if (expr) |
|
217 expr->print_code (os); |
|
218 |
|
219 print_code_new_line (os); |
|
220 |
|
221 if (list) |
|
222 { |
|
223 increment_indent_level (); |
|
224 list->print_code (os); |
|
225 decrement_indent_level (); |
|
226 } |
|
227 |
|
228 print_code_indent (os); |
|
229 |
|
230 os << "endwhile"; |
|
231 } |
|
232 |
578
|
233 // For. |
494
|
234 |
|
235 tree_for_command::~tree_for_command (void) |
|
236 { |
|
237 delete id; |
|
238 delete expr; |
|
239 delete list; |
|
240 } |
|
241 |
578
|
242 void |
|
243 tree_for_command::eval (void) |
494
|
244 { |
529
|
245 if (error_state || ! expr) |
578
|
246 return; |
494
|
247 |
|
248 tree_constant tmp_expr = expr->eval (0); |
|
249 |
|
250 if (error_state || tmp_expr.is_undefined ()) |
|
251 { |
|
252 eval_error (); |
578
|
253 return; |
494
|
254 } |
|
255 |
|
256 tree_constant_rep::constant_type expr_type = tmp_expr.const_type (); |
|
257 switch (expr_type) |
|
258 { |
|
259 case tree_constant_rep::complex_scalar_constant: |
|
260 case tree_constant_rep::scalar_constant: |
|
261 { |
|
262 tree_constant *rhs = new tree_constant (tmp_expr); |
|
263 int quit = 0; |
578
|
264 do_for_loop_once (rhs, quit); |
494
|
265 } |
|
266 break; |
|
267 case tree_constant_rep::complex_matrix_constant: |
|
268 case tree_constant_rep::matrix_constant: |
|
269 { |
|
270 Matrix m_tmp; |
|
271 ComplexMatrix cm_tmp; |
|
272 int nr; |
|
273 int steps; |
|
274 if (expr_type == tree_constant_rep::matrix_constant) |
|
275 { |
|
276 m_tmp = tmp_expr.matrix_value (); |
|
277 nr = m_tmp.rows (); |
|
278 steps = m_tmp.columns (); |
|
279 } |
|
280 else |
|
281 { |
|
282 cm_tmp = tmp_expr.complex_matrix_value (); |
|
283 nr = cm_tmp.rows (); |
|
284 steps = cm_tmp.columns (); |
|
285 } |
|
286 |
|
287 for (int i = 0; i < steps; i++) |
|
288 { |
|
289 tree_constant *rhs; |
|
290 |
|
291 if (nr == 1) |
|
292 { |
|
293 if (expr_type == tree_constant_rep::matrix_constant) |
|
294 rhs = new tree_constant (m_tmp (0, i)); |
|
295 else |
|
296 rhs = new tree_constant (cm_tmp (0, i)); |
|
297 } |
|
298 else |
|
299 { |
|
300 if (expr_type == tree_constant_rep::matrix_constant) |
|
301 rhs = new tree_constant (m_tmp.extract (0, i, nr-1, i)); |
|
302 else |
|
303 rhs = new tree_constant (cm_tmp.extract (0, i, nr-1, i)); |
|
304 } |
|
305 |
|
306 int quit = 0; |
578
|
307 do_for_loop_once (rhs, quit); |
494
|
308 if (quit) |
|
309 break; |
|
310 } |
|
311 } |
|
312 break; |
|
313 case tree_constant_rep::string_constant: |
|
314 gripe_string_invalid (); |
|
315 break; |
|
316 case tree_constant_rep::range_constant: |
|
317 { |
|
318 Range rng = tmp_expr.range_value (); |
|
319 |
|
320 int steps = rng.nelem (); |
|
321 double b = rng.base (); |
|
322 double increment = rng.inc (); |
|
323 |
|
324 for (int i = 0; i < steps; i++) |
|
325 { |
|
326 double tmp_val = b + i * increment; |
|
327 |
|
328 tree_constant *rhs = new tree_constant (tmp_val); |
|
329 |
|
330 int quit = 0; |
578
|
331 do_for_loop_once (rhs, quit); |
494
|
332 if (quit) |
|
333 break; |
|
334 } |
|
335 } |
|
336 break; |
|
337 default: |
|
338 panic_impossible (); |
|
339 break; |
|
340 } |
|
341 } |
|
342 |
|
343 void |
|
344 tree_for_command::eval_error (void) |
|
345 { |
|
346 if (error_state > 0) |
|
347 ::error ("evaluating for command near line %d, column %d", |
|
348 line (), column ()); |
|
349 } |
|
350 |
578
|
351 void |
494
|
352 tree_for_command::do_for_loop_once (tree_constant *rhs, int& quit) |
|
353 { |
|
354 quit = 0; |
|
355 |
578
|
356 tree_simple_assignment_expression tmp_ass (id, rhs, 1); |
494
|
357 |
|
358 tmp_ass.eval (0); |
|
359 |
|
360 if (error_state) |
|
361 { |
|
362 eval_error (); |
578
|
363 return; |
494
|
364 } |
|
365 |
529
|
366 if (list) |
494
|
367 { |
578
|
368 list->eval (1); |
494
|
369 if (error_state) |
|
370 { |
|
371 eval_error (); |
|
372 quit = 1; |
578
|
373 return; |
494
|
374 } |
|
375 } |
|
376 |
|
377 quit = quit_loop_now (); |
|
378 } |
|
379 |
581
|
380 void |
|
381 tree_for_command::print_code (ostream& os) |
|
382 { |
|
383 print_code_indent (os); |
|
384 |
|
385 os << "for "; |
|
386 |
|
387 if (id) |
|
388 id->print_code (os); |
|
389 |
|
390 os << " = "; |
|
391 |
|
392 if (expr) |
|
393 expr->print_code (os); |
|
394 |
|
395 print_code_new_line (os); |
|
396 |
|
397 if (list) |
|
398 { |
|
399 increment_indent_level (); |
|
400 list->print_code (os); |
|
401 decrement_indent_level (); |
|
402 } |
|
403 |
|
404 print_code_indent (os); |
|
405 |
|
406 os << "endfor"; |
|
407 } |
|
408 |
578
|
409 // If. |
494
|
410 |
|
411 tree_if_command::~tree_if_command (void) |
|
412 { |
|
413 delete list; |
|
414 } |
|
415 |
|
416 void |
578
|
417 tree_if_command::eval (void) |
494
|
418 { |
578
|
419 if (list) |
|
420 list->eval (); |
|
421 |
494
|
422 if (error_state > 0) |
|
423 ::error ("evaluating if command near line %d, column %d", |
|
424 line (), column ()); |
|
425 } |
|
426 |
581
|
427 void |
|
428 tree_if_command::print_code (ostream& os) |
|
429 { |
|
430 print_code_indent (os); |
|
431 |
|
432 os << "if "; |
|
433 |
|
434 if (list) |
|
435 list->print_code (os); |
|
436 |
|
437 print_code_indent (os); |
|
438 |
|
439 os << "endif"; |
|
440 } |
|
441 |
578
|
442 // Break. |
494
|
443 |
578
|
444 void |
|
445 tree_break_command::eval (void) |
494
|
446 { |
|
447 if (! error_state) |
|
448 breaking = 1; |
|
449 } |
|
450 |
581
|
451 void |
|
452 tree_break_command::print_code (ostream& os) |
|
453 { |
|
454 print_code_indent (os); |
|
455 |
|
456 os << "break"; |
|
457 } |
|
458 |
578
|
459 // Continue. |
494
|
460 |
578
|
461 void |
|
462 tree_continue_command::eval (void) |
494
|
463 { |
|
464 if (! error_state) |
|
465 continuing = 1; |
|
466 } |
|
467 |
581
|
468 void |
|
469 tree_continue_command::print_code (ostream& os) |
|
470 { |
|
471 print_code_indent (os); |
|
472 |
|
473 os << "continue"; |
|
474 } |
|
475 |
578
|
476 // Return. |
494
|
477 |
578
|
478 void |
|
479 tree_return_command::eval (void) |
494
|
480 { |
|
481 if (! error_state) |
|
482 returning = 1; |
|
483 } |
|
484 |
581
|
485 void |
|
486 tree_return_command::print_code (ostream& os) |
|
487 { |
|
488 print_code_indent (os); |
|
489 |
|
490 os << "return"; |
|
491 } |
|
492 |
494
|
493 /* |
|
494 ;;; Local Variables: *** |
|
495 ;;; mode: C++ *** |
|
496 ;;; page-delimiter: "^/\\*" *** |
|
497 ;;; End: *** |
|
498 */ |