comparison src/symtab.cc @ 5794:1138ced03f14

[project @ 2006-05-08 20:23:04 by jwe]
author jwe
date Mon, 08 May 2006 20:23:07 +0000
parents cbf717bf8150
children 368ce5470c90
comparison
equal deleted inserted replaced
5793:395382df0d8a 5794:1138ced03f14
54 unsigned long int symbol_table::symtab_count = 0; 54 unsigned long int symbol_table::symtab_count = 0;
55 55
56 // Should variables be allowed to hide functions of the same name? A 56 // Should variables be allowed to hide functions of the same name? A
57 // positive value means yes. A negative value means yes, but print a 57 // positive value means yes. A negative value means yes, but print a
58 // warning message. Zero means it should be considered an error. 58 // warning message. Zero means it should be considered an error.
59 static int Vvariables_can_hide_functions; 59 static int Vvariables_can_hide_functions = 1;
60 60
61 // Nonzero means we print debugging info about symbol table lookups. 61 // Nonzero means we print debugging info about symbol table lookups.
62 static int Vdebug_symtab_lookups; 62 static bool Vdebug_symtab_lookups = false;
63 63
64 // Defines layout for the whos/who -long command 64 // Defines layout for the whos/who -long command
65 std::string Vwhos_line_format; 65 std::string Vwhos_line_format
66 = " %p:4; %ln:6; %cs:16:6:8:1; %rb:12; %lt:-1;\n";
66 67
67 octave_allocator 68 octave_allocator
68 symbol_record::symbol_def::allocator (sizeof (symbol_record::symbol_def)); 69 symbol_record::symbol_def::allocator (sizeof (symbol_record::symbol_def));
69 70
70 #define SYMBOL_DEF symbol_record::symbol_def 71 #define SYMBOL_DEF symbol_record::symbol_def
80 retval = "built-in command"; 81 retval = "built-in command";
81 else if (is_mapper_function ()) 82 else if (is_mapper_function ())
82 retval = "built-in mapper function"; 83 retval = "built-in mapper function";
83 else if (is_user_function ()) 84 else if (is_user_function ())
84 retval = "user-defined function"; 85 retval = "user-defined function";
85 else if (is_builtin_variable ())
86 retval = "built-in variable";
87 else if (is_builtin_function ()) 86 else if (is_builtin_function ())
88 retval = "built-in function"; 87 retval = "built-in function";
89 else if (is_dld_function ()) 88 else if (is_dld_function ())
90 retval = "dynamically-linked function"; 89 retval = "dynamically-linked function";
91 90
128 tree_print_code tpc (os, "", pr_orig_txt); 127 tree_print_code tpc (os, "", pr_orig_txt);
129 128
130 defn->accept (tpc); 129 defn->accept (tpc);
131 } 130 }
132 } 131 }
133 else if (is_user_variable () || is_builtin_variable ()) 132 else if (is_user_variable ())
134 { 133 {
135 if (pr_type_info && ! quiet) 134 if (pr_type_info && ! quiet)
136 os << name << " is a " << type_as_string () << "\n"; 135 os << name << " is a " << type_as_string () << "\n";
137 136
138 definition.print_raw (os, true); 137 definition.print_raw (os, true);
204 203
205 void 204 void
206 symbol_record::define (const octave_value& v, unsigned int sym_type) 205 symbol_record::define (const octave_value& v, unsigned int sym_type)
207 { 206 {
208 if (! (is_variable () && read_only_error ("redefine"))) 207 if (! (is_variable () && read_only_error ("redefine")))
209 { 208 definition->define (v, sym_type);
210 if (definition->type () == symbol_record::BUILTIN_VARIABLE)
211 sym_type = symbol_record::BUILTIN_VARIABLE;
212
213 definition->define (v, sym_type);
214 }
215 }
216
217 void
218 symbol_record::define_builtin_var (const octave_value& v)
219 {
220 define (v, symbol_record::BUILTIN_VARIABLE);
221
222 if (chg_fcn)
223 chg_fcn ();
224 } 209 }
225 210
226 bool 211 bool
227 symbol_record::define (octave_function *f, unsigned int sym_type) 212 symbol_record::define (octave_function *f, unsigned int sym_type)
228 { 213 {
322 static octave_value foo; 307 static octave_value foo;
323 308
324 return is_variable () ? def () : foo; 309 return is_variable () ? def () : foo;
325 } 310 }
326 311
327 inline void
328 symbol_record::link_to_builtin_variable (void)
329 {
330 symbol_record *tmp_sym = fbi_sym_tab->lookup (name ());
331
332 if (tmp_sym && tmp_sym->is_builtin_variable ())
333 alias (tmp_sym);
334 }
335
336 octave_lvalue 312 octave_lvalue
337 symbol_record::variable_reference (void) 313 symbol_record::variable_reference (void)
338 { 314 {
339 if ((Vvariables_can_hide_functions <= 0 || ! can_hide_function) 315 if ((Vvariables_can_hide_functions <= 0 || ! can_hide_function)
340 && (is_function () 316 && (is_function ()
352 if (is_function ()) 328 if (is_function ())
353 clear (); 329 clear ();
354 330
355 if (! is_defined ()) 331 if (! is_defined ())
356 { 332 {
357 if (! (is_formal_parameter () || is_linked_to_global ())) 333 octave_value tmp;
358 link_to_builtin_variable (); 334 define (tmp);
359
360 if (! is_defined ())
361 {
362 octave_value tmp;
363 define (tmp);
364 }
365 } 335 }
366 336
367 return octave_lvalue (&(def ()), chg_fcn); 337 return octave_lvalue (&(def ()), chg_fcn);
368 } 338 }
369 339
1723 h = h * 33 + str[i]; 1693 h = h * 33 + str[i];
1724 1694
1725 return h & (table_size - 1); 1695 return h & (table_size - 1);
1726 } 1696 }
1727 1697
1728 1698 DEFUN (debug_symtab_lookups, args, nargout,
1729 static int 1699 "-*- texinfo -*-\n\
1730 variables_can_hide_functions (void) 1700 @deftypefn {Built-in Function} {@var{val} =} debug_symtab_lookups ()\n\
1731 { 1701 @deftypefnx {Built-in Function} {@var{old_val} =} debug_symtab_lookups (@var{new_val})\n\
1732 Vvariables_can_hide_functions 1702 Query or set the internal variable that controls whether debugging\n\
1733 = check_preference ("variables_can_hide_functions"); 1703 information is printed when searching for symbols in the symbol tables.\n\
1734 1704 @end deftypefn")
1735 return 0; 1705 {
1736 } 1706 return SET_INTERNAL_VARIABLE (debug_symtab_lookups);
1737 1707 }
1738 static int 1708
1739 debug_symtab_lookups (void) 1709 DEFUN (whos_line_format, args, nargout,
1740 { 1710 "-*- texinfo -*-\n\
1741 Vdebug_symtab_lookups = check_preference ("debug_symtab_lookups"); 1711 @deftypefn {Built-in Function} {@var{val} =} whos_line_format ()\n\
1742 1712 @deftypefnx {Built-in Function} {@var{old_val} =} whos_line_format (@var{new_val})\n\
1743 return 0; 1713 Query or set the format string used by the @code{whos}.\n\
1744 } 1714 \n\
1745 1715 The following escape sequences may be used in the format:\n\
1746 static int
1747 whos_line_format (void)
1748 {
1749 Vwhos_line_format = builtin_string_variable ("whos_line_format");
1750
1751 return 0;
1752 }
1753
1754 void
1755 symbols_of_symtab (void)
1756 {
1757 DEFVAR (variables_can_hide_functions, true, variables_can_hide_functions,
1758 "-*- texinfo -*-\n\
1759 @defvr {Built-in Variable} variables_can_hide_functions\n\
1760 If the value of this variable is nonzero, assignments to variables may\n\
1761 hide previously defined functions of the same name. A negative value\n\
1762 will cause Octave to print a warning, but allow the operation.\n\
1763 @end defvr");
1764
1765 DEFVAR (debug_symtab_lookups, false, debug_symtab_lookups,
1766 "-*- texinfo -*-\n\
1767 @defvr debug_symtab_lookups\n\
1768 If the value of this variable is nonzero, print debugging info when\n\
1769 searching for symbols in the symbol tables.\n\
1770 @end defvr");
1771
1772 DEFVAR (whos_line_format, " %p:4; %ln:6; %cs:16:6:8:1; %rb:12; %lt:-1;\n", whos_line_format,
1773 "-*- texinfo -*-\n\
1774 @defvr {Built-in Variable} whos_line_format\n\
1775 This string decides in what order attributtes of variables are to be printed.\n\
1776 The following commands are used:\n\
1777 @table @code\n\ 1716 @table @code\n\
1778 @item %b\n\ 1717 @item %b\n\
1779 Prints number of bytes occupied by variables.\n\ 1718 Prints number of bytes occupied by variables.\n\
1780 @item %e\n\ 1719 @item %e\n\
1781 Prints elements held by variables.\n\ 1720 Prints elements held by variables.\n\
1810 center-specific and print_dims may only be applied to command\n\ 1749 center-specific and print_dims may only be applied to command\n\
1811 %s. A negative value for print_dims will cause Octave to print all\n\ 1750 %s. A negative value for print_dims will cause Octave to print all\n\
1812 dimensions whatsoever.\n\ 1751 dimensions whatsoever.\n\
1813 balance specifies the offset for printing of the dimensions string.\n\ 1752 balance specifies the offset for printing of the dimensions string.\n\
1814 \n\ 1753 \n\
1815 Default format is \" %p:4; %ln:6; %cs:16:6:8:1; %rb:12; %lt:-1;\\n\".\n\ 1754 The default format is \" %p:4; %ln:6; %cs:16:6:8:1; %rb:12; %lt:-1;\\n\".\n\
1816 @end defvr"); 1755 @end deftypefn")
1756 {
1757 return SET_INTERNAL_VARIABLE (whos_line_format);
1758 }
1759
1760 DEFUN (variables_can_hide_functions, args, nargout,
1761 "-*- texinfo -*-\n\
1762 @deftypefn {Built-in Function} {@var{val} =} variables_can_hide_functions ()\n\
1763 @deftypefnx {Built-in Function} {@var{old_val} =} variables_can_hide_functions (@var{new_val})\n\
1764 Query or set the internal variable that controls whether assignments\n\
1765 to variables may hide previously defined functions of the same name.\n\
1766 If set to a nonzero value allows hiding, zero causes Octave to\n\
1767 generate an error, and a negative value cause Octave to print a\n\
1768 warning, but allow the operation.\n\
1769 @end deftypefn")
1770 {
1771 return SET_INTERNAL_VARIABLE (variables_can_hide_functions);
1817 } 1772 }
1818 1773
1819 /* 1774 /*
1820 ;;; Local Variables: *** 1775 ;;; Local Variables: ***
1821 ;;; mode: C++ *** 1776 ;;; mode: C++ ***