diff src/symtab.h @ 3011:2ad9af85b89b

[project @ 1997-06-01 19:34:20 by jwe]
author jwe
date Sun, 01 Jun 1997 19:37:26 +0000
parents a3556d2adec9
children 66a1cede95e7
line wrap: on
line diff
--- a/src/symtab.h	Sun Jun 01 19:29:53 1997 +0000
+++ b/src/symtab.h	Sun Jun 01 19:37:26 1997 +0000
@@ -27,61 +27,30 @@
 #pragma interface
 #endif
 
+#include <cassert>
+
 #include <string>
 
 #include "SLStack.h"
 
+#include "oct-alloc.h"
 #include "str-vec.h"
 
 #include "ov.h"
 
-// Must be multiple of 2.
-#define HASH_TABLE_SIZE 1024
-#define HASH_MASK (HASH_TABLE_SIZE - 1)
-
 class octave_lvalue;
 
 class string_vector;
 
-class symbol_def;
 class symbol_record;
-class symbol_record_info;
 class symbol_table;
 
-// Variables or functions.
-
-class symbol_def
-{
-  friend class symbol_record;
-  friend class symbol_record_info;
-
-public:
-
-  symbol_def (void);
-
-  symbol_def (const octave_value& val, unsigned int sym_type = 0);
-
-  ~symbol_def (void) { }
+// Individual records in a symbol table.
 
-  bool is_variable (void) const;
-  bool is_function (void) const;
-  bool is_text_function (void) const;
-  bool is_mapper_function (void) const;
-  bool is_user_variable (void) const;
-  bool is_user_function (void) const;
-  bool is_builtin_variable (void) const;
-  bool is_builtin_function (void) const;
-  bool is_map_element (const string& elts) const;
-
-  void define (const octave_value& val, unsigned int sym_type);
-
-  void protect (void);
-  void unprotect (void);
-  void make_eternal (void);
-
-  octave_value& def (void);
-  string help (void) const;
-  void document (const string& h);
+class
+symbol_record
+{
+public:
 
   enum TYPE
     {
@@ -94,104 +63,221 @@
       BUILTIN_VARIABLE = 32
     };
 
-  unsigned int symbol_type (void) { return type; }
-
-  friend maybe_delete (symbol_def *def);
-
 private:
 
-  unsigned int type : 6;
-  unsigned int eternal : 1;
-  unsigned int read_only : 1;
+  // Variables or functions.
+
+  class symbol_def
+  {
+  public:
+
+    symbol_def (const octave_value& val = octave_value (),
+		unsigned int sym_type = 0)
+      : symbol_type (sym_type), eternal (0), read_only (0), help_string (),
+	definition (val), next_elem (0), count (1) { }
+
+    ~symbol_def (void) { }
+
+    bool is_variable (void) const
+      {
+	return (symbol_type & symbol_record::USER_VARIABLE
+		|| symbol_type & symbol_record::BUILTIN_VARIABLE);
+      }
+
+    bool is_function (void) const
+      {
+	return (symbol_type & symbol_record::USER_FUNCTION
+		|| symbol_type & symbol_record::BUILTIN_FUNCTION);
+      }
+
+    bool is_user_variable (void) const
+      { return (symbol_type & symbol_record::USER_VARIABLE); }
 
-  string help_string;
-  octave_value definition;
-  symbol_def *next_elem;
-  int count;
+    bool is_text_function (void) const
+      { return (symbol_type & symbol_record::TEXT_FUNCTION); }
+
+    bool is_mapper_function (void) const
+      { return (symbol_type & symbol_record::MAPPER_FUNCTION); }
+
+    bool is_user_function (void) const
+      { return (symbol_type & symbol_record::USER_FUNCTION); }
+
+    bool is_builtin_variable (void) const
+      { return (symbol_type & symbol_record::BUILTIN_VARIABLE); }
 
-  void init_state (void);
+    bool is_builtin_function (void) const
+      { return (symbol_type & symbol_record::BUILTIN_FUNCTION); }
+
+    // XXX FIXME XXX
+    bool is_map_element (const string& /* elts */) const
+      { return false; }
+
+    bool is_defined (void) const
+      { return definition.is_defined (); }
+
+    bool is_read_only (void) const
+      { return read_only; }
+
+    bool is_eternal (void) const
+      { return eternal; }
 
-  symbol_def (const symbol_def& sd);
-  symbol_def& operator = (const symbol_def& sd);
-};
+    void define (const octave_value& val, unsigned int sym_type)
+      {
+	definition = val;
+	symbol_type = sym_type;
+      }
+
+    void protect (void) { read_only = 1; }
+
+    void unprotect (void) { read_only = 0; }
+
+    void make_eternal (void) { eternal = 1; }
+
+    octave_value& def (void) { return definition; }
+
+    string help (void) const { return help_string; }
+
+    void document (const string& h) { help_string = h; }
+
+    unsigned int type (void) { return symbol_type; }
+
+    void *operator new (size_t size)
+      { return allocator.alloc (size); }
+
+    void operator delete (void *p, size_t size)
+      { allocator.free (p, size); }
+
+    static octave_allocator allocator;
 
-// Individual records in a symbol table.
+    // The type of this symbol (see the enum above).
+    unsigned int symbol_type : 6;
+
+    // Nonzero means this variable cannot be cleared.
+    unsigned int eternal : 1;
+
+    // Nonzero means this variable cannot be given a new value.
+    unsigned int read_only : 1;
+
+    // The doc string associated with this variable.
+    string help_string;
+
+    // The value of this definition.  See ov.h and related files.
+    octave_value definition;
 
-class
-symbol_record
-{
-  friend class symbol_record_info;
+    // Pointer to next definition in chain.  This is used so that
+    // variables can hide function definitions, and so that the function
+    // definitions can reappear if the variable is cleared.
+    symbol_def *next_elem;
+
+    // Reference count.
+    int count;
+
+    // No copying!
+
+    symbol_def (const symbol_def& sd);
+
+    symbol_def& operator = (const symbol_def& sd);
+  };
 
 public:
 
-  typedef int (*sv_function) (void);
+  typedef int (*change_function) (void);
 
-  symbol_record (void);
-  symbol_record (const string& n, symbol_record *nxt = 0);
+  symbol_record (void)
+    : formal_param (0), linked_to_global (0), tagged_static (0),
+      nm (), chg_fcn (0), definition (new symbol_def ()),
+      next_elem (0) { }
+
+  symbol_record (const string& n, symbol_record *nxt)
+    : formal_param (0), linked_to_global (0), tagged_static (0),
+      nm (n), chg_fcn (0), definition (new symbol_def ()),
+      next_elem (nxt) { }
 
   ~symbol_record (void) { }
 
-  string name (void) const;
-  string help (void) const; 
+  string name (void) const { return nm; }
 
-  octave_value& def (void);
+  string help (void) const { return definition->help (); }
+
+  octave_value& def (void) { return definition->def (); }
 
   void rename (const string& new_name);
 
-  bool is_function (void) const;
-  bool is_user_function (void) const;
-  bool is_text_function (void) const;
-  bool is_mapper_function (void) const;
-  bool is_builtin_function (void) const;
-  bool is_variable (void) const;
-  bool is_user_variable (void) const;
-  bool is_builtin_variable (void) const;
-  bool is_map_element (const string& elts) const;
+  bool is_function (void) const
+    { return definition->is_function (); }
+
+  bool is_text_function (void) const
+    { return definition->is_text_function (); }
+
+  bool is_mapper_function (void) const
+    { return definition->is_mapper_function (); }
+
+  bool is_user_function (void) const
+    { return definition->is_user_function (); }
 
-  unsigned int type (void) const;
+  bool is_builtin_function (void) const
+    { return definition->is_builtin_function (); }
+
+  bool is_variable (void) const
+    { return definition->is_variable (); }
 
-  bool is_defined (void) const;
-  bool is_read_only (void) const;
-  bool is_eternal (void) const;
+  bool is_user_variable (void) const
+    { return definition->is_user_variable (); }
+
+  bool is_builtin_variable (void) const
+    { return definition->is_builtin_variable (); }
+
+  bool is_map_element (const string& elts) const
+    { return definition->is_map_element (elts); }
 
-  void protect (void);
-  void unprotect (void);
-  void make_eternal (void);
+  unsigned int type (void) const { return definition->type (); }
+
+  bool is_defined (void) const { return definition->is_defined (); }
+
+  bool is_read_only (void) const { return definition->is_read_only (); }
 
-  void set_sv_function (sv_function f);
+  bool is_eternal (void) const { return definition->is_eternal (); }
+
+  void protect (void) { definition->protect (); }
 
-  int define (const octave_value& v,
-	      unsigned int sym_type = symbol_def::USER_VARIABLE);
+  void unprotect (void) { definition->unprotect (); }
+
+  void make_eternal (void) { definition->make_eternal (); }
 
-  int define_as_fcn (const octave_value& v);
+  void set_change_function (change_function f) { chg_fcn = f; }
 
-  int define_builtin_var (const octave_value& v);
+  void define (const octave_value& v, unsigned int sym_type = USER_VARIABLE);
+
+  void define_builtin_var (const octave_value& v);
 
-  int define (octave_function *f, unsigned int sym_type);
+  bool define_as_fcn (const octave_value& v);
+
+  bool define (octave_function *f, unsigned int sym_type);
 
-  void document (const string& h);
+  void document (const string& h) { definition->document (h); }
 
-  int clear (void);
+  void clear (void);
 
   void alias (symbol_record *s, bool force = false);
 
   void mark_as_formal_parameter (void);
-  bool is_formal_parameter (void) const;
+  bool is_formal_parameter (void) const { return formal_param; }
 
   void mark_as_linked_to_global (void);
-  bool is_linked_to_global (void) const;
+  bool is_linked_to_global (void) const { return linked_to_global; }
 
   void mark_as_static (void);
-  bool is_static (void) const;
+  bool is_static (void) const { return tagged_static; }
 
   octave_value& variable_value (void);
   octave_lvalue variable_reference (void);
 
-  symbol_record *next (void) const;
+  symbol_record *next (void) const { return next_elem; }
 
-  void chain (symbol_record *s);
+  void chain (symbol_record *s) { next_elem = s; }
 
   void push_context (void);
+
   void pop_context (void);
 
 private:
@@ -201,25 +287,31 @@
   unsigned int tagged_static : 1;
 
   string nm;
-  sv_function sv_fcn;
+  change_function chg_fcn;
   symbol_def *definition;
   symbol_record *next_elem;
 
-// This should maybe be one stack with a structure containing all the
-// items we need to save for recursive calls...
+  // This should maybe be one stack with a structure containing all the
+  // items we need to save for recursive calls...
   SLStack <symbol_def *> context;
   SLStack <unsigned int> global_link_context;
 
-  void init_state (void);
-
-  int read_only_error (const char *action);
+  bool read_only_error (const char *action);
 
   void push_def (symbol_def *sd);
-  symbol_def *pop_def (void);
+
+  void remove_top_def (void);
+
+  void replace_all_defs (symbol_def *sd);
+
+  // No copying!
+
+  symbol_record (const symbol_record& s);
 
   symbol_record& operator = (const symbol_record& s);
 };
 
+#if 0
 // A structure for handling verbose information about a symbol_record.
 
 class
@@ -266,6 +358,7 @@
   string nm;
   string const_type;
 };
+#endif
 
 // A symbol table.
 
@@ -274,22 +367,31 @@
 
 #define SYMTAB_ALL_SCOPES (SYMTAB_LOCAL_SCOPE | SYMTAB_GLOBAL_SCOPE)
 
-#define SYMTAB_ALL_TYPES (symbol_def::USER_FUNCTION \
-			  | symbol_def::USER_VARIABLE \
-			  | symbol_def::BUILTIN_FUNCTION \
-			  | symbol_def::TEXT_FUNCTION \
-			  | symbol_def::MAPPER_FUNCTION \
-			  | symbol_def::BUILTIN_VARIABLE)
+#define SYMTAB_ALL_TYPES (symbol_record::USER_FUNCTION \
+			  | symbol_record::USER_VARIABLE \
+			  | symbol_record::BUILTIN_FUNCTION \
+			  | symbol_record::TEXT_FUNCTION \
+			  | symbol_record::MAPPER_FUNCTION \
+			  | symbol_record::BUILTIN_VARIABLE)
 
-#define SYMTAB_VARIABLES (symbol_def::USER_VARIABLE \
-			  | symbol_def::BUILTIN_VARIABLE)
+#define SYMTAB_VARIABLES (symbol_record::USER_VARIABLE \
+			  | symbol_record::BUILTIN_VARIABLE)
 
 class
 symbol_table
 {
 public:
 
-  symbol_table (void);
+  symbol_table (unsigned int tab_size = 128)
+    : table_size (tab_size), table (new symbol_record [table_size])
+  {
+    assert ((tab_size % 2) == 0);
+  }
+
+  ~symbol_table (void)
+  {
+    delete [] table;
+  }
 
   symbol_record *lookup (const string& nm, bool insert = false,
 			 bool warn = false);
@@ -297,15 +399,17 @@
   void rename (const string& old_name, const string& new_name);
 
   void clear (bool clear_user_functions = true);
-  int clear (const string& nm, bool clear_user_functions = true);
+  bool clear (const string& nm, bool clear_user_functions = true);
 
   int size (void) const;
 
+#if 0
   symbol_record_info *
   long_list (int& count, const string_vector& pats = string_vector (),
 	     int npats = 0, bool sort = false,
 	     unsigned int type = SYMTAB_ALL_TYPES,
 	     unsigned int scope = SYMTAB_ALL_SCOPES) const;
+#endif
 
   string_vector
   list (int& count, const string_vector& pats = string_vector (),
@@ -313,23 +417,35 @@
 	unsigned int type = SYMTAB_ALL_TYPES,
 	unsigned int scope = SYMTAB_ALL_SCOPES) const;
 
+
+  int maybe_list (const char *header, const string_vector& argv,
+		  int argc, ostream& os, bool show_verbose,
+		  unsigned type, unsigned scope);
+  
   symbol_record **glob (int& count, const string& pat = string ("*"),
 			unsigned int type = SYMTAB_ALL_TYPES,
 			unsigned int scope = SYMTAB_ALL_SCOPES) const;
 
   void push_context (void);
+
   void pop_context (void);
 
+  void print_stats (void);
+
 private:
 
+  unsigned int table_size;
+
+  symbol_record *table;
+
   unsigned int hash (const string& s);
 
-  symbol_record table[HASH_TABLE_SIZE];
-};
+  // No copying!
 
-extern bool valid_identifier (const char *s);
+  symbol_table (const symbol_table&);
 
-extern bool valid_identifier (const string& s);
+  symbol_table& operator = (const symbol_table&);
+};
 
 #endif