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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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 |
5861
|
48 tree_decl_elt * |
|
49 tree_decl_elt::dup (symbol_table *sym_tab) |
|
50 { |
|
51 return new tree_decl_elt (id ? id->dup (sym_tab) : 0, |
|
52 expr ? expr->dup (sym_tab) : 0); |
|
53 } |
|
54 |
2982
|
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 { |
4219
|
66 for (iterator p = begin (); p != end (); p++) |
2982
|
67 { |
4219
|
68 tree_decl_elt *elt = *p; |
|
69 |
|
70 f (*elt); |
2982
|
71 |
|
72 if (error_state) |
|
73 break; |
|
74 } |
|
75 } |
|
76 |
5861
|
77 tree_decl_init_list * |
|
78 tree_decl_init_list::dup (symbol_table *sym_tab) |
|
79 { |
|
80 tree_decl_init_list *new_dil = new tree_decl_init_list (); |
|
81 |
|
82 for (iterator p = begin (); p != end (); p++) |
|
83 { |
|
84 tree_decl_elt *elt = *p; |
|
85 |
|
86 new_dil->append (elt ? elt->dup (sym_tab) : 0); |
|
87 } |
|
88 |
|
89 return new_dil; |
|
90 } |
|
91 |
2982
|
92 void |
|
93 tree_decl_init_list::accept (tree_walker& tw) |
|
94 { |
|
95 tw.visit_decl_init_list (*this); |
|
96 } |
|
97 |
|
98 // Base class for declaration commands (global, static). |
|
99 |
|
100 tree_decl_command::~tree_decl_command (void) |
|
101 { |
|
102 delete init_list; |
|
103 } |
|
104 |
|
105 void |
|
106 tree_decl_command::accept (tree_walker& tw) |
|
107 { |
|
108 tw.visit_decl_command (*this); |
|
109 } |
|
110 |
|
111 // Global. |
|
112 |
2989
|
113 void |
|
114 tree_global_command::do_init (tree_decl_elt& elt) |
2982
|
115 { |
|
116 tree_identifier *id = elt.ident (); |
|
117 |
|
118 if (id) |
|
119 { |
|
120 id->link_to_global (); |
|
121 |
4009
|
122 if (! error_state) |
|
123 { |
|
124 octave_lvalue ult = id->lvalue (); |
2982
|
125 |
4009
|
126 if (ult.is_undefined ()) |
|
127 { |
|
128 tree_expression *expr = elt.expression (); |
2982
|
129 |
4009
|
130 octave_value init_val; |
3157
|
131 |
4009
|
132 if (expr) |
|
133 init_val = expr->rvalue (); |
4464
|
134 else |
|
135 init_val = Matrix (); |
2982
|
136 |
4009
|
137 ult.assign (octave_value::op_asn_eq, init_val); |
|
138 } |
2982
|
139 } |
|
140 } |
|
141 } |
|
142 |
|
143 void |
|
144 tree_global_command::eval (void) |
|
145 { |
|
146 if (init_list) |
|
147 { |
2989
|
148 init_list->eval (do_init); |
2982
|
149 |
|
150 initialized = true; |
|
151 } |
|
152 |
3965
|
153 if (error_state) |
2982
|
154 ::error ("evaluating global command near line %d, column %d", |
|
155 line (), column ()); |
|
156 } |
|
157 |
5861
|
158 tree_command * |
|
159 tree_global_command::dup (symbol_table *sym_tab) |
|
160 { |
|
161 return new tree_global_command (init_list ? init_list->dup (sym_tab) : 0, |
|
162 line (), column ()); |
|
163 } |
|
164 |
2982
|
165 // Static. |
|
166 |
2989
|
167 void |
|
168 tree_static_command::do_init (tree_decl_elt& elt) |
2982
|
169 { |
|
170 tree_identifier *id = elt.ident (); |
|
171 |
|
172 if (id) |
|
173 { |
|
174 id->mark_as_static (); |
|
175 |
4844
|
176 octave_lvalue ult = id->lvalue (); |
2982
|
177 |
4846
|
178 if (ult.is_undefined ()) |
2982
|
179 { |
4844
|
180 tree_expression *expr = elt.expression (); |
|
181 |
|
182 octave_value init_val; |
2982
|
183 |
4844
|
184 if (expr) |
|
185 init_val = expr->rvalue (); |
|
186 else |
|
187 init_val = Matrix (); |
2982
|
188 |
3538
|
189 ult.assign (octave_value::op_asn_eq, init_val); |
2982
|
190 } |
|
191 } |
|
192 } |
|
193 |
|
194 void |
|
195 tree_static_command::eval (void) |
|
196 { |
|
197 // Static variables only need to be marked and initialized once. |
|
198 |
|
199 if (init_list && ! initialized) |
|
200 { |
2989
|
201 init_list->eval (do_init); |
2982
|
202 |
|
203 initialized = true; |
|
204 |
3965
|
205 if (error_state) |
2982
|
206 ::error ("evaluating static command near line %d, column %d", |
|
207 line (), column ()); |
|
208 } |
|
209 } |
|
210 |
5861
|
211 tree_command * |
|
212 tree_static_command::dup (symbol_table *sym_tab) |
|
213 { |
|
214 return new tree_static_command (init_list ? init_list->dup (sym_tab) : 0, |
|
215 line (), column ()); |
|
216 } |
|
217 |
2982
|
218 /* |
|
219 ;;; Local Variables: *** |
|
220 ;;; mode: C++ *** |
|
221 ;;; End: *** |
|
222 */ |