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 * |
|
72 tree_decl_elt::dup (symbol_table *sym_tab) |
|
73 { |
|
74 return new tree_decl_elt (id ? id->dup (sym_tab) : 0, |
|
75 expr ? expr->dup (sym_tab) : 0); |
|
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 * |
|
101 tree_decl_init_list::dup (symbol_table *sym_tab) |
|
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 |
|
109 new_dil->append (elt ? elt->dup (sym_tab) : 0); |
|
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 { |
|
143 id->link_to_global (); |
|
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) |
|
172 { |
2989
|
173 init_list->eval (do_init); |
2982
|
174 |
|
175 initialized = true; |
|
176 } |
|
177 |
3965
|
178 if (error_state) |
2982
|
179 ::error ("evaluating global command near line %d, column %d", |
|
180 line (), column ()); |
|
181 } |
|
182 |
5861
|
183 tree_command * |
|
184 tree_global_command::dup (symbol_table *sym_tab) |
|
185 { |
|
186 return new tree_global_command (init_list ? init_list->dup (sym_tab) : 0, |
|
187 line (), column ()); |
|
188 } |
|
189 |
2982
|
190 // Static. |
|
191 |
2989
|
192 void |
|
193 tree_static_command::do_init (tree_decl_elt& elt) |
2982
|
194 { |
|
195 tree_identifier *id = elt.ident (); |
|
196 |
|
197 if (id) |
|
198 { |
|
199 id->mark_as_static (); |
|
200 |
4844
|
201 octave_lvalue ult = id->lvalue (); |
2982
|
202 |
4846
|
203 if (ult.is_undefined ()) |
2982
|
204 { |
4844
|
205 tree_expression *expr = elt.expression (); |
|
206 |
|
207 octave_value init_val; |
2982
|
208 |
4844
|
209 if (expr) |
|
210 init_val = expr->rvalue (); |
|
211 else |
|
212 init_val = Matrix (); |
2982
|
213 |
3538
|
214 ult.assign (octave_value::op_asn_eq, init_val); |
2982
|
215 } |
|
216 } |
|
217 } |
|
218 |
|
219 void |
|
220 tree_static_command::eval (void) |
|
221 { |
7205
|
222 MAYBE_DO_BREAKPOINT; |
|
223 |
2982
|
224 // Static variables only need to be marked and initialized once. |
|
225 |
|
226 if (init_list && ! initialized) |
|
227 { |
2989
|
228 init_list->eval (do_init); |
2982
|
229 |
|
230 initialized = true; |
|
231 |
3965
|
232 if (error_state) |
2982
|
233 ::error ("evaluating static command near line %d, column %d", |
|
234 line (), column ()); |
|
235 } |
|
236 } |
|
237 |
5861
|
238 tree_command * |
|
239 tree_static_command::dup (symbol_table *sym_tab) |
|
240 { |
|
241 return new tree_static_command (init_list ? init_list->dup (sym_tab) : 0, |
|
242 line (), column ()); |
|
243 } |
|
244 |
2982
|
245 /* |
|
246 ;;; Local Variables: *** |
|
247 ;;; mode: C++ *** |
|
248 ;;; End: *** |
|
249 */ |