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