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