Symbol.st
author claus
Wed, 13 Oct 1993 01:19:00 +0100
changeset 3 24d81bf47225
parent 2 6526dde5f3ac
child 5 67342904af11
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1988-93 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

String subclass:#Symbol
       instanceVariableNames:''
       classVariableNames:''
       poolDictionaries:''
       category:'Collections-Text'
!

Symbol comment:'

COPYRIGHT (c) 1988-93 by Claus Gittinger
              All Rights Reserved

Symbols represent unique strings - every symbol with same printString
exists exactly once in the system; Symbols are used for selectors, global
variable-keys etc.

$Header: /cvs/stx/stx/libbasic/Symbol.st,v 1.3 1993-10-13 00:18:36 claus Exp $
'!

!Symbol class methodsFor:'instance creation'!

basicNew:size
    "catch instance creation - symbols are not created with new"

    self error:'symbols may not be created with new:'
! 

intern:aString
    "return a unique symbol with printname taken from the String-argument"

%{
    if (_isString(aString)) {
        RETURN ( _MKSYMBOL(_stringVal(aString), (OBJ *)0, __context) );
    }
%}
.
    ^ self mustBeString
!

internCharacter:aCharacter
    "return a unique symbol with printname taken from the Character-argument"

    ^ self intern:(aCharacter asString)
! !

!Symbol class methodsFor:'queries'!

hasInterned:aString ifTrue:trueBlock
    "for ST-80 compatibility - if the argument, aString is known
     as Symbol, evaluate the block with the corresponding symbol
     as argument and return true; otherwise return false"

    aString knownAsSymbol ifTrue:[
        trueBlock value:(aString asSymbol).
        ^ true
    ].
    ^ false
! !

!Symbol methodsFor:'accessing'!

basicAt:index put:something
    "report an error - symbols may not be changed"

    self error:'symbols may not be changed'
!

at:index put:something
    "report an error - symbols may not be changed"

    self error:'symbols may not be changed'
! !

!Symbol methodsFor:'copying'!

deepCopy
    "return a copy of myself
     - reimplemented here since symbols are unique"

     ^ self
! !

!Symbol methodsFor:'converting'!

asString
    "return a string with printname taken from mine"

    ^ self printString
!

asSymbol
    "I am a symbol - just return myself"

    ^ self
! !

!Symbol methodsFor:'misc'!

species
    ^ String
!

nArgsIfSelector
    "if symbol is used as a selector, how many arguments would it take"

    |binopChars|

    (self size > 2) ifFalse:[
        binopChars := '|&-+=*/\<>~@,'.
        (self size == 1) ifTrue:[
            ((binopChars occurrencesOf:(self at:1)) == 0) ifTrue:[^ 0].
            ^ 1
        ].
        ((binopChars occurrencesOf:(self at:1)) == 0) ifFalse:[
            ((binopChars occurrencesOf:(self at:2)) == 0) ifFalse:[^ 1]
        ]
    ].
    ^ self occurrencesOf:$:
! !

!Symbol class methodsFor:'binary storage'!

binaryDefinitionFrom: stream manager: manager
    ^ self intern: (super binaryDefinitionFrom: stream manager: manager)
! !

!Symbol methodsFor:'printing & storing'!

printString
%{
    RETURN ( _MKSTRING(_stringVal(self) COMMA_CON) );
%}
!

printOn:aStream
    "aStream nextPut:$#."
    aStream nextPutAll:(self printString)
!
 
displayString
    ^ self storeString
!

storeString
    "return a String for storing myself"

    ^ '#''' , self , ''''
!

storeOn:aStream
    aStream nextPutAll:'#'''.
    aStream nextPutAll:(self printString).
    aStream nextPutAll:''''.
! !