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" |
7205
|
33 #include "pt-bp.h" |
2982
|
34 #include "pt-decl.h" |
|
35 #include "pt-exp.h" |
|
36 #include "pt-id.h" |
|
37 #include "pt-walk.h" |
3156
|
38 #include "utils.h" |
|
39 #include "variables.h" |
|
40 |
2982
|
41 // Declarations (global, static, etc.). |
|
42 |
|
43 tree_decl_elt::~tree_decl_elt (void) |
|
44 { |
|
45 delete id; |
|
46 delete expr; |
|
47 } |
|
48 |
6215
|
49 bool |
|
50 tree_decl_elt::eval (void) |
|
51 { |
|
52 bool retval = false; |
|
53 |
|
54 if (id && expr) |
|
55 { |
|
56 octave_lvalue ult = id->lvalue (); |
|
57 |
|
58 octave_value init_val = expr->rvalue (); |
|
59 |
|
60 if (! error_state) |
|
61 { |
|
62 ult.assign (octave_value::op_asn_eq, init_val); |
|
63 |
|
64 retval = true; |
|
65 } |
|
66 } |
|
67 |
|
68 return retval; |
|
69 } |
|
70 |
5861
|
71 tree_decl_elt * |
7336
|
72 tree_decl_elt::dup (symbol_table::scope_id scope) |
5861
|
73 { |
7336
|
74 return new tree_decl_elt (id ? id->dup (scope) : 0, |
|
75 expr ? expr->dup (scope) : 0); |
5861
|
76 } |
|
77 |
2982
|
78 void |
|
79 tree_decl_elt::accept (tree_walker& tw) |
|
80 { |
|
81 tw.visit_decl_elt (*this); |
|
82 } |
|
83 |
|
84 // Initializer lists for declaration statements. |
|
85 |
|
86 void |
2989
|
87 tree_decl_init_list::eval (tree_decl_elt::eval_fcn f) |
2982
|
88 { |
4219
|
89 for (iterator p = begin (); p != end (); p++) |
2982
|
90 { |
4219
|
91 tree_decl_elt *elt = *p; |
|
92 |
|
93 f (*elt); |
2982
|
94 |
|
95 if (error_state) |
|
96 break; |
|
97 } |
|
98 } |
|
99 |
5861
|
100 tree_decl_init_list * |
7336
|
101 tree_decl_init_list::dup (symbol_table::scope_id scope) |
5861
|
102 { |
|
103 tree_decl_init_list *new_dil = new tree_decl_init_list (); |
|
104 |
|
105 for (iterator p = begin (); p != end (); p++) |
|
106 { |
|
107 tree_decl_elt *elt = *p; |
|
108 |
7336
|
109 new_dil->append (elt ? elt->dup (scope) : 0); |
5861
|
110 } |
|
111 |
|
112 return new_dil; |
|
113 } |
|
114 |
2982
|
115 void |
|
116 tree_decl_init_list::accept (tree_walker& tw) |
|
117 { |
|
118 tw.visit_decl_init_list (*this); |
|
119 } |
|
120 |
|
121 // Base class for declaration commands (global, static). |
|
122 |
|
123 tree_decl_command::~tree_decl_command (void) |
|
124 { |
|
125 delete init_list; |
|
126 } |
|
127 |
|
128 void |
|
129 tree_decl_command::accept (tree_walker& tw) |
|
130 { |
|
131 tw.visit_decl_command (*this); |
|
132 } |
|
133 |
|
134 // Global. |
|
135 |
2989
|
136 void |
|
137 tree_global_command::do_init (tree_decl_elt& elt) |
2982
|
138 { |
|
139 tree_identifier *id = elt.ident (); |
|
140 |
|
141 if (id) |
|
142 { |
7336
|
143 id->mark_global (); |
2982
|
144 |
4009
|
145 if (! error_state) |
|
146 { |
|
147 octave_lvalue ult = id->lvalue (); |
2982
|
148 |
4009
|
149 if (ult.is_undefined ()) |
|
150 { |
|
151 tree_expression *expr = elt.expression (); |
2982
|
152 |
4009
|
153 octave_value init_val; |
3157
|
154 |
4009
|
155 if (expr) |
|
156 init_val = expr->rvalue (); |
4464
|
157 else |
|
158 init_val = Matrix (); |
2982
|
159 |
4009
|
160 ult.assign (octave_value::op_asn_eq, init_val); |
|
161 } |
2982
|
162 } |
|
163 } |
|
164 } |
|
165 |
|
166 void |
|
167 tree_global_command::eval (void) |
|
168 { |
7205
|
169 MAYBE_DO_BREAKPOINT; |
|
170 |
2982
|
171 if (init_list) |
7336
|
172 init_list->eval (do_init); |
2982
|
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 * |
7336
|
180 tree_global_command::dup (symbol_table::scope_id scope) |
5861
|
181 { |
7336
|
182 return new tree_global_command (init_list ? init_list->dup (scope) : 0, |
5861
|
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 { |
7205
|
218 MAYBE_DO_BREAKPOINT; |
|
219 |
2982
|
220 // Static variables only need to be marked and initialized once. |
|
221 |
7336
|
222 if (init_list) |
|
223 init_list->eval (do_init); |
2982
|
224 |
7336
|
225 if (error_state) |
|
226 ::error ("evaluating static command near line %d, column %d", |
|
227 line (), column ()); |
2982
|
228 } |
|
229 |
5861
|
230 tree_command * |
7336
|
231 tree_static_command::dup (symbol_table::scope_id scope) |
5861
|
232 { |
7336
|
233 return new tree_static_command (init_list ? init_list->dup (scope) : 0, |
5861
|
234 line (), column ()); |
|
235 } |
|
236 |
2982
|
237 /* |
|
238 ;;; Local Variables: *** |
|
239 ;;; mode: C++ *** |
|
240 ;;; End: *** |
|
241 */ |