2 Copyright (c) 2007 Luca Bruno
4 This file is part of Smalltalk YX.
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the 'Software'), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
32 "A list of direct subclasses of the receiver"
37 "Answer my superclass"
42 "Returns the Dictionary containing the methods of the class"
47 "Answer a copy of the names of the instance variables"
48 ^instanceVariableNames copy
52 "My string representation is my name itself"
59 <primitive: 'Behavior_new'>
64 <primitive: 'Behavior_new'>
69 <primitive: 'Behavior_newColon'>
74 self shouldNotImplement
78 ^self subclass: classSymbol instanceVariableNames: '' classVariableNames: ''
81 subclass: classSymbol instanceVariableNames: instanceVariables
82 ^self subclass: classSymbol instanceVariableNames: instanceVariables classVariableNames: ''
85 subclass: classSymbol instanceVariableNames: instanceVariableNames classVariableNames: classVariableNames
87 classSymbol first isUppercase
88 ifFalse: [ self error: 'Class names must be capitalized' ].
90 class := Smalltalk at: classSymbol ifAbsent: [
91 metaclass := Metaclass new.
92 class := metaclass initializeSubclassOf: self class named: nil.
93 Smalltalk at: classSymbol put: class ].
96 initializeSubclassOf: self named: classSymbol;
97 setInstanceVariableNames: instanceVariableNames;
98 setClassVariableNames: classVariableNames;
104 method := aString compileFor: self.
106 ifNil: [ methodDictionary := Dictionary new: 10 ].
107 methodDictionary at: method selector put: method
111 instanceSize := superclass instanceSize + instanceVariableNames size.
112 self allSubclassesDo: [ :ea | ea updateInstanceSize ]
115 parseVariableNames: aString
118 ifFalse: [ self error: 'Expected a string containing variable names' ].
119 lexer := CompilerLexer text: aString.
120 coll := OrderedCollection new.
121 [ (token := lexer nextToken) notNil ]
123 token class = CompilerIdentifierToken
124 ifFalse: [ self error: 'Instance variables must be valid identifiers' ].
125 token first isLowercase
126 ifFalse: [ self error: 'Instance variables must not be capitalized' ].
127 coll add: token asSymbol ].
131 setInstanceVariableNames: aString
132 instanceVariableNames := self parseVariableNames: aString.
133 self updateInstanceSize
141 initializeSubclassOf: aClass named: classSymbol
142 instanceSize := aClass instanceSize.
143 superclass := aClass.
144 instanceVariableNames := #().
146 methodDictionary := IdentityDictionary new.
148 aClass addSubclass: self
151 doesUnderstand: selector
152 "Answer true if the receiver understand the given message selector, else false"
157 (class methodDictionary includesKey: selector)
159 class := class superclass ].
164 subclasses := subclasses copyWith: aClass
168 "Answer a list of all subclasses of the receiver"
170 ret := OrderedCollection new.
171 self allSubclassesDo: [ :ea |
176 allSubclassesDo: aBlock
177 "Call aBlock recursing trough each subclass of this class"
178 self subclasses do: [ :ea |
180 ea allSubclassesDo: aBlock ]
183 allSuperclassesDo: aBlock
184 "Call aBlock for each superclass of this class"
187 [ (class := class superclass) notNil ]
188 whileTrue: [ aBlock value: class ]
197 setClassVariableNames: aString
199 array := self parseVariableNames: aString.
200 classVariables := IdentityDictionary new: array size.
202 classVariables at: ea put: nil ]