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