GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
pt-decl.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 John W. Eaton
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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_pt_decl_h)
24 #define octave_pt_decl_h 1
25 
26 class tree_expression;
27 class tree_identifier;
28 
29 class tree_walker;
30 
31 #include <string>
32 
33 #include "base-list.h"
34 #include "oct-lvalue.h"
35 #include "pt-cmd.h"
36 #include "pt-id.h"
37 #include "symtab.h"
38 
39 // List of expressions that make up a declaration statement.
40 
41 class
43 {
44 public:
45 
47  : id (i), expr (e) { }
48 
49  ~tree_decl_elt (void);
50 
51  bool eval (void);
52 
53  bool is_defined (void) { return id ? id->is_defined () : false; }
54 
55  bool is_variable (void) { return id ? id->is_variable () : false; }
56 
57  void mark_as_formal_parameter (void)
58  {
59  if (id)
60  id->mark_as_formal_parameter ();
61  }
62 
63  bool lvalue_ok (void) { return id ? id->lvalue_ok () : false; }
64 
65  // Do not allow functions to return null values.
66  octave_value rvalue1 (int nargout = 1)
67  {
68  return id ? id->rvalue1 (nargout).storable_value () : octave_value ();
69  }
70 
71  octave_value_list rvalue (int nargout)
72  {
73  octave_value_list retval;
74 
75  if (nargout > 1)
76  error ("invalid number of output arguments in declaration list");
77  else
78  retval = rvalue1 (nargout);
79 
80  return retval;
81  }
82 
83  octave_lvalue lvalue (void) { return id ? id->lvalue () : octave_lvalue (); }
84 
85  tree_identifier *ident (void) { return id; }
86 
87  std::string name (void) { return id ? id->name () : ""; }
88 
89  tree_expression *expression (void) { return expr; }
90 
93 
94  void accept (tree_walker& tw);
95 
96 private:
97 
98  // An identifier to tag with the declared property.
100 
101  // An initializer expression (may be zero);
103 
104  // No copying!
105 
106  tree_decl_elt (const tree_decl_elt&);
107 
109 };
110 
111 class
113 {
114 public:
115 
117 
118  tree_decl_init_list (tree_decl_elt *t) { append (t); }
119 
121  {
122  while (! empty ())
123  {
124  iterator p = begin ();
125  delete *p;
126  erase (p);
127  }
128  }
129 
132 
133  void accept (tree_walker& tw);
134 
135 private:
136 
137  // No copying!
138 
140 
142 };
143 
144 // Base class for declaration commands -- global, static, etc.
145 
146 class
148 {
149 public:
150 
151  tree_decl_command (const std::string& n, int l = -1, int c = -1)
152  : tree_command (l, c), cmd_name (n), init_list (0) { }
153 
154  tree_decl_command (const std::string& n, tree_decl_init_list *t,
155  int l = -1, int c = -1)
156  : tree_command (l, c), cmd_name (n), init_list (t) { }
157 
158  ~tree_decl_command (void);
159 
160  tree_decl_init_list *initializer_list (void) { return init_list; }
161 
162  std::string name (void) { return cmd_name; }
163 
164 protected:
165 
166  // The name of this command -- global, static, etc.
167  std::string cmd_name;
168 
169  // The list of variables or initializers in this declaration command.
171 
172 private:
173 
174  // No copying!
175 
177 
179 };
180 
181 // Global.
182 
183 class
185 {
186 public:
187 
188  tree_global_command (int l = -1, int c = -1)
189  : tree_decl_command ("global", l, c) { }
190 
191  tree_global_command (tree_decl_init_list *t, int l = -1, int c = -1)
192  : tree_decl_command ("global", t, l, c) { }
193 
195 
198 
199  void accept (tree_walker& tw);
200 
201 private:
202 
203  static void do_init (tree_decl_elt& elt);
204 
205  // No copying!
206 
208 
210 };
211 
212 // Persistent.
213 
214 class
216 {
217 public:
218 
219  tree_persistent_command (int l = -1, int c = -1)
220  : tree_decl_command ("persistent", l, c) { }
221 
222  tree_persistent_command (tree_decl_init_list *t, int l = -1, int c = -1)
223  : tree_decl_command ("persistent", t, l, c) { }
224 
226 
229 
230  void accept (tree_walker& tw);
231 
232 private:
233 
234  static void do_init (tree_decl_elt& elt);
235 
236  // No copying!
237 
239 
241 };
242 
243 #endif