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