Symbol.st
changeset 1 a27a279701f8
child 2 6526dde5f3ac
equal deleted inserted replaced
0:aa2498ef6470 1:a27a279701f8
       
     1 "
       
     2  COPYRIGHT (c) 1988-93 by Claus Gittinger
       
     3               All Rights Reserved
       
     4 
       
     5  This software is furnished under a license and may be used
       
     6  only in accordance with the terms of that license and with the
       
     7  inclusion of the above copyright notice.   This software may not
       
     8  be provided or otherwise made available to, or used by, any
       
     9  other person.  No title to or ownership of the software is
       
    10  hereby transferred.
       
    11 "
       
    12 
       
    13 String subclass:#Symbol
       
    14        instanceVariableNames:''
       
    15        classVariableNames:''
       
    16        poolDictionaries:''
       
    17        category:'Collections-Text'
       
    18 !
       
    19 
       
    20 Symbol comment:'
       
    21 
       
    22 COPYRIGHT (c) 1988-93 by Claus Gittinger
       
    23               All Rights Reserved
       
    24 
       
    25 Symbols represent unique strings - every symbol with same printString
       
    26 exists exactly once in the system; Symbols are used for selectors, global
       
    27 variable-keys etc.
       
    28 
       
    29 %W% %E%
       
    30 '!
       
    31 
       
    32 !Symbol class methodsFor:'instance creation'!
       
    33 
       
    34 basicNew:size
       
    35     "catch instance creation - symbols are not created with new"
       
    36 
       
    37     self error:'symbols may not be created with new:'
       
    38 ! 
       
    39 
       
    40 intern:aString
       
    41     "return a unique symbol with printname taken from the String-argument"
       
    42 
       
    43 %{
       
    44     if (_isString(aString)) {
       
    45         RETURN ( _MKSYMBOL(_stringVal(aString), (OBJ *)0, __context) );
       
    46     }
       
    47 %}
       
    48 .
       
    49     ^ self mustBeString
       
    50 !
       
    51 
       
    52 internCharacter:aCharacter
       
    53     "return a unique symbol with printname taken from the Character-argument"
       
    54 
       
    55     ^ self intern:(aCharacter asString)
       
    56 ! !
       
    57 
       
    58 !Symbol class methodsFor:'queries'!
       
    59 
       
    60 hasInterned:aString ifTrue:trueBlock
       
    61     "for ST-80 compatibility - if the argument, aString is known
       
    62      as Symbol, evaluate the block with the corresponding symbol
       
    63      as argument and return true; otherwise return false"
       
    64 
       
    65     aString knownAsSymbol ifTrue:[
       
    66         trueBlock value:(aString asSymbol).
       
    67         ^ true
       
    68     ].
       
    69     ^ false
       
    70 ! !
       
    71 
       
    72 !Symbol methodsFor:'accessing'!
       
    73 
       
    74 basicAt:index put:something
       
    75     "report an error - symbols may not be changed"
       
    76 
       
    77     self error:'symbols may not be changed'
       
    78 !
       
    79 
       
    80 at:index put:something
       
    81     "report an error - symbols may not be changed"
       
    82 
       
    83     self error:'symbols may not be changed'
       
    84 ! !
       
    85 
       
    86 !Symbol methodsFor:'copying'!
       
    87 
       
    88 deepCopy
       
    89     "return a copy of myself
       
    90      - reimplemented here since symbols are unique"
       
    91 
       
    92      ^ self
       
    93 ! !
       
    94 
       
    95 !Symbol methodsFor:'converting'!
       
    96 
       
    97 asString
       
    98     "return a string with printname taken from mine"
       
    99 
       
   100     ^ self printString
       
   101 !
       
   102 
       
   103 asSymbol
       
   104     "I am a symbol - just return myself"
       
   105 
       
   106     ^ self
       
   107 ! !
       
   108 
       
   109 !Symbol methodsFor:'misc'!
       
   110 
       
   111 species
       
   112     ^ String
       
   113 !
       
   114 
       
   115 nArgsIfSelector
       
   116     "if symbol is used as a selector, how many arguments would it take"
       
   117 
       
   118     |binopChars|
       
   119 
       
   120     (self size > 2) ifFalse:[
       
   121         binopChars := '|&-+=*/\<>~@,'.
       
   122         (self size == 1) ifTrue:[
       
   123             ((binopChars occurrencesOf:(self at:1)) == 0) ifTrue:[^ 0].
       
   124             ^ 1
       
   125         ].
       
   126         ((binopChars occurrencesOf:(self at:1)) == 0) ifFalse:[
       
   127             ((binopChars occurrencesOf:(self at:2)) == 0) ifFalse:[^ 1]
       
   128         ]
       
   129     ].
       
   130     ^ self occurrencesOf:$:
       
   131 ! !
       
   132 
       
   133 !Symbol methodsFor:'printing & storing'!
       
   134 
       
   135 printString
       
   136 %{
       
   137     RETURN ( _MKSTRING(_stringVal(self) COMMA_CON) );
       
   138 %}
       
   139 !
       
   140 
       
   141 printOn:aStream
       
   142     "aStream nextPut:$#."
       
   143     aStream nextPutAll:(self printString)
       
   144 !
       
   145  
       
   146 displayString
       
   147     ^ self storeString
       
   148 !
       
   149 
       
   150 storeString
       
   151     "return a String for storing myself"
       
   152 
       
   153     ^ $# asString , self
       
   154 !
       
   155 
       
   156 storeOn:aStream
       
   157     aStream nextPut:$#.
       
   158     aStream nextPutAll:(self printString)
       
   159 ! !