home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / src / symtab.h < prev    next >
C/C++ Source or Header  |  1995-01-03  |  7KB  |  327 lines

  1. // symtab.h                                             -*- C++ -*-
  2. /*
  3.  
  4. Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton
  5.  
  6. This file is part of Octave.
  7.  
  8. Octave is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2, or (at your option) any
  11. later version.
  12.  
  13. Octave is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with Octave; see the file COPYING.  If not, write to the Free
  20. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. */
  23.  
  24. #if !defined (octave_symtab_h)
  25. #define octave_symtab_h 1
  26.  
  27. #include "SLStack.h"
  28.  
  29. #include "variables.h"
  30.  
  31. class ostream;
  32.  
  33. // Must be multiple of 2.
  34. #define HASH_TABLE_SIZE 1024
  35. #define HASH_MASK (HASH_TABLE_SIZE - 1)
  36.  
  37. class tree;
  38. class tree_fvc;
  39. class tree_builtin;
  40. class tree_constant;
  41. class tree_function;
  42.  
  43. class symbol_def;
  44. class symbol_record;
  45. class symbol_record_info;
  46. class symbol_table;
  47.  
  48. // Variables or functions.
  49.  
  50. class symbol_def
  51. {
  52.   friend class symbol_record;
  53.   friend class symbol_record_info;
  54.  
  55. public:
  56.  
  57.   symbol_def (void);
  58.   symbol_def (tree_constant *t);
  59.   symbol_def (tree_builtin *t, unsigned fcn_type = 0);
  60.   symbol_def (tree_function *t, unsigned fcn_type = 0);
  61.  
  62.   ~symbol_def (void);
  63.  
  64.   int is_variable (void) const;
  65.   int is_function (void) const;
  66.   int is_text_function (void) const;
  67.   int is_mapper_function (void) const;
  68.   int is_user_variable (void) const;
  69.   int is_user_function (void) const;
  70.   int is_builtin_variable (void) const;
  71.   int is_builtin_function (void) const;
  72.  
  73.   void define (tree_constant *t);
  74.   void define (tree_builtin *t, unsigned fcn_type = 0);
  75.   void define (tree_function *t, unsigned fcn_type = 0);
  76.  
  77.   void protect (void);
  78.   void unprotect (void);
  79.   void make_eternal (void);
  80.  
  81.   tree_fvc *def (void) const;
  82.   char *help (void) const;
  83.   void document (const char *h);
  84.  
  85.   enum TYPE
  86.     {
  87.       UNKNOWN = 0,
  88.       USER_FUNCTION = 1,
  89.       USER_VARIABLE = 2,
  90.       BUILTIN_FUNCTION = 4,
  91.       TEXT_FUNCTION = 8,
  92.       MAPPER_FUNCTION = 16,
  93.       BUILTIN_VARIABLE = 32
  94.     };
  95.  
  96.   friend maybe_delete (symbol_def *def);
  97.  
  98. private:
  99.  
  100.   unsigned type : 6;
  101.   unsigned eternal : 1;
  102.   unsigned read_only : 1;
  103.  
  104.   char *help_string;
  105.   tree_fvc *definition;
  106.   symbol_def *next_elem;
  107.   int count;
  108.  
  109.   void init_state (void);
  110.  
  111.   symbol_def (const symbol_def& sd);
  112.   symbol_def& operator = (const symbol_def& sd);
  113. };
  114.  
  115. // Individual records in a symbol table.
  116.  
  117. class
  118. symbol_record
  119. {
  120.   friend class symbol_record_info;
  121.  
  122. public:
  123.   symbol_record (void);
  124.   symbol_record (const char *n, symbol_record *nxt = 0);
  125.  
  126.  ~symbol_record (void);
  127.  
  128.   char *name (void) const;
  129.   char *help (void) const; 
  130.   tree_fvc *def (void) const;
  131.  
  132.   void rename (const char *new_name);
  133.  
  134.   int is_function (void) const;
  135.   int is_user_function (void) const;
  136.   int is_text_function (void) const;
  137.   int is_mapper_function (void) const;
  138.   int is_builtin_function (void) const;
  139.   int is_variable (void) const;
  140.   int is_user_variable (void) const;
  141.   int is_builtin_variable (void) const;
  142.  
  143.   unsigned type (void) const;
  144.  
  145.   int is_defined (void) const;
  146.   int is_read_only (void) const;
  147.   int is_eternal (void) const;
  148.  
  149.   void protect (void);
  150.   void unprotect (void);
  151.   void make_eternal (void);
  152.  
  153.   void set_sv_function (sv_Function f);
  154.  
  155.   int define (tree_constant *t);
  156.   int define (tree_builtin *t, int text_fcn = 0);
  157.   int define (tree_function *t, int text_fcn = 0);
  158.   int define_as_fcn (tree_constant *t);
  159.   int define_builtin_var (tree_constant *t);
  160.  
  161.   void document (const char *h);
  162.  
  163.   int clear (void);
  164.  
  165.   void alias (symbol_record *s, int force = 0);
  166.  
  167.   void mark_as_formal_parameter (void);
  168.   int is_formal_parameter (void) const;
  169.  
  170.   void mark_as_linked_to_global (void);
  171.   int is_linked_to_global (void) const;
  172.  
  173.   symbol_record *next (void) const;
  174.  
  175.   void chain (symbol_record *s);
  176.  
  177.   void push_context (void);
  178.   void pop_context (void);
  179.  
  180. private:
  181.  
  182.   unsigned formal_param : 1;
  183.   unsigned linked_to_global : 1;
  184.  
  185.   char *nm;
  186.   sv_Function sv_fcn;
  187.   symbol_def *definition;
  188.   symbol_record *next_elem;
  189.  
  190. // This should maybe be one stack with a structure containing all the
  191. // items we need to save for recursive calls...
  192.   SLStack <symbol_def *> context;
  193.   SLStack <unsigned> global_link_context;
  194.  
  195.   void init_state (void);
  196.  
  197.   int read_only_error (void);
  198.  
  199.   void push_def (symbol_def *sd);
  200.   symbol_def *pop_def (void);
  201.  
  202.   symbol_record& operator = (const symbol_record& s);
  203. };
  204.  
  205. // A structure for handling verbose information about a symbol_record.
  206.  
  207. class
  208. symbol_record_info
  209. {
  210. public:
  211.  
  212.   symbol_record_info (void);
  213.   symbol_record_info (const symbol_record& s);
  214.  
  215.   symbol_record_info (const symbol_record_info& s);
  216.  
  217.   ~symbol_record_info (void);
  218.  
  219.   symbol_record_info& operator = (const symbol_record_info& s);
  220.  
  221.   int is_defined (void) const;
  222.   int is_read_only (void) const;
  223.   int is_eternal (void) const;
  224.   int hides_fcn (void) const;
  225.   int hides_builtin (void) const;
  226.   char *type_as_string (void) const;
  227.   int is_function (void) const;
  228.   int rows (void) const;
  229.   int columns (void) const;
  230.   char *name (void) const;
  231.  
  232.   enum HIDES
  233.     {
  234.       SR_INFO_NONE = 0,
  235.       SR_INFO_USER_FUNCTION = 1,
  236.       SR_INFO_BUILTIN_FUNCTION = 2
  237.     };
  238.  
  239.   enum CONST_TYPE
  240.     {
  241.       SR_INFO_UNKNOWN = 0,
  242.       SR_INFO_SCALAR = 1,
  243.       SR_INFO_COMPLEX_SCALAR = 2,
  244.       SR_INFO_MATRIX = 4,
  245.       SR_INFO_COMPLEX_MATRIX = 8,
  246.       SR_INFO_RANGE = 16,
  247.       SR_INFO_STRING = 32
  248.     };
  249.  
  250. private:
  251.  
  252.   void init_state (void);
  253.  
  254.   unsigned type : 4;
  255.   unsigned const_type : 6;
  256.   unsigned hides : 2;
  257.   unsigned eternal : 1;
  258.   unsigned read_only : 1;
  259.   int nr;
  260.   int nc;
  261.   char *nm;
  262.   
  263.   int initialized;
  264. };
  265.  
  266. // A symbol table.
  267.  
  268. #define SYMTAB_LOCAL_SCOPE 1
  269. #define SYMTAB_GLOBAL_SCOPE 2
  270.  
  271. #define SYMTAB_ALL_SCOPES (SYMTAB_LOCAL_SCOPE | SYMTAB_GLOBAL_SCOPE)
  272.  
  273. #define SYMTAB_ALL_TYPES (symbol_def::USER_FUNCTION \
  274.               | symbol_def::USER_VARIABLE \
  275.               | symbol_def::BUILTIN_FUNCTION \
  276.               | symbol_def::TEXT_FUNCTION \
  277.               | symbol_def::MAPPER_FUNCTION \
  278.               | symbol_def::BUILTIN_VARIABLE)
  279.  
  280. class
  281. symbol_table
  282. {
  283. public:
  284.  
  285.   symbol_table (void);
  286.  
  287.   symbol_record *lookup (const char *nm, int insert = 0, int warn = 0);
  288.  
  289.   void rename (const char *old_name, const char *new_name);
  290.  
  291.   void clear (int clear_user_functions = 1);
  292.   int clear (const char *nm, int clear_user_functions = 1);
  293.  
  294.   int size (void) const;
  295.  
  296.   symbol_record_info *long_list (int& count, char **pats = 0,
  297.                  int npats = 0, int sort = 0,
  298.                  unsigned type = SYMTAB_ALL_TYPES,
  299.                  unsigned scope = SYMTAB_ALL_SCOPES) const;
  300.  
  301.   char **list (int& count, char **pats = 0, int npats = 0,
  302.            int sort = 0, unsigned type = SYMTAB_ALL_TYPES,
  303.            unsigned scope = SYMTAB_ALL_SCOPES) const;
  304.  
  305.   symbol_record **glob (int& count, char *pat = "*",
  306.             unsigned type = SYMTAB_ALL_TYPES,
  307.             unsigned scope = SYMTAB_ALL_SCOPES) const;
  308.  
  309.   void push_context (void);
  310.   void pop_context (void);
  311.  
  312. private:
  313.  
  314.   unsigned int hash (const char *s);
  315.  
  316.   symbol_record table[HASH_TABLE_SIZE];
  317. };
  318.  
  319. #endif
  320.  
  321. /*
  322. ;;; Local Variables: ***
  323. ;;; mode: C++ ***
  324. ;;; page-delimiter: "^/\\*" ***
  325. ;;; End: ***
  326. */
  327.