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" |
2955
|
35 #include "oct-var-ref.h" |
2900
|
36 #include "pager.h" |
2887
|
37 #include "pt-const.h" |
|
38 #include "pt-id.h" |
|
39 #include "pt-walk.h" |
2900
|
40 #include "symtab.h" |
2887
|
41 #include "utils.h" |
|
42 |
|
43 // Symbols from the symbol table. |
|
44 |
|
45 string |
|
46 tree_identifier::name (void) const |
|
47 { |
|
48 string retval; |
|
49 if (sym) |
|
50 retval = sym->name (); |
|
51 return retval; |
|
52 } |
|
53 |
|
54 tree_identifier * |
|
55 tree_identifier::define (octave_symbol *s, unsigned int sym_type) |
|
56 { |
|
57 int status = sym->define (s, sym_type); |
|
58 return status ? this : 0; |
|
59 } |
|
60 |
|
61 tree_identifier * |
|
62 tree_identifier::define (octave_function *f, unsigned int sym_type) |
|
63 { |
|
64 int status = sym->define (f, sym_type); |
|
65 return status ? this : 0; |
|
66 } |
|
67 |
|
68 tree_identifier * |
|
69 tree_identifier::define (const octave_value& v) |
|
70 { |
|
71 int status = sym->define (v); |
|
72 return status ? this : 0; |
|
73 } |
|
74 |
|
75 void |
|
76 tree_identifier::document (const string& s) |
|
77 { |
|
78 if (sym) |
|
79 sym->document (s); |
|
80 } |
|
81 |
|
82 bool |
|
83 tree_identifier::is_defined (void) |
|
84 { |
|
85 return (sym && sym->is_defined ()); |
|
86 } |
|
87 |
|
88 void |
|
89 tree_identifier::increment (void) |
|
90 { |
|
91 if (sym) |
|
92 { |
|
93 if (sym->is_read_only ()) |
|
94 ::error ("can't redefined read-only variable `%s'", name ().c_str ()); |
|
95 else if (sym->is_defined () && sym->is_variable ()) |
|
96 reference () . increment (); |
|
97 else |
|
98 ::error ("can only increment variables"); |
|
99 } |
|
100 } |
|
101 |
|
102 void |
|
103 tree_identifier::decrement (void) |
|
104 { |
|
105 if (sym) |
|
106 { |
|
107 if (sym->is_read_only ()) |
|
108 ::error ("can't redefined read-only variable `%s'", name ().c_str ()); |
|
109 else if (sym->is_defined () && sym->is_variable ()) |
|
110 reference () . decrement (); |
|
111 else |
|
112 ::error ("can only decrement variables"); |
|
113 } |
|
114 } |
|
115 |
|
116 void |
|
117 tree_identifier::eval_undefined_error (void) |
|
118 { |
|
119 int l = line (); |
|
120 int c = column (); |
|
121 |
|
122 if (l == -1 && c == -1) |
|
123 ::error ("`%s' undefined", name ().c_str ()); |
|
124 else |
|
125 ::error ("`%s' undefined near line %d column %d", |
|
126 name ().c_str (), l, c); |
|
127 } |
|
128 |
|
129 // Try to find a definition for an identifier. Here's how: |
|
130 // |
|
131 // * If the identifier is already defined and is a function defined |
|
132 // in an function file that has been modified since the last time |
|
133 // we parsed it, parse it again. |
|
134 // |
|
135 // * If the identifier is not defined, try to find a builtin |
|
136 // variable or an already compiled function with the same name. |
|
137 // |
|
138 // * If the identifier is still undefined, try looking for an |
|
139 // function file to parse. |
|
140 // |
|
141 // * On systems that support dynamic linking, we prefer .oct files |
|
142 // over .m files. |
|
143 |
|
144 octave_symbol * |
|
145 tree_identifier::do_lookup (bool& script_file_executed, bool exec_script) |
|
146 { |
|
147 script_file_executed = lookup (sym, exec_script); |
|
148 |
|
149 octave_symbol *retval = 0; |
|
150 |
|
151 if (! script_file_executed) |
|
152 retval = sym->def (); |
|
153 |
|
154 return retval; |
|
155 } |
|
156 |
|
157 void |
|
158 tree_identifier::link_to_global (void) |
|
159 { |
|
160 if (sym) |
|
161 link_to_global_variable (sym); |
|
162 } |
|
163 |
|
164 void |
|
165 tree_identifier::mark_as_static (void) |
|
166 { |
|
167 if (sym) |
|
168 sym->mark_as_static (); |
|
169 } |
|
170 |
|
171 void |
|
172 tree_identifier::mark_as_formal_parameter (void) |
|
173 { |
|
174 if (sym) |
|
175 sym->mark_as_formal_parameter (); |
|
176 } |
|
177 |
|
178 octave_value |
|
179 tree_identifier::eval (bool print) |
|
180 { |
|
181 octave_value retval; |
|
182 |
|
183 if (error_state) |
|
184 return retval; |
|
185 |
|
186 bool script_file_executed = false; |
|
187 |
|
188 octave_symbol *object_to_eval = do_lookup (script_file_executed); |
|
189 |
|
190 if (! script_file_executed) |
|
191 { |
|
192 if (object_to_eval) |
|
193 { |
|
194 int nargout = maybe_do_ans_assign ? 0 : 1; |
|
195 |
|
196 if (nargout) |
|
197 { |
|
198 octave_value_list tmp_args; |
|
199 octave_value_list tmp = object_to_eval->eval (nargout, tmp_args); |
|
200 |
|
201 if (tmp.length () > 0) |
|
202 retval = tmp(0); |
|
203 } |
|
204 else |
|
205 retval = object_to_eval->eval (); |
|
206 } |
|
207 else |
|
208 eval_undefined_error (); |
|
209 } |
|
210 |
|
211 if (! error_state) |
|
212 { |
|
213 if (retval.is_defined ()) |
|
214 { |
|
215 if (maybe_do_ans_assign && ! object_to_eval->is_constant ()) |
|
216 bind_ans (retval, print); |
|
217 else if (print) |
2900
|
218 retval.print_with_name (octave_stdout, name ()); |
2887
|
219 } |
|
220 else if (object_to_eval && object_to_eval->is_constant ()) |
|
221 eval_undefined_error (); |
|
222 } |
|
223 |
|
224 return retval; |
|
225 } |
|
226 |
|
227 octave_value_list |
|
228 tree_identifier::eval (bool print, int nargout, const octave_value_list& args) |
|
229 { |
|
230 octave_value_list retval; |
|
231 |
|
232 if (error_state) |
|
233 return retval; |
|
234 |
|
235 bool script_file_executed = false; |
|
236 |
|
237 octave_symbol *object_to_eval = do_lookup (script_file_executed); |
|
238 |
|
239 if (! script_file_executed) |
|
240 { |
|
241 if (object_to_eval) |
|
242 { |
|
243 if (maybe_do_ans_assign && nargout == 1) |
|
244 { |
|
245 // Don't count the output arguments that we create |
|
246 // automatically. |
|
247 |
|
248 nargout = 0; |
|
249 |
|
250 retval = object_to_eval->eval (nargout, args); |
|
251 |
|
252 if (retval.length () > 0 && retval(0).is_defined ()) |
|
253 bind_ans (retval(0), print); |
|
254 } |
|
255 else |
|
256 retval = object_to_eval->eval (nargout, args); |
|
257 } |
|
258 else |
|
259 eval_undefined_error (); |
|
260 } |
|
261 |
|
262 return retval; |
|
263 } |
|
264 |
|
265 void |
|
266 tree_identifier::accept (tree_walker& tw) |
|
267 { |
|
268 tw.visit_identifier (*this); |
|
269 } |
|
270 |
|
271 octave_value |
|
272 tree_identifier::value (void) const |
|
273 { |
|
274 return sym->variable_value (); |
|
275 } |
|
276 |
2948
|
277 octave_variable_reference |
2887
|
278 tree_identifier::reference (void) |
|
279 { |
|
280 return sym->variable_reference (); |
|
281 } |
|
282 |
|
283 /* |
|
284 ;;; Local Variables: *** |
|
285 ;;; mode: C++ *** |
|
286 ;;; End: *** |
|
287 */ |