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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
2982
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
2982
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
3156
|
27 #include "defun.h" |
2982
|
28 #include "error.h" |
|
29 #include "pt-cmd.h" |
|
30 #include "ov.h" |
|
31 #include "oct-lvalue.h" |
|
32 #include "pt-decl.h" |
|
33 #include "pt-exp.h" |
|
34 #include "pt-id.h" |
|
35 #include "pt-walk.h" |
3156
|
36 #include "utils.h" |
|
37 #include "variables.h" |
|
38 |
2982
|
39 // Declarations (global, static, etc.). |
|
40 |
|
41 tree_decl_elt::~tree_decl_elt (void) |
|
42 { |
|
43 delete id; |
|
44 delete expr; |
|
45 } |
|
46 |
6215
|
47 bool |
|
48 tree_decl_elt::eval (void) |
|
49 { |
|
50 bool retval = false; |
|
51 |
|
52 if (id && expr) |
|
53 { |
|
54 octave_lvalue ult = id->lvalue (); |
|
55 |
|
56 octave_value init_val = expr->rvalue (); |
|
57 |
|
58 if (! error_state) |
|
59 { |
|
60 ult.assign (octave_value::op_asn_eq, init_val); |
|
61 |
|
62 retval = true; |
|
63 } |
|
64 } |
|
65 |
|
66 return retval; |
|
67 } |
|
68 |
5861
|
69 tree_decl_elt * |
|
70 tree_decl_elt::dup (symbol_table *sym_tab) |
|
71 { |
|
72 return new tree_decl_elt (id ? id->dup (sym_tab) : 0, |
|
73 expr ? expr->dup (sym_tab) : 0); |
|
74 } |
|
75 |
2982
|
76 void |
|
77 tree_decl_elt::accept (tree_walker& tw) |
|
78 { |
|
79 tw.visit_decl_elt (*this); |
|
80 } |
|
81 |
|
82 // Initializer lists for declaration statements. |
|
83 |
|
84 void |
2989
|
85 tree_decl_init_list::eval (tree_decl_elt::eval_fcn f) |
2982
|
86 { |
4219
|
87 for (iterator p = begin (); p != end (); p++) |
2982
|
88 { |
4219
|
89 tree_decl_elt *elt = *p; |
|
90 |
|
91 f (*elt); |
2982
|
92 |
|
93 if (error_state) |
|
94 break; |
|
95 } |
|
96 } |
|
97 |
5861
|
98 tree_decl_init_list * |
|
99 tree_decl_init_list::dup (symbol_table *sym_tab) |
|
100 { |
|
101 tree_decl_init_list *new_dil = new tree_decl_init_list (); |
|
102 |
|
103 for (iterator p = begin (); p != end (); p++) |
|
104 { |
|
105 tree_decl_elt *elt = *p; |
|
106 |
|
107 new_dil->append (elt ? elt->dup (sym_tab) : 0); |
|
108 } |
|
109 |
|
110 return new_dil; |
|
111 } |
|
112 |
2982
|
113 void |
|
114 tree_decl_init_list::accept (tree_walker& tw) |
|
115 { |
|
116 tw.visit_decl_init_list (*this); |
|
117 } |
|
118 |
|
119 // Base class for declaration commands (global, static). |
|
120 |
|
121 tree_decl_command::~tree_decl_command (void) |
|
122 { |
|
123 delete init_list; |
|
124 } |
|
125 |
|
126 void |
|
127 tree_decl_command::accept (tree_walker& tw) |
|
128 { |
|
129 tw.visit_decl_command (*this); |
|
130 } |
|
131 |
|
132 // Global. |
|
133 |
2989
|
134 void |
|
135 tree_global_command::do_init (tree_decl_elt& elt) |
2982
|
136 { |
|
137 tree_identifier *id = elt.ident (); |
|
138 |
|
139 if (id) |
|
140 { |
|
141 id->link_to_global (); |
|
142 |
4009
|
143 if (! error_state) |
|
144 { |
|
145 octave_lvalue ult = id->lvalue (); |
2982
|
146 |
4009
|
147 if (ult.is_undefined ()) |
|
148 { |
|
149 tree_expression *expr = elt.expression (); |
2982
|
150 |
4009
|
151 octave_value init_val; |
3157
|
152 |
4009
|
153 if (expr) |
|
154 init_val = expr->rvalue (); |
4464
|
155 else |
|
156 init_val = Matrix (); |
2982
|
157 |
4009
|
158 ult.assign (octave_value::op_asn_eq, init_val); |
|
159 } |
2982
|
160 } |
|
161 } |
|
162 } |
|
163 |
|
164 void |
|
165 tree_global_command::eval (void) |
|
166 { |
|
167 if (init_list) |
|
168 { |
2989
|
169 init_list->eval (do_init); |
2982
|
170 |
|
171 initialized = true; |
|
172 } |
|
173 |
3965
|
174 if (error_state) |
2982
|
175 ::error ("evaluating global command near line %d, column %d", |
|
176 line (), column ()); |
|
177 } |
|
178 |
5861
|
179 tree_command * |
|
180 tree_global_command::dup (symbol_table *sym_tab) |
|
181 { |
|
182 return new tree_global_command (init_list ? init_list->dup (sym_tab) : 0, |
|
183 line (), column ()); |
|
184 } |
|
185 |
2982
|
186 // Static. |
|
187 |
2989
|
188 void |
|
189 tree_static_command::do_init (tree_decl_elt& elt) |
2982
|
190 { |
|
191 tree_identifier *id = elt.ident (); |
|
192 |
|
193 if (id) |
|
194 { |
|
195 id->mark_as_static (); |
|
196 |
4844
|
197 octave_lvalue ult = id->lvalue (); |
2982
|
198 |
4846
|
199 if (ult.is_undefined ()) |
2982
|
200 { |
4844
|
201 tree_expression *expr = elt.expression (); |
|
202 |
|
203 octave_value init_val; |
2982
|
204 |
4844
|
205 if (expr) |
|
206 init_val = expr->rvalue (); |
|
207 else |
|
208 init_val = Matrix (); |
2982
|
209 |
3538
|
210 ult.assign (octave_value::op_asn_eq, init_val); |
2982
|
211 } |
|
212 } |
|
213 } |
|
214 |
|
215 void |
|
216 tree_static_command::eval (void) |
|
217 { |
|
218 // Static variables only need to be marked and initialized once. |
|
219 |
|
220 if (init_list && ! initialized) |
|
221 { |
2989
|
222 init_list->eval (do_init); |
2982
|
223 |
|
224 initialized = true; |
|
225 |
3965
|
226 if (error_state) |
2982
|
227 ::error ("evaluating static command near line %d, column %d", |
|
228 line (), column ()); |
|
229 } |
|
230 } |
|
231 |
5861
|
232 tree_command * |
|
233 tree_static_command::dup (symbol_table *sym_tab) |
|
234 { |
|
235 return new tree_static_command (init_list ? init_list->dup (sym_tab) : 0, |
|
236 line (), column ()); |
|
237 } |
|
238 |
2982
|
239 /* |
|
240 ;;; Local Variables: *** |
|
241 ;;; mode: C++ *** |
|
242 ;;; End: *** |
|
243 */ |