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 |
4066
|
23 #if defined (__GNUG__) && ! defined (NO_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 { |
|
66 for (Pix p = first (); p != 0; next (p)) |
|
67 { |
2989
|
68 f (*(this->operator () (p))); |
2982
|
69 |
|
70 if (error_state) |
|
71 break; |
|
72 } |
|
73 } |
|
74 |
|
75 void |
|
76 tree_decl_init_list::accept (tree_walker& tw) |
|
77 { |
|
78 tw.visit_decl_init_list (*this); |
|
79 } |
|
80 |
|
81 // Base class for declaration commands (global, static). |
|
82 |
|
83 tree_decl_command::~tree_decl_command (void) |
|
84 { |
|
85 delete init_list; |
|
86 } |
|
87 |
|
88 void |
|
89 tree_decl_command::accept (tree_walker& tw) |
|
90 { |
|
91 tw.visit_decl_command (*this); |
|
92 } |
|
93 |
|
94 // Global. |
|
95 |
2989
|
96 void |
|
97 tree_global_command::do_init (tree_decl_elt& elt) |
2982
|
98 { |
|
99 tree_identifier *id = elt.ident (); |
|
100 |
|
101 if (id) |
|
102 { |
|
103 id->link_to_global (); |
|
104 |
4009
|
105 if (! error_state) |
|
106 { |
|
107 octave_lvalue ult = id->lvalue (); |
2982
|
108 |
4009
|
109 if (ult.is_undefined ()) |
|
110 { |
|
111 tree_expression *expr = elt.expression (); |
2982
|
112 |
4009
|
113 octave_value init_val; |
3157
|
114 |
4009
|
115 if (expr) |
|
116 init_val = expr->rvalue (); |
|
117 else if (Vinitialize_global_variables) |
|
118 init_val |
|
119 = builtin_any_variable ("default_global_variable_value"); |
2982
|
120 |
4009
|
121 ult.assign (octave_value::op_asn_eq, init_val); |
|
122 } |
2982
|
123 } |
|
124 } |
|
125 } |
|
126 |
|
127 void |
|
128 tree_global_command::eval (void) |
|
129 { |
|
130 if (init_list) |
|
131 { |
2989
|
132 init_list->eval (do_init); |
2982
|
133 |
|
134 initialized = true; |
|
135 } |
|
136 |
3965
|
137 if (error_state) |
2982
|
138 ::error ("evaluating global command near line %d, column %d", |
|
139 line (), column ()); |
|
140 } |
|
141 |
|
142 // Static. |
|
143 |
2989
|
144 void |
|
145 tree_static_command::do_init (tree_decl_elt& elt) |
2982
|
146 { |
|
147 tree_identifier *id = elt.ident (); |
|
148 |
|
149 if (id) |
|
150 { |
|
151 id->mark_as_static (); |
|
152 |
|
153 tree_expression *expr = elt.expression (); |
|
154 |
|
155 if (expr) |
|
156 { |
|
157 octave_value init_val = expr->rvalue (); |
|
158 |
|
159 octave_lvalue ult = id->lvalue (); |
|
160 |
3538
|
161 ult.assign (octave_value::op_asn_eq, init_val); |
2982
|
162 } |
|
163 } |
|
164 } |
|
165 |
|
166 void |
|
167 tree_static_command::eval (void) |
|
168 { |
|
169 // Static variables only need to be marked and initialized once. |
|
170 |
|
171 if (init_list && ! initialized) |
|
172 { |
2989
|
173 init_list->eval (do_init); |
2982
|
174 |
|
175 initialized = true; |
|
176 |
3965
|
177 if (error_state) |
2982
|
178 ::error ("evaluating static command near line %d, column %d", |
|
179 line (), column ()); |
|
180 } |
|
181 } |
|
182 |
3156
|
183 static int |
|
184 initialize_global_variables (void) |
|
185 { |
|
186 Vinitialize_global_variables |
|
187 = check_preference ("initialize_global_variables"); |
|
188 |
|
189 return 0; |
|
190 } |
|
191 |
|
192 void |
|
193 symbols_of_pt_decl (void) |
|
194 { |
3258
|
195 DEFVAR (default_global_variable_value, , 0, |
3361
|
196 "-*- texinfo -*-\n\ |
3373
|
197 @defvr {Built-in Variable} default_global_variable_value\n\ |
3361
|
198 The default for value for otherwise uninitialized global variables.\n\ |
|
199 Only used if the variable initialize_global_variables is nonzero.\n\ |
|
200 If @code{initialize_global_variables} is nonzero, the value of\n\ |
|
201 @code{default_glbaol_variable_value} is used as the initial value of\n\ |
|
202 global variables that are not explicitly initialized. for example,\n\ |
|
203 \n\ |
|
204 @example\n\ |
|
205 @group\n\ |
|
206 initialize_global_variables = 1;\n\ |
|
207 default_global_variable_value = 13;\n\ |
|
208 global foo;\n\ |
|
209 foo\n\ |
|
210 @result{} 13\n\ |
|
211 @end group\n\ |
|
212 @end example\n\ |
|
213 \n\ |
|
214 the variable @code{default_global_variable_value} is initially undefined.\n\ |
|
215 @end defvr"); |
3156
|
216 |
3258
|
217 DEFVAR (initialize_global_variables, 0.0, initialize_global_variables, |
3448
|
218 "-*- texinfo -*-\n\ |
|
219 @defvr initialize_global_variables\n\ |
|
220 If the value of this variable is nonzero, global variables are given\n\ |
|
221 the default initial value specified by the built-in variable\n\ |
|
222 @code{default_global_variable_value}.\n\ |
|
223 @end defvr"); |
|
224 |
3156
|
225 } |
|
226 |
2982
|
227 /* |
|
228 ;;; Local Variables: *** |
|
229 ;;; mode: C++ *** |
|
230 ;;; End: *** |
|
231 */ |