home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sa104os2.zip / SATHR104.ZIP / SATHER / COMPILER / IDENT.SA < prev    next >
Text File  |  1994-12-22  |  5KB  |  128 lines

  1. -- Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  --
  2. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  3. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  4. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  5. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  6. --------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
  7.  
  8. -- ident.sa: Identifier related classes.
  9. -------------------------------------------------------------------
  10. -- IDENT: Representation of identifiers in the Sather compiler.
  11. -- IDENT_TBL: A table mapping strings to identifiers.
  12. -------------------------------------------------------------------
  13. value class IDENT < $HASH, $IS_EQ{IDENT} is
  14.    -- Representation of identifiers in the Sather compiler.
  15.    -- A value class with one attribute: a pointer to a string containing
  16.    -- the identifier. The string objects are uniquely associated with
  17.    -- the string, so SI_IDENT equality can be used to test for string
  18.    -- equality. 
  19.      
  20.    attr str:STR;        -- The string represented by self.
  21.  
  22.    create(s:STR):SAME is
  23.       -- Create a new identifier for the string `s'.
  24.       r:SAME; return r.str(s) end;
  25.  
  26.    is_eq(i:SAME):BOOL is
  27.       -- Test whether self is the same identifier as `i'.
  28.       return SYS::ob_eq(str,i.str) end;
  29.  
  30.    is_neq(i:SAME):BOOL is
  31.       -- Test whether self is the same identifier as `i'.
  32.       return ~SYS::ob_eq(str,i.str) end;
  33.  
  34.    is_iter:BOOL is
  35.       -- True if self is the name of an iter (ends with a "!").
  36.       return ~void(str) and str[str.size-1]='!' end;
  37.  
  38.    str_in(s:FSTR):FSTR is
  39.       -- Append the string represented by self to `s'.
  40.       return s + str end;
  41.  
  42.    hash:INT is
  43.       -- MBK, SYS::id(str) by itself not that good a hash. 
  44.       x::=SYS::id(str);
  45.       return (x*1664525) + 1013904223; -- Numerical Recipes in C, p 284
  46.    end;
  47.  
  48.  
  49. end; -- value class IDENT
  50.  
  51. -------------------------------------------------------------------
  52. class IDENT_TBL is
  53.    -- A table mapping strings to identifiers.
  54.    -- get_query(s:STR):IDENT to get identifier if present.
  55.    -- insert(i:IDENT):SAME to add a new identifier.
  56.  
  57.    include FQSET{STR,IDENT};
  58.    
  59.    query_test(s:STR, i:IDENT):BOOL is
  60.       -- True if `i' represents `s'.
  61.       return s=i.str end;
  62.  
  63.    query_hash(s:STR):INT is
  64.       -- Hash value for the string `s'.
  65.       return s.hash end;
  66.    
  67.    elt_hash(i:IDENT):INT is
  68.       -- Hash value for the identifier `i'.
  69.       return i.str.hash end;
  70.    
  71. end; -- class IDENT_TBL
  72.  
  73. -------------------------------------------------------------------
  74. class IDENT_BUILTIN is
  75.    -- Cache for quick access to the built-in identifers.
  76.  
  77.    attr dollar_OB_ident, ARR_ident, INT_ident, INTI_ident, FLT_ident, 
  78.       FLTD_ident, FLTDX_ident, FLTX_ident, plus_ident, minus_ident, 
  79.       times_ident, div_ident, pow_ident, mod_ident, is_lt_ident, 
  80.       is_leq_ident, is_gt_ident, is_geq_ident, negate_ident, not_ident, 
  81.       AVAL_ident, AREF_ident, aget_ident, aset_ident, call_ident,
  82.       call_bang_ident, create_ident, ARRAY_ident, self_ident, 
  83.       invariant_ident, asize_ident, main_ident, is_eq_ident, 
  84.       is_neq_ident:IDENT;
  85.    
  86.    create (p:PROG):SAME is
  87.       -- A new cache for the builtin identifiers.
  88.       r::=new;
  89.       r.dollar_OB_ident:=p.ident_for("$OB");
  90.       r.ARR_ident:=p.ident_for("ARR");
  91.       r.INT_ident:=p.ident_for("INT");
  92.       r.INTI_ident:=p.ident_for("INTI");
  93.       r.FLT_ident:=p.ident_for("FLT");
  94.       r.FLTD_ident:=p.ident_for("FLTD");
  95.       r.FLTDX_ident:=p.ident_for("FLTDX");
  96.       r.FLTX_ident:=p.ident_for("FLTX");
  97.       r.plus_ident:=p.ident_for("plus");
  98.       r.minus_ident:=p.ident_for("minus");
  99.       r.times_ident:=p.ident_for("times");
  100.       r.div_ident:=p.ident_for("div");
  101.       r.pow_ident:=p.ident_for("pow");
  102.       r.mod_ident:=p.ident_for("mod");
  103.       r.is_lt_ident:=p.ident_for("is_lt");
  104.       r.is_leq_ident:=p.ident_for("is_leq");
  105.       r.is_gt_ident:=p.ident_for("is_gt");
  106.       r.is_geq_ident:=p.ident_for("is_geq");
  107.       r.negate_ident:=p.ident_for("negate");
  108.       r.not_ident:=p.ident_for("not");
  109.       r.AVAL_ident:=p.ident_for("AVAL");
  110.       r.AREF_ident:=p.ident_for("AREF");
  111.       r.aget_ident:=p.ident_for("aget");      
  112.       r.aset_ident:=p.ident_for("aset"); 
  113.       r.call_ident:=p.ident_for("call"); 
  114.       r.call_bang_ident:=p.ident_for("call!");       
  115.       r.create_ident:=p.ident_for("create");       
  116.       r.ARRAY_ident:=p.ident_for("ARRAY");             
  117.       r.self_ident:=p.ident_for("self");                   
  118.       r.invariant_ident:=p.ident_for("invariant");                         
  119.       r.asize_ident:=p.ident_for("asize");         
  120.       r.main_ident:=p.ident_for("main");               
  121.       r.is_eq_ident:=p.ident_for("is_eq");               
  122.       r.is_neq_ident:=p.ident_for("is_neq");                     
  123.       return r end;
  124.    
  125. end; -- class IDENT_BUILTIN
  126.  
  127. -------------------------------------------------------------------
  128.