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 |
|
23 #if !defined (octave_tree_misc_h) |
|
24 #define octave_tree_misc_h 1 |
|
25 |
1297
|
26 #if defined (__GNUG__) |
|
27 #pragma interface |
|
28 #endif |
|
29 |
581
|
30 class ostream; |
1740
|
31 |
2086
|
32 class octave_value_list; |
|
33 class octave_value; |
577
|
34 class tree_command; |
|
35 class tree_expression; |
|
36 class tree_simple_assignment_expression; |
1740
|
37 class tree_index_expression; |
577
|
38 class tree_identifier; |
|
39 class symbol_record; |
|
40 class symbol_table; |
|
41 |
|
42 class tree_statement; |
|
43 class tree_statement_list; |
|
44 class tree_argument_list; |
|
45 class tree_parameter_list; |
|
46 class tree_return_list; |
723
|
47 class tree_va_return_list; |
2846
|
48 class tree_decl_elt; |
|
49 class tree_decl_init_list; |
2764
|
50 class tree_if_clause; |
|
51 class tree_if_command_list; |
|
52 class tree_switch_case; |
|
53 class tree_switch_case_list; |
577
|
54 |
2124
|
55 class tree_walker; |
|
56 |
577
|
57 #include <SLList.h> |
|
58 |
1740
|
59 #include "pt-base.h" |
577
|
60 |
2124
|
61 // A statement is either a command to execute or an expression to |
|
62 // evaluate. |
577
|
63 |
|
64 class |
2124
|
65 tree_statement |
577
|
66 { |
|
67 friend class tree_statement_list; |
|
68 |
|
69 public: |
2124
|
70 |
577
|
71 tree_statement (void) |
2124
|
72 : cmd (0), expr (0), print_flag (true) { } |
577
|
73 |
|
74 tree_statement (tree_command *c) |
2124
|
75 : cmd (c), expr (0), print_flag (true) { } |
577
|
76 |
|
77 tree_statement (tree_expression *e) |
2124
|
78 : cmd (0), expr (e), print_flag (true) { } |
577
|
79 |
|
80 ~tree_statement (void); |
|
81 |
1827
|
82 void set_print_flag (bool print) |
577
|
83 { print_flag = print; } |
|
84 |
1827
|
85 bool is_command (void) |
2124
|
86 { return cmd != 0; } |
1510
|
87 |
1827
|
88 bool is_expression (void) |
2124
|
89 { return expr != 0; } |
1510
|
90 |
1740
|
91 int line (void); |
|
92 int column (void); |
1510
|
93 |
1827
|
94 void maybe_echo_code (bool); |
1588
|
95 |
2124
|
96 bool print_result (void) { return print_flag; } |
|
97 |
|
98 tree_command *command (void) { return cmd; } |
|
99 |
|
100 tree_expression *expression (void) { return expr; } |
|
101 |
|
102 void accept (tree_walker& tw); |
581
|
103 |
577
|
104 private: |
2124
|
105 |
|
106 // Only one of cmd or expr can be valid at once. |
|
107 |
|
108 // Command to execute. |
|
109 tree_command *cmd; |
|
110 |
|
111 // Expression to evaluate. |
|
112 tree_expression *expr; |
|
113 |
|
114 // Print result of eval for this command? |
|
115 bool print_flag; |
577
|
116 }; |
|
117 |
2124
|
118 // A list of statements to evaluate. |
|
119 |
577
|
120 class |
2124
|
121 tree_statement_list : public SLList<tree_statement *> |
577
|
122 { |
|
123 public: |
2124
|
124 |
1227
|
125 tree_statement_list (void) |
2124
|
126 : SLList<tree_statement *> (), function_body (false) { } |
1227
|
127 |
|
128 tree_statement_list (tree_statement *s) |
2124
|
129 : SLList<tree_statement *> (), function_body (false) { append (s); } |
577
|
130 |
|
131 ~tree_statement_list (void) |
|
132 { |
|
133 while (! empty ()) |
|
134 { |
|
135 tree_statement *t = remove_front (); |
|
136 delete t; |
|
137 } |
|
138 } |
|
139 |
1827
|
140 void mark_as_function_body (void) { function_body = true; } |
1588
|
141 |
2086
|
142 octave_value eval (bool print); |
581
|
143 |
2086
|
144 octave_value_list eval (bool print, int nargout); |
672
|
145 |
2124
|
146 void accept (tree_walker& tw); |
1588
|
147 |
|
148 private: |
2124
|
149 |
|
150 // Does this list of statements make up the body of a function? |
1827
|
151 bool function_body; |
577
|
152 }; |
|
153 |
|
154 // Argument lists. Used to hold the list of expressions that are the |
|
155 // arguments in a function call or index expression. |
|
156 |
|
157 class |
2124
|
158 tree_argument_list : public SLList<tree_expression *> |
577
|
159 { |
|
160 public: |
2124
|
161 |
1227
|
162 tree_argument_list (void) |
2124
|
163 : SLList<tree_expression *> () { } |
1227
|
164 |
|
165 tree_argument_list (tree_expression *t) |
2124
|
166 : SLList<tree_expression *> () { append (t); } |
577
|
167 |
|
168 ~tree_argument_list (void) |
|
169 { |
|
170 while (! empty ()) |
|
171 { |
|
172 tree_expression *t = remove_front (); |
|
173 delete t; |
|
174 } |
|
175 } |
|
176 |
2086
|
177 octave_value_list convert_to_const_vector (void); |
581
|
178 |
2124
|
179 void accept (tree_walker& tw); |
577
|
180 }; |
|
181 |
|
182 // Parameter lists. Used to hold the list of input and output |
|
183 // parameters in a function definition. Elements are identifiers |
|
184 // only. |
|
185 |
|
186 class |
2124
|
187 tree_parameter_list : public SLList<tree_identifier *> |
577
|
188 { |
|
189 public: |
2124
|
190 |
1227
|
191 tree_parameter_list (void) |
2124
|
192 : SLList<tree_identifier *> (), marked_for_varargs (0) { } |
632
|
193 |
1227
|
194 tree_parameter_list (tree_identifier *t) |
2124
|
195 : SLList<tree_identifier *> (), marked_for_varargs (0) { append (t); } |
577
|
196 |
1740
|
197 ~tree_parameter_list (void); |
577
|
198 |
|
199 void mark_as_formal_parameters (void); |
|
200 |
|
201 void mark_varargs (void) |
|
202 { marked_for_varargs = 1; } |
|
203 |
1827
|
204 bool takes_varargs (void) const |
|
205 { return marked_for_varargs != 0; } |
577
|
206 |
|
207 void mark_varargs_only (void) |
|
208 { marked_for_varargs = -1; } |
|
209 |
1827
|
210 bool varargs_only (void) |
577
|
211 { return (marked_for_varargs < 0); } |
|
212 |
2086
|
213 void initialize_undefined_elements (octave_value& val); |
1093
|
214 |
2086
|
215 void define_from_arg_vector (const octave_value_list& args); |
577
|
216 |
1827
|
217 bool is_defined (void); |
577
|
218 |
2086
|
219 octave_value_list convert_to_const_vector (tree_va_return_list *vr_list); |
577
|
220 |
2124
|
221 void accept (tree_walker& tw); |
581
|
222 |
577
|
223 private: |
2124
|
224 |
577
|
225 int marked_for_varargs; |
|
226 }; |
|
227 |
|
228 // Return lists. Used to hold the right hand sides of multiple |
|
229 // assignment expressions. |
|
230 |
|
231 class |
2124
|
232 tree_return_list : public SLList<tree_index_expression *> |
577
|
233 { |
|
234 public: |
2124
|
235 |
1227
|
236 tree_return_list (void) |
2124
|
237 : SLList<tree_index_expression *> () { } |
1227
|
238 |
577
|
239 tree_return_list (tree_index_expression *t) |
2124
|
240 : SLList<tree_index_expression *> () { append (t); } |
577
|
241 |
1740
|
242 ~tree_return_list (void); |
581
|
243 |
2124
|
244 void accept (tree_walker& tw); |
577
|
245 }; |
|
246 |
723
|
247 class |
2086
|
248 tree_va_return_list : public SLList<octave_value> |
723
|
249 { |
|
250 public: |
2124
|
251 |
2086
|
252 tree_va_return_list (void) : SLList<octave_value> () { } |
1269
|
253 |
|
254 ~tree_va_return_list (void) { } |
723
|
255 }; |
|
256 |
2846
|
257 // List of expressions that make up a declaration statement. |
577
|
258 |
|
259 class |
2846
|
260 tree_decl_elt |
577
|
261 { |
|
262 public: |
|
263 |
2846
|
264 typedef void (*eval_fcn) (tree_decl_elt &, bool); |
|
265 |
|
266 tree_decl_elt (void) |
2124
|
267 : id (0), ass_expr (0) { } |
|
268 |
2846
|
269 tree_decl_elt (tree_identifier *i) |
2124
|
270 : id (i), ass_expr (0) { } |
577
|
271 |
2846
|
272 tree_decl_elt (tree_simple_assignment_expression *ass) |
2124
|
273 : id (0), ass_expr (ass) { } |
577
|
274 |
2846
|
275 ~tree_decl_elt (void); |
577
|
276 |
|
277 void eval (void); |
|
278 |
2124
|
279 tree_identifier *ident (void) { return id; } |
|
280 |
|
281 tree_simple_assignment_expression *assign_expr (void) { return ass_expr; } |
|
282 |
|
283 void accept (tree_walker& tw); |
581
|
284 |
577
|
285 private: |
2124
|
286 |
|
287 // Only one of id or ass_expr can be valid at once. |
|
288 |
2846
|
289 // An identifier to tag with the declared property. |
2124
|
290 tree_identifier *id; |
|
291 |
|
292 // An assignemnt expression. Valid only if the left hand side of |
|
293 // the assignment is a simple identifier. |
|
294 tree_simple_assignment_expression *ass_expr; |
577
|
295 }; |
|
296 |
|
297 class |
2846
|
298 tree_decl_init_list : public SLList<tree_decl_elt *> |
577
|
299 { |
|
300 public: |
2124
|
301 |
2846
|
302 tree_decl_init_list (void) |
|
303 : SLList<tree_decl_elt *> () { } |
1227
|
304 |
2846
|
305 tree_decl_init_list (tree_decl_elt *t) |
|
306 : SLList<tree_decl_elt *> () { append (t); } |
577
|
307 |
2846
|
308 ~tree_decl_init_list (void) |
577
|
309 { |
|
310 while (! empty ()) |
|
311 { |
2846
|
312 tree_decl_elt *t = remove_front (); |
577
|
313 delete t; |
|
314 } |
|
315 } |
|
316 |
2846
|
317 void eval (tree_decl_elt::eval_fcn, bool); |
581
|
318 |
2124
|
319 void accept (tree_walker& tw); |
577
|
320 }; |
|
321 |
|
322 class |
2124
|
323 tree_if_clause |
577
|
324 { |
|
325 public: |
2124
|
326 |
|
327 tree_if_clause (void) : expr (0), list (0) { } |
577
|
328 |
1740
|
329 tree_if_clause (tree_statement_list *l) |
2124
|
330 : expr (0), list (l) { } |
577
|
331 |
|
332 tree_if_clause (tree_expression *e, tree_statement_list *l) |
2124
|
333 : expr (e), list (l) { } |
577
|
334 |
1740
|
335 ~tree_if_clause (void); |
577
|
336 |
1827
|
337 bool is_else_clause (void) |
|
338 { return ! expr; } |
1063
|
339 |
577
|
340 int eval (void); |
|
341 |
2124
|
342 tree_expression *condition (void) { return expr; } |
|
343 |
|
344 tree_statement_list *commands (void) { return list; } |
|
345 |
|
346 void accept (tree_walker& tw); |
581
|
347 |
577
|
348 private: |
2124
|
349 |
|
350 // The condition to test. |
577
|
351 tree_expression *expr; |
2124
|
352 |
|
353 // The list of statements to evaluate if expr is true. |
577
|
354 tree_statement_list *list; |
|
355 }; |
|
356 |
|
357 class |
2124
|
358 tree_if_command_list : public SLList<tree_if_clause *> |
577
|
359 { |
|
360 public: |
2124
|
361 |
1227
|
362 tree_if_command_list (void) |
2124
|
363 : SLList<tree_if_clause *> () { } |
1227
|
364 |
|
365 tree_if_command_list (tree_if_clause *t) |
2124
|
366 : SLList<tree_if_clause *> () { append (t); } |
577
|
367 |
|
368 ~tree_if_command_list (void) |
|
369 { |
|
370 while (! empty ()) |
|
371 { |
|
372 tree_if_clause *t = remove_front (); |
|
373 delete t; |
|
374 } |
|
375 } |
|
376 |
|
377 void eval (void); |
581
|
378 |
2124
|
379 void accept (tree_walker& tw); |
577
|
380 }; |
|
381 |
2764
|
382 class |
|
383 tree_switch_case |
|
384 { |
|
385 public: |
|
386 |
|
387 tree_switch_case (void) : label (0), list (0) { } |
|
388 |
|
389 tree_switch_case (tree_statement_list *l) |
|
390 : label (0), list (l) { } |
|
391 |
|
392 tree_switch_case (tree_expression *e, tree_statement_list *l) |
|
393 : label (e), list (l) { } |
|
394 |
|
395 ~tree_switch_case (void); |
|
396 |
|
397 bool is_default_case (void) |
|
398 { return ! label; } |
|
399 |
|
400 bool label_matches (const octave_value& val); |
|
401 |
|
402 int eval (const octave_value& val); |
|
403 |
|
404 void eval_error (void); |
|
405 |
|
406 tree_expression *case_label (void) { return label; } |
|
407 |
|
408 tree_statement_list *commands (void) { return list; } |
|
409 |
|
410 void accept (tree_walker& tw); |
|
411 |
|
412 private: |
|
413 |
|
414 // The case label. |
|
415 tree_expression *label; |
|
416 |
|
417 // The list of statements to evaluate if the label matches. |
|
418 tree_statement_list *list; |
|
419 }; |
|
420 |
|
421 class |
|
422 tree_switch_case_list : public SLList<tree_switch_case *> |
|
423 { |
|
424 public: |
|
425 |
|
426 tree_switch_case_list (void) |
|
427 : SLList<tree_switch_case *> () { } |
|
428 |
|
429 tree_switch_case_list (tree_switch_case *t) |
|
430 : SLList<tree_switch_case *> () { append (t); } |
|
431 |
|
432 ~tree_switch_case_list (void) |
|
433 { |
|
434 while (! empty ()) |
|
435 { |
|
436 tree_switch_case *t = remove_front (); |
|
437 delete t; |
|
438 } |
|
439 } |
|
440 |
|
441 void eval (const octave_value& val); |
|
442 |
|
443 void accept (tree_walker& tw); |
|
444 }; |
|
445 |
577
|
446 #endif |
|
447 |
|
448 /* |
|
449 ;;; Local Variables: *** |
|
450 ;;; mode: C++ *** |
|
451 ;;; End: *** |
|
452 */ |