Mercurial > hg > octave-lyh
annotate src/pt-decl.cc @ 7767:71f068b22fcc
scope and context fixes for function handles
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 07 May 2008 13:45:30 -0400 |
parents | 745a8299c2b5 |
children | 3100283874d7 |
rev | line source |
---|---|
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 * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
72 tree_decl_elt::dup (symbol_table::scope_id scope, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
73 symbol_table::context_id context) |
5861 | 74 { |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
75 return new tree_decl_elt (id ? id->dup (scope, context) : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
76 expr ? expr->dup (scope, context) : 0); |
5861 | 77 } |
78 | |
2982 | 79 void |
80 tree_decl_elt::accept (tree_walker& tw) | |
81 { | |
82 tw.visit_decl_elt (*this); | |
83 } | |
84 | |
85 // Initializer lists for declaration statements. | |
86 | |
87 void | |
2989 | 88 tree_decl_init_list::eval (tree_decl_elt::eval_fcn f) |
2982 | 89 { |
4219 | 90 for (iterator p = begin (); p != end (); p++) |
2982 | 91 { |
4219 | 92 tree_decl_elt *elt = *p; |
93 | |
94 f (*elt); | |
2982 | 95 |
96 if (error_state) | |
97 break; | |
98 } | |
99 } | |
100 | |
5861 | 101 tree_decl_init_list * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
102 tree_decl_init_list::dup (symbol_table::scope_id scope, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
103 symbol_table::context_id context) |
5861 | 104 { |
105 tree_decl_init_list *new_dil = new tree_decl_init_list (); | |
106 | |
107 for (iterator p = begin (); p != end (); p++) | |
108 { | |
109 tree_decl_elt *elt = *p; | |
110 | |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
111 new_dil->append (elt ? elt->dup (scope, context) : 0); |
5861 | 112 } |
113 | |
114 return new_dil; | |
115 } | |
116 | |
2982 | 117 void |
118 tree_decl_init_list::accept (tree_walker& tw) | |
119 { | |
120 tw.visit_decl_init_list (*this); | |
121 } | |
122 | |
123 // Base class for declaration commands (global, static). | |
124 | |
125 tree_decl_command::~tree_decl_command (void) | |
126 { | |
127 delete init_list; | |
128 } | |
129 | |
130 void | |
131 tree_decl_command::accept (tree_walker& tw) | |
132 { | |
133 tw.visit_decl_command (*this); | |
134 } | |
135 | |
136 // Global. | |
137 | |
2989 | 138 void |
139 tree_global_command::do_init (tree_decl_elt& elt) | |
2982 | 140 { |
141 tree_identifier *id = elt.ident (); | |
142 | |
143 if (id) | |
144 { | |
7336 | 145 id->mark_global (); |
2982 | 146 |
4009 | 147 if (! error_state) |
148 { | |
149 octave_lvalue ult = id->lvalue (); | |
2982 | 150 |
4009 | 151 if (ult.is_undefined ()) |
152 { | |
153 tree_expression *expr = elt.expression (); | |
2982 | 154 |
4009 | 155 octave_value init_val; |
3157 | 156 |
4009 | 157 if (expr) |
158 init_val = expr->rvalue (); | |
4464 | 159 else |
160 init_val = Matrix (); | |
2982 | 161 |
4009 | 162 ult.assign (octave_value::op_asn_eq, init_val); |
163 } | |
2982 | 164 } |
165 } | |
166 } | |
167 | |
168 void | |
169 tree_global_command::eval (void) | |
170 { | |
7205 | 171 MAYBE_DO_BREAKPOINT; |
172 | |
2982 | 173 if (init_list) |
7336 | 174 init_list->eval (do_init); |
2982 | 175 |
3965 | 176 if (error_state) |
2982 | 177 ::error ("evaluating global command near line %d, column %d", |
178 line (), column ()); | |
179 } | |
180 | |
5861 | 181 tree_command * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
182 tree_global_command::dup (symbol_table::scope_id scope, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
183 symbol_table::context_id context) |
5861 | 184 { |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
185 return |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
186 new tree_global_command (init_list ? init_list->dup (scope, context) : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
187 line (), column ()); |
5861 | 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 | |
7336 | 226 if (init_list) |
227 init_list->eval (do_init); | |
2982 | 228 |
7336 | 229 if (error_state) |
230 ::error ("evaluating static command near line %d, column %d", | |
231 line (), column ()); | |
2982 | 232 } |
233 | |
5861 | 234 tree_command * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
235 tree_static_command::dup (symbol_table::scope_id scope, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
236 symbol_table::context_id context) |
5861 | 237 { |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
238 return |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
239 new tree_static_command (init_list ? init_list->dup (scope, context) : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
240 line (), column ()); |
5861 | 241 } |
242 | |
2982 | 243 /* |
244 ;;; Local Variables: *** | |
245 ;;; mode: C++ *** | |
246 ;;; End: *** | |
247 */ |