2982
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 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 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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2982
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
3156
|
31 #include "defun.h" |
2982
|
32 #include "error.h" |
|
33 #include "pt-cmd.h" |
|
34 #include "ov.h" |
|
35 #include "oct-lvalue.h" |
|
36 #include "pt-decl.h" |
|
37 #include "pt-exp.h" |
|
38 #include "pt-id.h" |
|
39 #include "pt-walk.h" |
3156
|
40 #include "utils.h" |
|
41 #include "variables.h" |
|
42 |
|
43 // Control whether otherwise uninitialized global variables are |
|
44 // given a default value. |
|
45 static int Vinitialize_global_variables; |
2982
|
46 |
|
47 // Declarations (global, static, etc.). |
|
48 |
|
49 tree_decl_elt::~tree_decl_elt (void) |
|
50 { |
|
51 delete id; |
|
52 delete expr; |
|
53 } |
|
54 |
|
55 void |
|
56 tree_decl_elt::accept (tree_walker& tw) |
|
57 { |
|
58 tw.visit_decl_elt (*this); |
|
59 } |
|
60 |
|
61 // Initializer lists for declaration statements. |
|
62 |
|
63 void |
2989
|
64 tree_decl_init_list::eval (tree_decl_elt::eval_fcn f) |
2982
|
65 { |
4219
|
66 for (iterator p = begin (); p != end (); p++) |
2982
|
67 { |
4219
|
68 tree_decl_elt *elt = *p; |
|
69 |
|
70 f (*elt); |
2982
|
71 |
|
72 if (error_state) |
|
73 break; |
|
74 } |
|
75 } |
|
76 |
|
77 void |
|
78 tree_decl_init_list::accept (tree_walker& tw) |
|
79 { |
|
80 tw.visit_decl_init_list (*this); |
|
81 } |
|
82 |
|
83 // Base class for declaration commands (global, static). |
|
84 |
|
85 tree_decl_command::~tree_decl_command (void) |
|
86 { |
|
87 delete init_list; |
|
88 } |
|
89 |
|
90 void |
|
91 tree_decl_command::accept (tree_walker& tw) |
|
92 { |
|
93 tw.visit_decl_command (*this); |
|
94 } |
|
95 |
|
96 // Global. |
|
97 |
2989
|
98 void |
|
99 tree_global_command::do_init (tree_decl_elt& elt) |
2982
|
100 { |
|
101 tree_identifier *id = elt.ident (); |
|
102 |
|
103 if (id) |
|
104 { |
|
105 id->link_to_global (); |
|
106 |
4009
|
107 if (! error_state) |
|
108 { |
|
109 octave_lvalue ult = id->lvalue (); |
2982
|
110 |
4009
|
111 if (ult.is_undefined ()) |
|
112 { |
|
113 tree_expression *expr = elt.expression (); |
2982
|
114 |
4009
|
115 octave_value init_val; |
3157
|
116 |
4009
|
117 if (expr) |
|
118 init_val = expr->rvalue (); |
|
119 else if (Vinitialize_global_variables) |
|
120 init_val |
|
121 = builtin_any_variable ("default_global_variable_value"); |
2982
|
122 |
4009
|
123 ult.assign (octave_value::op_asn_eq, init_val); |
|
124 } |
2982
|
125 } |
|
126 } |
|
127 } |
|
128 |
|
129 void |
|
130 tree_global_command::eval (void) |
|
131 { |
|
132 if (init_list) |
|
133 { |
2989
|
134 init_list->eval (do_init); |
2982
|
135 |
|
136 initialized = true; |
|
137 } |
|
138 |
3965
|
139 if (error_state) |
2982
|
140 ::error ("evaluating global command near line %d, column %d", |
|
141 line (), column ()); |
|
142 } |
|
143 |
|
144 // Static. |
|
145 |
2989
|
146 void |
|
147 tree_static_command::do_init (tree_decl_elt& elt) |
2982
|
148 { |
|
149 tree_identifier *id = elt.ident (); |
|
150 |
|
151 if (id) |
|
152 { |
|
153 id->mark_as_static (); |
|
154 |
|
155 tree_expression *expr = elt.expression (); |
|
156 |
|
157 if (expr) |
|
158 { |
|
159 octave_value init_val = expr->rvalue (); |
|
160 |
|
161 octave_lvalue ult = id->lvalue (); |
|
162 |
3538
|
163 ult.assign (octave_value::op_asn_eq, init_val); |
2982
|
164 } |
|
165 } |
|
166 } |
|
167 |
|
168 void |
|
169 tree_static_command::eval (void) |
|
170 { |
|
171 // Static variables only need to be marked and initialized once. |
|
172 |
|
173 if (init_list && ! initialized) |
|
174 { |
2989
|
175 init_list->eval (do_init); |
2982
|
176 |
|
177 initialized = true; |
|
178 |
3965
|
179 if (error_state) |
2982
|
180 ::error ("evaluating static command near line %d, column %d", |
|
181 line (), column ()); |
|
182 } |
|
183 } |
|
184 |
3156
|
185 static int |
|
186 initialize_global_variables (void) |
|
187 { |
|
188 Vinitialize_global_variables |
|
189 = check_preference ("initialize_global_variables"); |
|
190 |
|
191 return 0; |
|
192 } |
|
193 |
|
194 void |
|
195 symbols_of_pt_decl (void) |
|
196 { |
3258
|
197 DEFVAR (default_global_variable_value, , 0, |
3361
|
198 "-*- texinfo -*-\n\ |
3373
|
199 @defvr {Built-in Variable} default_global_variable_value\n\ |
3361
|
200 The default for value for otherwise uninitialized global variables.\n\ |
|
201 Only used if the variable initialize_global_variables is nonzero.\n\ |
|
202 If @code{initialize_global_variables} is nonzero, the value of\n\ |
|
203 @code{default_glbaol_variable_value} is used as the initial value of\n\ |
|
204 global variables that are not explicitly initialized. for example,\n\ |
|
205 \n\ |
|
206 @example\n\ |
|
207 @group\n\ |
|
208 initialize_global_variables = 1;\n\ |
|
209 default_global_variable_value = 13;\n\ |
|
210 global foo;\n\ |
|
211 foo\n\ |
|
212 @result{} 13\n\ |
|
213 @end group\n\ |
|
214 @end example\n\ |
|
215 \n\ |
|
216 the variable @code{default_global_variable_value} is initially undefined.\n\ |
|
217 @end defvr"); |
3156
|
218 |
4233
|
219 DEFVAR (initialize_global_variables, false, initialize_global_variables, |
3448
|
220 "-*- texinfo -*-\n\ |
|
221 @defvr initialize_global_variables\n\ |
|
222 If the value of this variable is nonzero, global variables are given\n\ |
|
223 the default initial value specified by the built-in variable\n\ |
|
224 @code{default_global_variable_value}.\n\ |
|
225 @end defvr"); |
|
226 |
3156
|
227 } |
|
228 |
2982
|
229 /* |
|
230 ;;; Local Variables: *** |
|
231 ;;; mode: C++ *** |
|
232 ;;; End: *** |
|
233 */ |