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 |
|
23 #if defined (__GNUG__) |
|
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 |
3107
|
105 octave_lvalue ult = id->lvalue (); |
2982
|
106 |
3157
|
107 if (ult.is_undefined ()) |
2982
|
108 { |
3107
|
109 tree_expression *expr = elt.expression (); |
2982
|
110 |
3157
|
111 octave_value init_val; |
|
112 |
|
113 if (expr) |
|
114 init_val = expr->rvalue (); |
|
115 else if (Vinitialize_global_variables) |
|
116 init_val = builtin_any_variable ("default_global_variable_value"); |
2982
|
117 |
|
118 ult.assign (octave_value::asn_eq, init_val); |
|
119 } |
|
120 } |
|
121 } |
|
122 |
|
123 void |
|
124 tree_global_command::eval (void) |
|
125 { |
|
126 if (init_list) |
|
127 { |
2989
|
128 init_list->eval (do_init); |
2982
|
129 |
|
130 initialized = true; |
|
131 } |
|
132 |
|
133 if (error_state > 0) |
|
134 ::error ("evaluating global command near line %d, column %d", |
|
135 line (), column ()); |
|
136 } |
|
137 |
|
138 // Static. |
|
139 |
2989
|
140 void |
|
141 tree_static_command::do_init (tree_decl_elt& elt) |
2982
|
142 { |
|
143 tree_identifier *id = elt.ident (); |
|
144 |
|
145 if (id) |
|
146 { |
|
147 id->mark_as_static (); |
|
148 |
|
149 tree_expression *expr = elt.expression (); |
|
150 |
|
151 if (expr) |
|
152 { |
|
153 octave_value init_val = expr->rvalue (); |
|
154 |
|
155 octave_lvalue ult = id->lvalue (); |
|
156 |
|
157 ult.assign (octave_value::asn_eq, init_val); |
|
158 } |
|
159 } |
|
160 } |
|
161 |
|
162 void |
|
163 tree_static_command::eval (void) |
|
164 { |
|
165 // Static variables only need to be marked and initialized once. |
|
166 |
|
167 if (init_list && ! initialized) |
|
168 { |
2989
|
169 init_list->eval (do_init); |
2982
|
170 |
|
171 initialized = true; |
|
172 |
|
173 if (error_state > 0) |
|
174 ::error ("evaluating static command near line %d, column %d", |
|
175 line (), column ()); |
|
176 } |
|
177 } |
|
178 |
3156
|
179 static int |
|
180 initialize_global_variables (void) |
|
181 { |
|
182 Vinitialize_global_variables |
|
183 = check_preference ("initialize_global_variables"); |
|
184 |
|
185 return 0; |
|
186 } |
|
187 |
|
188 void |
|
189 symbols_of_pt_decl (void) |
|
190 { |
3258
|
191 DEFVAR (default_global_variable_value, , 0, |
3361
|
192 "-*- texinfo -*-\n\ |
3373
|
193 @defvr {Built-in Variable} default_global_variable_value\n\ |
3361
|
194 The default for value for otherwise uninitialized global variables.\n\ |
|
195 Only used if the variable initialize_global_variables is nonzero.\n\ |
|
196 If @code{initialize_global_variables} is nonzero, the value of\n\ |
|
197 @code{default_glbaol_variable_value} is used as the initial value of\n\ |
|
198 global variables that are not explicitly initialized. for example,\n\ |
|
199 \n\ |
|
200 @example\n\ |
|
201 @group\n\ |
|
202 initialize_global_variables = 1;\n\ |
|
203 default_global_variable_value = 13;\n\ |
|
204 global foo;\n\ |
|
205 foo\n\ |
|
206 @result{} 13\n\ |
|
207 @end group\n\ |
|
208 @end example\n\ |
|
209 \n\ |
|
210 the variable @code{default_global_variable_value} is initially undefined.\n\ |
|
211 @end defvr"); |
3156
|
212 |
3258
|
213 DEFVAR (initialize_global_variables, 0.0, initialize_global_variables, |
3448
|
214 "-*- texinfo -*-\n\ |
|
215 @defvr initialize_global_variables\n\ |
|
216 If the value of this variable is nonzero, global variables are given\n\ |
|
217 the default initial value specified by the built-in variable\n\ |
|
218 @code{default_global_variable_value}.\n\ |
|
219 @end defvr"); |
|
220 |
3156
|
221 } |
|
222 |
2982
|
223 /* |
|
224 ;;; Local Variables: *** |
|
225 ;;; mode: C++ *** |
|
226 ;;; End: *** |
|
227 */ |