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