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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2887
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "error.h" |
|
29 #include "oct-obj.h" |
2979
|
30 #include "oct-lvalue.h" |
2900
|
31 #include "pager.h" |
3770
|
32 #include "pt-bp.h" |
2887
|
33 #include "pt-const.h" |
|
34 #include "pt-id.h" |
|
35 #include "pt-walk.h" |
2900
|
36 #include "symtab.h" |
2887
|
37 #include "utils.h" |
2956
|
38 #include "variables.h" |
2887
|
39 |
|
40 // Symbols from the symbol table. |
|
41 |
3536
|
42 std::string |
2887
|
43 tree_identifier::name (void) const |
|
44 { |
3523
|
45 std::string retval; |
2887
|
46 if (sym) |
|
47 retval = sym->name (); |
|
48 return retval; |
|
49 } |
|
50 |
|
51 tree_identifier * |
|
52 tree_identifier::define (octave_function *f, unsigned int sym_type) |
|
53 { |
|
54 int status = sym->define (f, sym_type); |
|
55 return status ? this : 0; |
|
56 } |
|
57 |
|
58 void |
3523
|
59 tree_identifier::document (const std::string& s) |
2887
|
60 { |
|
61 if (sym) |
|
62 sym->document (s); |
|
63 } |
|
64 |
|
65 bool |
|
66 tree_identifier::is_defined (void) |
|
67 { |
|
68 return (sym && sym->is_defined ()); |
|
69 } |
|
70 |
2971
|
71 bool |
|
72 tree_identifier::is_function (void) |
|
73 { |
|
74 return (sym && sym->is_function ()); |
|
75 } |
|
76 |
2887
|
77 void |
|
78 tree_identifier::eval_undefined_error (void) |
|
79 { |
|
80 int l = line (); |
|
81 int c = column (); |
|
82 |
|
83 if (l == -1 && c == -1) |
|
84 ::error ("`%s' undefined", name ().c_str ()); |
|
85 else |
|
86 ::error ("`%s' undefined near line %d column %d", |
|
87 name ().c_str (), l, c); |
|
88 } |
|
89 |
|
90 // Try to find a definition for an identifier. Here's how: |
|
91 // |
|
92 // * If the identifier is already defined and is a function defined |
|
93 // in an function file that has been modified since the last time |
|
94 // we parsed it, parse it again. |
|
95 // |
|
96 // * If the identifier is not defined, try to find a builtin |
|
97 // variable or an already compiled function with the same name. |
|
98 // |
|
99 // * If the identifier is still undefined, try looking for an |
|
100 // function file to parse. |
|
101 // |
5864
|
102 // * On systems that support dynamic linking, we prefer .oct files, |
|
103 // then .mex files, then .m files. |
2887
|
104 |
2971
|
105 octave_value |
2887
|
106 tree_identifier::do_lookup (bool& script_file_executed, bool exec_script) |
|
107 { |
2971
|
108 static octave_value foo; |
|
109 |
2887
|
110 script_file_executed = lookup (sym, exec_script); |
|
111 |
2971
|
112 return script_file_executed ? foo : sym->def (); |
2887
|
113 } |
|
114 |
|
115 void |
|
116 tree_identifier::link_to_global (void) |
|
117 { |
|
118 if (sym) |
4009
|
119 { |
|
120 if (! sym->is_linked_to_global ()) |
|
121 { |
|
122 if (sym->is_defined () && sym->is_variable ()) |
6257
|
123 { |
|
124 std::string nm = sym->name (); |
|
125 |
|
126 warning ("value of local variable `%s' may have changed to match global", |
|
127 nm.c_str ()); |
|
128 } |
4009
|
129 |
|
130 link_to_global_variable (sym); |
|
131 } |
|
132 } |
2887
|
133 } |
|
134 |
|
135 void |
|
136 tree_identifier::mark_as_static (void) |
|
137 { |
|
138 if (sym) |
|
139 sym->mark_as_static (); |
|
140 } |
|
141 |
|
142 void |
|
143 tree_identifier::mark_as_formal_parameter (void) |
|
144 { |
|
145 if (sym) |
|
146 sym->mark_as_formal_parameter (); |
|
147 } |
|
148 |
|
149 octave_value_list |
2971
|
150 tree_identifier::rvalue (int nargout) |
2887
|
151 { |
|
152 octave_value_list retval; |
|
153 |
3770
|
154 MAYBE_DO_BREAKPOINT; |
|
155 |
2887
|
156 if (error_state) |
|
157 return retval; |
|
158 |
|
159 bool script_file_executed = false; |
|
160 |
2971
|
161 octave_value val = do_lookup (script_file_executed); |
2887
|
162 |
|
163 if (! script_file_executed) |
|
164 { |
2971
|
165 if (val.is_defined ()) |
2887
|
166 { |
5775
|
167 // GAGME -- this would be cleaner if we required |
2971
|
168 // parens to indicate function calls. |
|
169 // |
|
170 // If this identifier refers to a function, we need to know |
|
171 // whether it is indexed so that we can do the same thing |
|
172 // for `f' and `f()'. If the index is present, return the |
|
173 // function object and let tree_index_expression::rvalue |
|
174 // handle indexing. Otherwise, arrange to call the function |
|
175 // here, so that we don't return the function definition as |
|
176 // a value. |
2887
|
177 |
2971
|
178 if (val.is_function () && ! is_postfix_indexed ()) |
|
179 { |
|
180 octave_value_list tmp_args; |
2887
|
181 |
3544
|
182 retval = val.do_multi_index_op (nargout, tmp_args); |
2887
|
183 } |
|
184 else |
2971
|
185 { |
|
186 if (print_result () && nargout == 0) |
|
187 val.print_with_name (octave_stdout, name ()); |
|
188 |
|
189 retval = val; |
|
190 } |
2887
|
191 } |
|
192 else |
|
193 eval_undefined_error (); |
|
194 } |
|
195 |
|
196 return retval; |
|
197 } |
|
198 |
2971
|
199 octave_value |
|
200 tree_identifier::rvalue (void) |
|
201 { |
|
202 octave_value retval; |
|
203 |
|
204 octave_value_list tmp = rvalue (1); |
|
205 |
|
206 if (! tmp.empty ()) |
|
207 retval = tmp(0); |
|
208 |
|
209 return retval; |
|
210 } |
|
211 |
2979
|
212 octave_lvalue |
2971
|
213 tree_identifier::lvalue (void) |
|
214 { |
3770
|
215 MAYBE_DO_BREAKPOINT; |
|
216 |
2971
|
217 return sym->variable_reference (); |
|
218 } |
|
219 |
5861
|
220 tree_identifier * |
|
221 tree_identifier::dup (symbol_table *sym_tab) |
|
222 { |
|
223 symbol_record *sr = (sym_tab && sym) ? sym_tab->lookup (sym->name ()) : 0; |
|
224 |
|
225 tree_identifier *new_id = new tree_identifier (sr, line (), column ()); |
|
226 |
|
227 new_id->copy_base (*this); |
|
228 |
|
229 return new_id; |
|
230 } |
|
231 |
2887
|
232 void |
|
233 tree_identifier::accept (tree_walker& tw) |
|
234 { |
|
235 tw.visit_identifier (*this); |
|
236 } |
|
237 |
|
238 /* |
|
239 ;;; Local Variables: *** |
|
240 ;;; mode: C++ *** |
|
241 ;;; End: *** |
|
242 */ |