2887
|
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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include "error.h" |
|
32 #include "oct-obj.h" |
|
33 #include "oct-fcn.h" |
|
34 #include "oct-sym.h" |
2900
|
35 #include "pager.h" |
2887
|
36 #include "pt-const.h" |
|
37 #include "pt-id.h" |
|
38 #include "pt-walk.h" |
2900
|
39 #include "symtab.h" |
2887
|
40 #include "utils.h" |
|
41 |
|
42 // Symbols from the symbol table. |
|
43 |
|
44 string |
|
45 tree_identifier::name (void) const |
|
46 { |
|
47 string retval; |
|
48 if (sym) |
|
49 retval = sym->name (); |
|
50 return retval; |
|
51 } |
|
52 |
|
53 tree_identifier * |
|
54 tree_identifier::define (octave_symbol *s, unsigned int sym_type) |
|
55 { |
|
56 int status = sym->define (s, sym_type); |
|
57 return status ? this : 0; |
|
58 } |
|
59 |
|
60 tree_identifier * |
|
61 tree_identifier::define (octave_function *f, unsigned int sym_type) |
|
62 { |
|
63 int status = sym->define (f, sym_type); |
|
64 return status ? this : 0; |
|
65 } |
|
66 |
|
67 tree_identifier * |
|
68 tree_identifier::define (const octave_value& v) |
|
69 { |
|
70 int status = sym->define (v); |
|
71 return status ? this : 0; |
|
72 } |
|
73 |
|
74 void |
|
75 tree_identifier::document (const string& s) |
|
76 { |
|
77 if (sym) |
|
78 sym->document (s); |
|
79 } |
|
80 |
|
81 octave_value |
|
82 tree_identifier::assign (octave_value::assign_op op, const octave_value& rhs) |
|
83 { |
|
84 octave_value retval; |
|
85 |
|
86 if (rhs.is_defined ()) |
|
87 { |
|
88 if (! sym->is_defined ()) |
|
89 { |
|
90 if (! (sym->is_formal_parameter () |
|
91 || sym->is_linked_to_global ())) |
|
92 { |
|
93 link_to_builtin_variable (sym); |
|
94 } |
|
95 } |
|
96 else if (sym->is_function ()) |
|
97 { |
|
98 sym->clear (); |
|
99 } |
|
100 |
|
101 // XXX FIXME XXX -- make this work for ops other than `='. |
|
102 |
|
103 if (sym->define (rhs)) |
|
104 retval = rhs; |
|
105 } |
|
106 |
|
107 return retval; |
|
108 } |
|
109 |
|
110 octave_value |
|
111 tree_identifier::assign (octave_value::assign_op op, |
|
112 const octave_value_list& args, |
|
113 const octave_value& rhs) |
|
114 { |
|
115 octave_value retval; |
|
116 |
|
117 if (rhs.is_defined ()) |
|
118 { |
|
119 if (! sym->is_defined ()) |
|
120 { |
|
121 if (! (sym->is_formal_parameter () |
|
122 || sym->is_linked_to_global ())) |
|
123 { |
|
124 link_to_builtin_variable (sym); |
|
125 } |
|
126 } |
|
127 else if (sym->is_function ()) |
|
128 { |
|
129 sym->clear (); |
|
130 } |
|
131 |
|
132 if (sym->is_variable () && sym->is_defined ()) |
|
133 { |
|
134 sym->variable_reference () . assign (op, args, rhs); |
|
135 } |
|
136 else |
|
137 { |
|
138 assert (! sym->is_defined ()); |
|
139 |
|
140 if (! Vresize_on_range_error) |
|
141 { |
|
142 ::error ("indexed assignment to previously undefined variables"); |
|
143 ::error ("is only possible when resize_on_range_error is true"); |
|
144 } |
|
145 else |
|
146 { |
|
147 retval.assign (op, args, rhs); |
|
148 |
|
149 if (retval.is_defined ()) |
|
150 sym->define (retval); |
|
151 } |
|
152 } |
|
153 } |
|
154 |
|
155 return retval; |
|
156 } |
|
157 |
|
158 bool |
|
159 tree_identifier::is_defined (void) |
|
160 { |
|
161 return (sym && sym->is_defined ()); |
|
162 } |
|
163 |
|
164 void |
|
165 tree_identifier::increment (void) |
|
166 { |
|
167 if (sym) |
|
168 { |
|
169 if (sym->is_read_only ()) |
|
170 ::error ("can't redefined read-only variable `%s'", name ().c_str ()); |
|
171 else if (sym->is_defined () && sym->is_variable ()) |
|
172 reference () . increment (); |
|
173 else |
|
174 ::error ("can only increment variables"); |
|
175 } |
|
176 } |
|
177 |
|
178 void |
|
179 tree_identifier::decrement (void) |
|
180 { |
|
181 if (sym) |
|
182 { |
|
183 if (sym->is_read_only ()) |
|
184 ::error ("can't redefined read-only variable `%s'", name ().c_str ()); |
|
185 else if (sym->is_defined () && sym->is_variable ()) |
|
186 reference () . decrement (); |
|
187 else |
|
188 ::error ("can only decrement variables"); |
|
189 } |
|
190 } |
|
191 |
|
192 void |
|
193 tree_identifier::eval_undefined_error (void) |
|
194 { |
|
195 int l = line (); |
|
196 int c = column (); |
|
197 |
|
198 if (l == -1 && c == -1) |
|
199 ::error ("`%s' undefined", name ().c_str ()); |
|
200 else |
|
201 ::error ("`%s' undefined near line %d column %d", |
|
202 name ().c_str (), l, c); |
|
203 } |
|
204 |
|
205 // Try to find a definition for an identifier. Here's how: |
|
206 // |
|
207 // * If the identifier is already defined and is a function defined |
|
208 // in an function file that has been modified since the last time |
|
209 // we parsed it, parse it again. |
|
210 // |
|
211 // * If the identifier is not defined, try to find a builtin |
|
212 // variable or an already compiled function with the same name. |
|
213 // |
|
214 // * If the identifier is still undefined, try looking for an |
|
215 // function file to parse. |
|
216 // |
|
217 // * On systems that support dynamic linking, we prefer .oct files |
|
218 // over .m files. |
|
219 |
|
220 octave_symbol * |
|
221 tree_identifier::do_lookup (bool& script_file_executed, bool exec_script) |
|
222 { |
|
223 script_file_executed = lookup (sym, exec_script); |
|
224 |
|
225 octave_symbol *retval = 0; |
|
226 |
|
227 if (! script_file_executed) |
|
228 retval = sym->def (); |
|
229 |
|
230 return retval; |
|
231 } |
|
232 |
|
233 void |
|
234 tree_identifier::link_to_global (void) |
|
235 { |
|
236 if (sym) |
|
237 link_to_global_variable (sym); |
|
238 } |
|
239 |
|
240 void |
|
241 tree_identifier::mark_as_static (void) |
|
242 { |
|
243 if (sym) |
|
244 sym->mark_as_static (); |
|
245 } |
|
246 |
|
247 void |
|
248 tree_identifier::mark_as_formal_parameter (void) |
|
249 { |
|
250 if (sym) |
|
251 sym->mark_as_formal_parameter (); |
|
252 } |
|
253 |
|
254 octave_value |
|
255 tree_identifier::eval (bool print) |
|
256 { |
|
257 octave_value retval; |
|
258 |
|
259 if (error_state) |
|
260 return retval; |
|
261 |
|
262 bool script_file_executed = false; |
|
263 |
|
264 octave_symbol *object_to_eval = do_lookup (script_file_executed); |
|
265 |
|
266 if (! script_file_executed) |
|
267 { |
|
268 if (object_to_eval) |
|
269 { |
|
270 int nargout = maybe_do_ans_assign ? 0 : 1; |
|
271 |
|
272 if (nargout) |
|
273 { |
|
274 octave_value_list tmp_args; |
|
275 octave_value_list tmp = object_to_eval->eval (nargout, tmp_args); |
|
276 |
|
277 if (tmp.length () > 0) |
|
278 retval = tmp(0); |
|
279 } |
|
280 else |
|
281 retval = object_to_eval->eval (); |
|
282 } |
|
283 else |
|
284 eval_undefined_error (); |
|
285 } |
|
286 |
|
287 if (! error_state) |
|
288 { |
|
289 if (retval.is_defined ()) |
|
290 { |
|
291 if (maybe_do_ans_assign && ! object_to_eval->is_constant ()) |
|
292 bind_ans (retval, print); |
|
293 else if (print) |
2900
|
294 retval.print_with_name (octave_stdout, name ()); |
2887
|
295 } |
|
296 else if (object_to_eval && object_to_eval->is_constant ()) |
|
297 eval_undefined_error (); |
|
298 } |
|
299 |
|
300 return retval; |
|
301 } |
|
302 |
|
303 octave_value_list |
|
304 tree_identifier::eval (bool print, int nargout, const octave_value_list& args) |
|
305 { |
|
306 octave_value_list retval; |
|
307 |
|
308 if (error_state) |
|
309 return retval; |
|
310 |
|
311 bool script_file_executed = false; |
|
312 |
|
313 octave_symbol *object_to_eval = do_lookup (script_file_executed); |
|
314 |
|
315 if (! script_file_executed) |
|
316 { |
|
317 if (object_to_eval) |
|
318 { |
|
319 if (maybe_do_ans_assign && nargout == 1) |
|
320 { |
|
321 // Don't count the output arguments that we create |
|
322 // automatically. |
|
323 |
|
324 nargout = 0; |
|
325 |
|
326 retval = object_to_eval->eval (nargout, args); |
|
327 |
|
328 if (retval.length () > 0 && retval(0).is_defined ()) |
|
329 bind_ans (retval(0), print); |
|
330 } |
|
331 else |
|
332 retval = object_to_eval->eval (nargout, args); |
|
333 } |
|
334 else |
|
335 eval_undefined_error (); |
|
336 } |
|
337 |
|
338 return retval; |
|
339 } |
|
340 |
|
341 void |
|
342 tree_identifier::accept (tree_walker& tw) |
|
343 { |
|
344 tw.visit_identifier (*this); |
|
345 } |
|
346 |
|
347 octave_value |
|
348 tree_identifier::value (void) const |
|
349 { |
|
350 return sym->variable_value (); |
|
351 } |
|
352 |
|
353 octave_value& |
|
354 tree_identifier::reference (void) |
|
355 { |
|
356 return sym->variable_reference (); |
|
357 } |
|
358 |
|
359 /* |
|
360 ;;; Local Variables: *** |
|
361 ;;; mode: C++ *** |
|
362 ;;; End: *** |
|
363 */ |