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