494
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
494
|
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. |
494
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_tree_cmd_h) |
|
24 #define octave_tree_cmd_h 1 |
|
25 |
1297
|
26 #if defined (__GNUG__) |
|
27 #pragma interface |
|
28 #endif |
|
29 |
1740
|
30 class ostream; |
581
|
31 |
2086
|
32 class octave_value_list; |
1228
|
33 |
2973
|
34 class tree_argument_list; |
578
|
35 class tree_statement_list; |
2846
|
36 class tree_decl_init_list; |
578
|
37 class tree_if_command_list; |
2764
|
38 class tree_switch_case_list; |
494
|
39 class tree_expression; |
|
40 class tree_index_expression; |
1168
|
41 class tree_identifier; |
1228
|
42 class tree_return_list; |
2086
|
43 class octave_value; |
494
|
44 |
|
45 class tree_command; |
2846
|
46 class tree_decl_command; |
494
|
47 class tree_global_command; |
2846
|
48 class tree_static_command; |
494
|
49 class tree_while_command; |
2973
|
50 class tree_simple_for_command; |
|
51 class tree_complex_for_command; |
494
|
52 class tree_if_command; |
2764
|
53 class tree_switch_command; |
1489
|
54 class tree_try_catch_command; |
916
|
55 class tree_unwind_protect_command; |
2620
|
56 class tree_no_op_command; |
494
|
57 class tree_break_command; |
|
58 class tree_continue_command; |
|
59 class tree_return_command; |
|
60 |
2124
|
61 class tree_walker; |
|
62 |
2620
|
63 #include <string> |
|
64 |
1740
|
65 #include "pt-base.h" |
578
|
66 |
|
67 // A base class for commands. |
|
68 |
494
|
69 class |
|
70 tree_command : public tree |
|
71 { |
578
|
72 public: |
2124
|
73 |
578
|
74 tree_command (int l = -1, int c = -1) : tree (l, c) { } |
|
75 |
1270
|
76 virtual ~tree_command (void) { } |
|
77 |
578
|
78 virtual void eval (void) = 0; |
494
|
79 }; |
|
80 |
2846
|
81 // Base class for declaration commands -- global, static, etc. |
|
82 |
494
|
83 class |
2846
|
84 tree_decl_command : public tree_command |
|
85 { |
|
86 public: |
|
87 |
|
88 tree_decl_command (const string& n, int l = -1, int c = -1) |
|
89 : tree_command (l, c), cmd_name (n), initialized (false), init_list (0) { } |
|
90 |
|
91 tree_decl_command (const string& n, tree_decl_init_list *t, |
|
92 int l = -1, int c = -1) |
|
93 : tree_command (l, c), cmd_name (n), initialized (false), init_list (t) { } |
|
94 |
|
95 ~tree_decl_command (void); |
|
96 |
|
97 tree_decl_init_list *initializer_list (void) { return init_list; } |
|
98 |
|
99 void accept (tree_walker& tw); |
|
100 |
|
101 string name (void) { return cmd_name; } |
|
102 |
|
103 protected: |
|
104 |
|
105 // The name of this command -- global, static, etc. |
|
106 string cmd_name; |
|
107 |
|
108 // TRUE if this command has been evaluated. |
|
109 bool initialized; |
|
110 |
|
111 // The list of variables or initializers in this declaration command. |
|
112 tree_decl_init_list *init_list; |
|
113 }; |
|
114 |
|
115 // Global. |
|
116 |
|
117 class |
|
118 tree_global_command : public tree_decl_command |
494
|
119 { |
916
|
120 public: |
2124
|
121 |
1740
|
122 tree_global_command (int l = -1, int c = -1) |
2846
|
123 : tree_decl_command ("global", l, c) { } |
578
|
124 |
2846
|
125 tree_global_command (tree_decl_init_list *t, int l = -1, int c = -1) |
|
126 : tree_decl_command ("global", t, l, c) { } |
494
|
127 |
2846
|
128 ~tree_global_command (void) { } |
494
|
129 |
578
|
130 void eval (void); |
2846
|
131 }; |
494
|
132 |
2846
|
133 // Static. |
2124
|
134 |
2846
|
135 class |
|
136 tree_static_command : public tree_decl_command |
|
137 { |
|
138 public: |
581
|
139 |
2846
|
140 tree_static_command (int l = -1, int c = -1) |
|
141 : tree_decl_command ("static", l, c) { } |
2124
|
142 |
2846
|
143 tree_static_command (tree_decl_init_list *t, int l = -1, int c = -1) |
|
144 : tree_decl_command ("static", t, l, c) { } |
|
145 |
|
146 ~tree_static_command (void) { } |
|
147 |
|
148 void eval (void); |
494
|
149 }; |
|
150 |
578
|
151 // While. |
|
152 |
494
|
153 class |
|
154 tree_while_command : public tree_command |
|
155 { |
916
|
156 public: |
2124
|
157 |
1740
|
158 tree_while_command (int l = -1, int c = -1) |
|
159 : tree_command (l, c), expr (0), list (0) { } |
578
|
160 |
|
161 tree_while_command (tree_expression *e, int l = -1, int c = -1) |
1740
|
162 : tree_command (l, c), expr (e), list (0) { } |
578
|
163 |
|
164 tree_while_command (tree_expression *e, tree_statement_list *lst, |
|
165 int l = -1, int c = -1) |
1740
|
166 : tree_command (l, c), expr (e), list (lst) { } |
494
|
167 |
|
168 ~tree_while_command (void); |
|
169 |
578
|
170 void eval (void); |
494
|
171 |
|
172 void eval_error (void); |
|
173 |
2124
|
174 tree_expression *condition (void) { return expr; } |
|
175 |
|
176 tree_statement_list *body (void) { return list; } |
|
177 |
|
178 void accept (tree_walker& tw); |
581
|
179 |
916
|
180 private: |
2124
|
181 |
|
182 // Expression to test. |
|
183 tree_expression *expr; |
|
184 |
|
185 // List of commands to execute. |
|
186 tree_statement_list *list; |
494
|
187 }; |
|
188 |
578
|
189 // For. |
|
190 |
494
|
191 class |
2973
|
192 tree_simple_for_command : public tree_command |
494
|
193 { |
916
|
194 public: |
2124
|
195 |
2973
|
196 tree_simple_for_command (int l = -1, int c = -1) |
|
197 : tree_command (l, c), lhs (0), expr (0), list (0) { } |
578
|
198 |
2973
|
199 tree_simple_for_command (tree_expression *le, tree_expression *re, |
|
200 tree_statement_list *lst, int l = -1, int c = -1) |
|
201 : tree_command (l, c), lhs (le), expr (re), list (lst) { } |
1228
|
202 |
2973
|
203 ~tree_simple_for_command (void); |
494
|
204 |
578
|
205 void eval (void); |
494
|
206 |
|
207 void eval_error (void); |
|
208 |
2973
|
209 tree_expression *left_hand_side (void) { return lhs; } |
2124
|
210 |
|
211 tree_expression *control_expr (void) { return expr; } |
|
212 |
|
213 tree_statement_list *body (void) { return list; } |
|
214 |
|
215 void accept (tree_walker& tw); |
581
|
216 |
916
|
217 private: |
2124
|
218 |
2973
|
219 // Expression to modify. |
|
220 tree_expression *lhs; |
2124
|
221 |
|
222 // Expression to evaluate. |
|
223 tree_expression *expr; |
|
224 |
|
225 // List of commands to execute. |
|
226 tree_statement_list *list; |
|
227 |
2979
|
228 void do_for_loop_once (octave_lvalue &ult, const octave_value& rhs, |
|
229 bool& quit); |
2973
|
230 }; |
|
231 |
|
232 class |
|
233 tree_complex_for_command : public tree_command |
|
234 { |
|
235 public: |
|
236 |
|
237 tree_complex_for_command (int l = -1, int c = -1) |
|
238 : tree_command (l, c), lhs (0), expr (0), list (0) { } |
|
239 |
|
240 tree_complex_for_command (tree_argument_list *le, tree_expression *re, |
|
241 tree_statement_list *lst, int l = -1, int c = -1) |
|
242 : tree_command (l, c), lhs (le), expr (re), list (lst) { } |
|
243 |
|
244 ~tree_complex_for_command (void); |
|
245 |
|
246 void eval (void); |
|
247 |
|
248 void eval_error (void); |
1228
|
249 |
2973
|
250 tree_argument_list *left_hand_side (void) { return lhs; } |
|
251 |
|
252 tree_expression *control_expr (void) { return expr; } |
|
253 |
|
254 tree_statement_list *body (void) { return list; } |
|
255 |
|
256 void accept (tree_walker& tw); |
|
257 |
|
258 private: |
1228
|
259 |
2973
|
260 // Expression to modify. |
|
261 tree_argument_list *lhs; |
|
262 |
|
263 // Expression to evaluate. |
|
264 tree_expression *expr; |
|
265 |
|
266 // List of commands to execute. |
|
267 tree_statement_list *list; |
|
268 |
2979
|
269 void do_for_loop_once (octave_lvalue &val_ref, octave_lvalue &key_ref, |
|
270 const octave_value& val, const octave_value& key, |
2973
|
271 bool& quit); |
494
|
272 }; |
|
273 |
578
|
274 // If. |
|
275 |
494
|
276 class |
|
277 tree_if_command : public tree_command |
|
278 { |
916
|
279 public: |
2124
|
280 |
1740
|
281 tree_if_command (int l = -1, int c = -1) |
|
282 : tree_command (l, c), list (0) { } |
578
|
283 |
|
284 tree_if_command (tree_if_command_list *lst, int l = -1, int c = -1) |
1740
|
285 : tree_command (l, c), list (lst) { } |
494
|
286 |
|
287 ~tree_if_command (void); |
|
288 |
578
|
289 void eval (void); |
494
|
290 |
2124
|
291 tree_if_command_list *cmd_list (void) { return list; } |
|
292 |
|
293 void accept (tree_walker& tw); |
581
|
294 |
916
|
295 private: |
2124
|
296 |
|
297 // List of if commands (if, elseif, elseif, ... else, endif) |
578
|
298 tree_if_command_list *list; |
494
|
299 }; |
|
300 |
2764
|
301 // Switch. |
|
302 |
|
303 class |
|
304 tree_switch_command : public tree_command |
|
305 { |
|
306 public: |
|
307 |
|
308 tree_switch_command (int l = -1, int c = -1) |
|
309 : tree_command (l, c), expr (0), list (0) { } |
|
310 |
|
311 tree_switch_command (tree_expression *e, tree_switch_case_list *lst, |
|
312 int l = -1, int c = -1) |
|
313 : tree_command (l, c), expr (e), list (lst) { } |
|
314 |
|
315 ~tree_switch_command (void); |
|
316 |
|
317 void eval (void); |
|
318 |
|
319 void eval_error (void); |
|
320 |
|
321 tree_expression *switch_value (void) { return expr; } |
|
322 |
|
323 tree_switch_case_list *case_list (void) { return list; } |
|
324 |
|
325 void accept (tree_walker& tw); |
|
326 |
|
327 private: |
|
328 |
|
329 // Value on which to switch. |
|
330 tree_expression *expr; |
|
331 |
|
332 // List of cases (case 1, case 2, ..., default) |
|
333 tree_switch_case_list *list; |
|
334 }; |
|
335 |
916
|
336 // Simple exception handling. |
|
337 |
|
338 class |
|
339 tree_unwind_protect_command : public tree_command |
|
340 { |
|
341 public: |
2124
|
342 |
1740
|
343 tree_unwind_protect_command (int l = -1, int c = -1) |
|
344 : tree_command (l, c), unwind_protect_code (0), cleanup_code (0) { } |
916
|
345 |
|
346 tree_unwind_protect_command (tree_statement_list *tc, |
|
347 tree_statement_list *cc, |
|
348 int l = -1, int c = -1) |
1740
|
349 : tree_command (l, c), unwind_protect_code (tc), cleanup_code (cc) { } |
916
|
350 |
|
351 ~tree_unwind_protect_command (void); |
|
352 |
|
353 void eval (void); |
|
354 |
2124
|
355 tree_statement_list *body (void) { return unwind_protect_code; } |
|
356 |
|
357 tree_statement_list *cleanup (void) { return cleanup_code; } |
|
358 |
|
359 void accept (tree_walker& tw); |
916
|
360 |
|
361 private: |
2124
|
362 |
|
363 // The first body of code to attempt to execute. |
916
|
364 tree_statement_list *unwind_protect_code; |
2124
|
365 |
|
366 // The body of code to execute no matter what happens in the first |
|
367 // body of code. |
916
|
368 tree_statement_list *cleanup_code; |
|
369 }; |
|
370 |
1489
|
371 // Simple exception handling. |
|
372 |
|
373 class |
|
374 tree_try_catch_command : public tree_command |
|
375 { |
|
376 public: |
2124
|
377 |
1740
|
378 tree_try_catch_command (int l = -1, int c = -1) |
|
379 : tree_command (l, c), try_code (0), catch_code (0) { } |
1489
|
380 |
|
381 tree_try_catch_command (tree_statement_list *tc, |
|
382 tree_statement_list *cc, |
|
383 int l = -1, int c = -1) |
1740
|
384 : tree_command (l, c), try_code (tc), catch_code (cc) { } |
1489
|
385 |
|
386 ~tree_try_catch_command (void); |
|
387 |
|
388 void eval (void); |
|
389 |
2124
|
390 tree_statement_list *body (void) { return try_code; } |
|
391 |
|
392 tree_statement_list *cleanup (void) { return catch_code; } |
|
393 |
|
394 void accept (tree_walker& tw); |
1489
|
395 |
|
396 private: |
2124
|
397 |
|
398 // The first block of code to attempt to execute. |
1489
|
399 tree_statement_list *try_code; |
2124
|
400 |
|
401 // The code to execute if an error occurs in the first block. |
1489
|
402 tree_statement_list *catch_code; |
|
403 }; |
|
404 |
2620
|
405 // No-op. |
|
406 |
|
407 class |
|
408 tree_no_op_command : public tree_command |
|
409 { |
|
410 public: |
|
411 |
|
412 tree_no_op_command (const string& cmd = "no_op", int l = -1, int c = -1) |
|
413 : tree_command (l, c), orig_cmd (cmd) { } |
|
414 |
|
415 ~tree_no_op_command (void) { } |
|
416 |
|
417 void eval (void) { } |
|
418 |
|
419 void accept (tree_walker& tw); |
|
420 |
|
421 string original_command (void) { return orig_cmd; } |
|
422 |
|
423 private: |
|
424 |
|
425 string orig_cmd; |
|
426 }; |
|
427 |
578
|
428 // Break. |
|
429 |
494
|
430 class |
|
431 tree_break_command : public tree_command |
|
432 { |
916
|
433 public: |
2124
|
434 |
578
|
435 tree_break_command (int l = -1, int c = -1) : tree_command (l, c) { } |
494
|
436 |
578
|
437 ~tree_break_command (void) { } |
494
|
438 |
578
|
439 void eval (void); |
581
|
440 |
2124
|
441 void accept (tree_walker& tw); |
494
|
442 }; |
|
443 |
578
|
444 // Continue. |
|
445 |
494
|
446 class |
|
447 tree_continue_command : public tree_command |
|
448 { |
916
|
449 public: |
2124
|
450 |
578
|
451 tree_continue_command (int l = -1, int c = -1) : tree_command (l, c) { } |
494
|
452 |
578
|
453 ~tree_continue_command (void) { } |
494
|
454 |
578
|
455 void eval (void); |
581
|
456 |
2124
|
457 void accept (tree_walker& tw); |
494
|
458 }; |
|
459 |
578
|
460 // Return. |
|
461 |
494
|
462 class |
|
463 tree_return_command : public tree_command |
|
464 { |
578
|
465 public: |
2124
|
466 |
578
|
467 tree_return_command (int l = -1, int c = -1) : tree_command (l, c) { } |
494
|
468 |
578
|
469 ~tree_return_command (void) { } |
494
|
470 |
578
|
471 void eval (void); |
581
|
472 |
2124
|
473 void accept (tree_walker& tw); |
494
|
474 }; |
|
475 |
|
476 #endif |
|
477 |
|
478 /* |
|
479 ;;; Local Variables: *** |
|
480 ;;; mode: C++ *** |
|
481 ;;; End: *** |
|
482 */ |