1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
159
|
3 |
All Rights Reserved
|
1
|
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:'
|
5
|
21 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
159
|
22 |
All Rights Reserved
|
95
|
23 |
|
159
|
24 |
$Header: /cvs/stx/stx/libbasic/Symbol.st,v 1.12 1994-10-10 00:28:50 claus Exp $
|
1
|
25 |
'!
|
|
26 |
|
77
|
27 |
!Symbol class methodsFor:'documentation'!
|
1
|
28 |
|
88
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
159
|
32 |
All Rights Reserved
|
88
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
159
|
45 |
$Header: /cvs/stx/stx/libbasic/Symbol.st,v 1.12 1994-10-10 00:28:50 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
77
|
49 |
documentation
|
|
50 |
"
|
|
51 |
Symbols represent unique strings - every symbol with same printString
|
|
52 |
exists exactly once in the system; Symbols are used for selectors, global
|
|
53 |
variable-keys etc.
|
|
54 |
"
|
|
55 |
! !
|
1
|
56 |
|
77
|
57 |
!Symbol class methodsFor:'instance creation'!
|
1
|
58 |
|
62
|
59 |
basicNew:size
|
|
60 |
"redefined to return a string instead of a symbol -
|
|
61 |
this allows all copy methods inherited from String to
|
|
62 |
return strings containing the symbols characters.
|
|
63 |
Real symbols are only created with intern: or asSymbol."
|
|
64 |
|
|
65 |
^ String new:size
|
|
66 |
!
|
|
67 |
|
|
68 |
new:size
|
|
69 |
"redefined to return a string instead of a symbol -
|
|
70 |
this allows all copy methods inherited from String to
|
|
71 |
return strings containing the symbols characters.
|
|
72 |
Real symbols are only created with intern: or asSymbol."
|
|
73 |
|
|
74 |
^ String new:size
|
|
75 |
!
|
|
76 |
|
1
|
77 |
intern:aString
|
|
78 |
"return a unique symbol with printname taken from the String-argument"
|
|
79 |
|
62
|
80 |
%{ /* NOCONTEXT */
|
|
81 |
if (__isSymbol(aString)) {
|
159
|
82 |
RETURN (aString);
|
62
|
83 |
}
|
|
84 |
if (__isString(aString)) {
|
159
|
85 |
RETURN ( _MKSYMBOL(_stringVal(aString), (OBJ *)0, __context) );
|
1
|
86 |
}
|
|
87 |
%}
|
|
88 |
.
|
|
89 |
^ self mustBeString
|
|
90 |
!
|
|
91 |
|
77
|
92 |
fromString:aString
|
|
93 |
"same as intern: for Symbol, but may be used to create interned instances
|
|
94 |
of subclasses.
|
|
95 |
Notice: this fails, if you try to intern an instance of a subclass, AND
|
|
96 |
a symbol with the same name already exists. In this case, the original
|
|
97 |
symbol is returned. To use it for enum-type symbols, make certain, that the
|
|
98 |
names are unique (for example by including the classes name as a prefix-string)."
|
|
99 |
|
|
100 |
|newSym len|
|
|
101 |
|
|
102 |
aString knownAsSymbol ifTrue:[
|
159
|
103 |
^ aString asSymbol
|
77
|
104 |
].
|
|
105 |
|
|
106 |
"
|
|
107 |
create a new uninterned instance first
|
|
108 |
"
|
|
109 |
len := aString size.
|
|
110 |
newSym := super basicNew:len.
|
|
111 |
newSym replaceFrom:1 to:len with:aString.
|
|
112 |
|
|
113 |
"now, intern it"
|
|
114 |
%{
|
|
115 |
extern OBJ _INTERNSYMBOL();
|
|
116 |
|
|
117 |
newSym = _INTERNSYMBOL(newSym, (OBJ *)0, __context);
|
|
118 |
%}.
|
|
119 |
^ newSym
|
|
120 |
!
|
|
121 |
|
1
|
122 |
internCharacter:aCharacter
|
|
123 |
"return a unique symbol with printname taken from the Character-argument"
|
|
124 |
|
|
125 |
^ self intern:(aCharacter asString)
|
|
126 |
! !
|
|
127 |
|
|
128 |
!Symbol class methodsFor:'queries'!
|
|
129 |
|
|
130 |
hasInterned:aString ifTrue:trueBlock
|
|
131 |
"for ST-80 compatibility - if the argument, aString is known
|
|
132 |
as Symbol, evaluate the block with the corresponding symbol
|
|
133 |
as argument and return true; otherwise return false"
|
|
134 |
|
|
135 |
aString knownAsSymbol ifTrue:[
|
159
|
136 |
trueBlock value:(aString asSymbol).
|
|
137 |
^ true
|
1
|
138 |
].
|
|
139 |
^ false
|
77
|
140 |
!
|
|
141 |
|
|
142 |
hasInterned:aString
|
|
143 |
"return true, if the argument, aString is known as Symbol;
|
|
144 |
false otherwise"
|
|
145 |
|
|
146 |
^ aString knownAsSymbol
|
1
|
147 |
! !
|
|
148 |
|
|
149 |
!Symbol methodsFor:'accessing'!
|
|
150 |
|
|
151 |
basicAt:index put:something
|
77
|
152 |
"report an error if interned - interned symbols may not be changed."
|
1
|
153 |
|
77
|
154 |
self knownAsSymbol ifTrue:[
|
159
|
155 |
self error:'symbols may not be changed'.
|
|
156 |
^ something
|
77
|
157 |
].
|
|
158 |
"
|
|
159 |
uninterned - allow change
|
|
160 |
"
|
|
161 |
^ super basicAt:index put:something
|
1
|
162 |
! !
|
|
163 |
|
|
164 |
!Symbol methodsFor:'copying'!
|
|
165 |
|
159
|
166 |
copy
|
|
167 |
"return a copy of myself
|
|
168 |
- reimplemented here since symbols are unique"
|
|
169 |
|
|
170 |
^ self
|
|
171 |
!
|
|
172 |
|
|
173 |
shallowCopy
|
13
|
174 |
"return a copy of myself
|
|
175 |
- reimplemented here since symbols are unique"
|
|
176 |
|
|
177 |
^ self
|
|
178 |
!
|
|
179 |
|
1
|
180 |
deepCopy
|
|
181 |
"return a copy of myself
|
|
182 |
- reimplemented here since symbols are unique"
|
|
183 |
|
|
184 |
^ self
|
13
|
185 |
!
|
|
186 |
|
159
|
187 |
deepCopyUsing:aDictionary
|
|
188 |
"return a copy of myself
|
|
189 |
- reimplemented here since symbols are unique"
|
|
190 |
|
|
191 |
^ self
|
|
192 |
!
|
|
193 |
|
13
|
194 |
simpleDeepCopy
|
|
195 |
"return a copy of myself
|
|
196 |
- reimplemented here since symbols are unique"
|
|
197 |
|
|
198 |
^ self
|
1
|
199 |
! !
|
|
200 |
|
|
201 |
!Symbol methodsFor:'converting'!
|
|
202 |
|
|
203 |
asString
|
|
204 |
"return a string with printname taken from mine"
|
|
205 |
|
|
206 |
^ self printString
|
|
207 |
!
|
|
208 |
|
|
209 |
asSymbol
|
|
210 |
"I am a symbol - just return myself"
|
|
211 |
|
|
212 |
^ self
|
|
213 |
! !
|
|
214 |
|
|
215 |
!Symbol methodsFor:'misc'!
|
|
216 |
|
|
217 |
species
|
|
218 |
^ String
|
|
219 |
! !
|
|
220 |
|
77
|
221 |
!Symbol methodsFor:'system primitives'!
|
|
222 |
|
|
223 |
become:anotherObject
|
|
224 |
"make all references to the receiver become references to anotherObject
|
95
|
225 |
and vice-versa. For symbols, some special action is required, to
|
|
226 |
correctly handle a become of the global dictionaries.
|
77
|
227 |
Anyway: this is very dangerous - mysterous side-effects are to be
|
95
|
228 |
expected.
|
|
229 |
|
|
230 |
Notice: because of the danger here, this method may report an error
|
159
|
231 |
in future versions"
|
77
|
232 |
|
|
233 |
(Smalltalk includesKey:self) ifTrue:[
|
159
|
234 |
super become:anotherObject.
|
77
|
235 |
%{
|
159
|
236 |
__rehashSystemDictionaries();
|
77
|
237 |
%}.
|
|
238 |
] ifFalse:[
|
159
|
239 |
super become:anotherObject
|
77
|
240 |
]
|
|
241 |
!
|
|
242 |
|
|
243 |
becomeNil
|
|
244 |
"make all references to the receiver become nil - effectively getting
|
95
|
245 |
rid of the receiver. For symbols, this is not allowed, if the receiver
|
77
|
246 |
is used as a key in some SytemDictionary.
|
95
|
247 |
This can be a very dangerous operation - be warned.
|
|
248 |
|
|
249 |
Notice: because of the danger here, this method may report an error
|
159
|
250 |
in future versions"
|
77
|
251 |
|
|
252 |
(Smalltalk includesKey:self) ifTrue:[
|
159
|
253 |
self primitiveFailed
|
77
|
254 |
] ifFalse:[
|
159
|
255 |
super becomeNil
|
77
|
256 |
]
|
|
257 |
! !
|
|
258 |
|
2
|
259 |
!Symbol class methodsFor:'binary storage'!
|
|
260 |
|
|
261 |
binaryDefinitionFrom: stream manager: manager
|
|
262 |
^ self intern: (super binaryDefinitionFrom: stream manager: manager)
|
|
263 |
! !
|
|
264 |
|
1
|
265 |
!Symbol methodsFor:'printing & storing'!
|
|
266 |
|
|
267 |
printString
|
|
268 |
%{
|
|
269 |
RETURN ( _MKSTRING(_stringVal(self) COMMA_CON) );
|
|
270 |
%}
|
|
271 |
!
|
|
272 |
|
|
273 |
printOn:aStream
|
|
274 |
"aStream nextPut:$#."
|
68
|
275 |
aStream nextPutAll:self "(self printString)"
|
1
|
276 |
!
|
|
277 |
|
|
278 |
displayString
|
10
|
279 |
"return a string for displaying the receiver"
|
|
280 |
|
1
|
281 |
^ self storeString
|
|
282 |
!
|
|
283 |
|
|
284 |
storeString
|
10
|
285 |
"return a String for storing the receiver"
|
1
|
286 |
|
10
|
287 |
(self at:1) isLetter ifTrue:[
|
159
|
288 |
2 to:self size do:[:index |
|
|
289 |
(self at:index) isLetterOrDigit ifFalse:[
|
|
290 |
^ '#''' , self , ''''
|
|
291 |
].
|
|
292 |
].
|
|
293 |
^ '#' , self
|
10
|
294 |
].
|
2
|
295 |
^ '#''' , self , ''''
|
1
|
296 |
!
|
|
297 |
|
|
298 |
storeOn:aStream
|
10
|
299 |
"store myself on a stream"
|
|
300 |
|
|
301 |
aStream nextPutAll:(self storeString)
|
1
|
302 |
! !
|