home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / smalltk / src / basic.st next >
Text File  |  1991-10-12  |  8KB  |  415 lines

  1. *
  2. * Little Smalltalk, version 3
  3. * basic methods needed for execution, including
  4. *    object creation
  5. *    block creation, execution and return
  6. *
  7. Class Object
  8. Class Block Object context argCount argLoc bytePointer
  9. Class Boolean Object
  10. Class    True Boolean
  11. Class    False Boolean
  12. Class Class Object name instanceSize methods superClass variables
  13. Class Context Object linkLocation method arguments temporaries
  14. Class Integer Object
  15. Class Method Object text message bytecodes literals stackSize temporarySize class watch
  16. Class Smalltalk Object
  17. Class Switch Object const notdone
  18. Class Symbol Object
  19. Class UndefinedObject Object
  20. *
  21. Methods Block 'initialization'
  22.     checkArgumentCount: count
  23.         ^ (argCount = count)
  24.             ifTrue: [ true ]
  25.             ifFalse: [ smalltalk error:
  26.                 'wrong number of arguments passed to block'.
  27.                 false ]
  28. |
  29.     blockContext: ctx
  30.         context <- ctx
  31. |
  32.     value
  33.         ^ (self checkArgumentCount: 0)
  34.             ifTrue: [ context returnToBlock: bytePointer ]
  35. |
  36.     value: x
  37.         ^ (self checkArgumentCount:  1)
  38.             ifTrue: [ context at: argLoc put: x.
  39.                   context returnToBlock: bytePointer ]
  40. |
  41.     value: x value: y
  42.         ^ (self checkArgumentCount: 2)
  43.             ifTrue: [ context at: argLoc put: x.
  44.                   context at: argLoc + 1 put: y.
  45.                   context returnToBlock: bytePointer ]
  46. |
  47.     value: x value: y value: z
  48.         ^ (self checkArgumentCount:  3)
  49.             ifTrue: [ context at: argLoc put: x.
  50.                   context at: argLoc + 1 put: y.
  51.                   context at: argLoc + 2 put: z.
  52.                   context returnToBlock: bytePointer ]
  53. |
  54.     whileTrue: aBlock
  55.         ( self value ) ifTrue:
  56.             [ aBlock value. 
  57.                 self whileTrue: aBlock ]
  58. |
  59.     whileTrue
  60.         self whileTrue: []
  61. |
  62.     whileFalse: aBlock
  63.         [ self value not ] whileTrue: aBlock
  64. ]
  65. Methods Boolean 'all'
  66.     ifTrue: trueBlock
  67.         ^ self ifTrue: trueBlock ifFalse: []
  68. |
  69.     ifFalse: falseBlock
  70.         ^ self ifTrue: [] ifFalse: falseBlock
  71. |
  72.     ifFalse: falseBlock ifTrue: trueBlock
  73.         ^ self ifTrue: trueBlock
  74.             ifFalse: falseBlock
  75. |
  76.     and: aBlock
  77.         ^ self ifTrue: aBlock ifFalse: [ false ]
  78. |
  79.     or: aBlock
  80.         ^ self ifTrue: [ true ] ifFalse: aBlock
  81. ]
  82. Methods Class 'creation'
  83.     new        | newObject |
  84.         newObject <- self new: instanceSize.
  85.         ^ (self == Class)
  86.             ifTrue: [ newObject initialize ]
  87.             ifFalse: [ newObject new ]
  88. |
  89.     new: size    " hack out block the right size and class "
  90.         "create a new block, set its class"
  91.         ^ < 22 < 58 size > self >
  92. |
  93.     addSubClass: aSymbol instanceVariableNames: aString    | newClass |
  94.         newClass <- Class new; name: aSymbol; superClass: self;
  95.                 variables: 
  96.                   (aString words: [:x | x isAlphabetic ]).
  97.         aSymbol assign: newClass.
  98.         classes at: aSymbol put: newClass
  99. |
  100.     initialize
  101.         superClass <- Object.
  102.         instanceSize <- 0.
  103.         methods <- Dictionary new
  104. |
  105.     methods
  106.         ^ methods
  107. |
  108.     methodNamed: name
  109.         (methods includesKey: name)
  110.             ifTrue: [ ^ methods at: name ].
  111.         (superClass notNil)
  112.             ifTrue: [ ^ superClass methodNamed: name ].
  113.         ^ nil
  114. |
  115.     name
  116.         ^ name
  117. |
  118.     name: aString
  119.         name <- aString
  120. |
  121.     instanceSize
  122.         ^ instanceSize
  123. |
  124.     printString
  125.         ^ name asString
  126. |
  127.     respondsTo    | theSet |
  128.         theSet <- Dictionary new.
  129.         self upSuperclassChain: 
  130.             [:x | theSet addAll: x methods ].
  131.         ^ theSet
  132. |
  133.     subClasses
  134.         ^ classes inject: List new
  135.             into: [:x :y | (y superClass == self)
  136.                         ifTrue: [ x add: y]. x ]
  137. |
  138.     superClass
  139.         ^ superClass
  140. |
  141.     superClass: aClass
  142.         superClass <- aClass
  143. |
  144.     upSuperclassChain: aBlock
  145.         aBlock value: self.
  146.         (superClass notNil)
  147.             ifTrue: [ superClass upSuperclassChain: aBlock ]
  148. |
  149.     variables
  150.         ^ variables
  151. |
  152.     variables: nameArray
  153.         variables <- nameArray.
  154.         instanceSize <- superClass instanceSize + nameArray size
  155. |
  156.     watch: name    | m |
  157.         m <- self methodNamed: name.
  158.         (m notNil) 
  159.             ifTrue: [ ^ m watch: 
  160.                 [:a | ('executing ', name) print. a print] ]
  161.             ifFalse: [ ^ 'no such method' ]
  162. ]
  163. Methods Context 'all'
  164.     at: key put: value
  165.         temporaries at: key put: value
  166. |
  167.     method: m
  168.         method <- m
  169. |
  170.     arguments: a
  171.         arguments <- a
  172. |
  173.     temporaries: t
  174.         temporaries <- t
  175. |
  176.     returnToBlock: bytePtr
  177.         " change the location we will return to, to execute a block"
  178.         <28 self bytePtr>
  179. |
  180.     copy
  181.         ^ super copy temporaries: temporaries copy
  182. |
  183.     blockReturn
  184.         <18 self>
  185.             ifFalse: [ ^ smalltalk error: 
  186.                 'incorrect context for block return']
  187. ]
  188. Methods False 'all'
  189.     ifTrue: trueBlock ifFalse: falseBlock
  190.         ^ falseBlock value
  191. |
  192.     not
  193.         ^ true
  194. |
  195.     xor: aBoolean
  196.         ^ aBoolean
  197. |
  198.     printString
  199.         ^ 'false'
  200. ]
  201. Methods Method 'all'
  202.     compileWithClass: aClass
  203.         ^ <39 aClass text self>
  204. |
  205.     name
  206.         ^ message
  207. |
  208.     message: aSymbol
  209.         message <- aSymbol
  210. |
  211.     printString
  212.         ^ message asString
  213. |
  214.     signature
  215.         ^ class asString,' ', message asString
  216. |
  217.     text
  218.         ^ (text notNil)
  219.             ifTrue: [ text ]
  220.             ifFalse: [ 'text not saved']
  221. |
  222.     text: aString
  223.         text <- aString
  224. |
  225.     display
  226.         ('Method ', message) print.
  227.         'text' print.
  228.         text print.
  229.         'literals' print.
  230.         literals print.
  231.         'bytecodes' print.
  232.         bytecodes class print.
  233.         bytecodes do: [:x |
  234.             (x printString, ' ', (x quo: 16), ' ', (x rem: 16))
  235.                 print ]
  236. |
  237.     executeWith: arguments
  238.         ^ ( Context new ; method: self ; 
  239.             temporaries: ( Array new: temporarySize) ;
  240.             arguments: arguments )
  241.            returnToBlock: 1
  242. |
  243.     watch: aBlock
  244.         watch <- aBlock
  245. |
  246.     watchWith: arguments
  247.         " note that we are being watched "
  248.         text print.
  249.         watch value: arguments.
  250.         ^ self executeWith: arguments
  251. ]
  252. Methods Object 'all'
  253.     assign: name value: val
  254.         ^ name assign: val
  255. |
  256.     == aValue
  257.         ^ <21 self aValue>
  258. |
  259.     ~~ aValue
  260.         ^ (self == aValue) not
  261. |
  262.     = aValue
  263.         ^ self == aValue
  264. |
  265.     asString
  266.         ^ self printString
  267. |
  268.     basicAt: index
  269.         ^ <25 self index>
  270. |
  271.     basicAt: index put: value
  272.         ^ <31 self index value>
  273. |
  274.     basicSize
  275.         ^ <12 self>
  276. |
  277.     class
  278.         ^ <11 self>
  279. |
  280.     copy
  281.         ^ self shallowCopy
  282. |
  283.     deepCopy    | newObj |
  284.         newObj <- self class new.
  285.         (1 to: self basicSize) do: 
  286.             [:i | newObj basicAt: i put: (self basicAt: i) copy].
  287.         ^ newObj
  288. |
  289.     display
  290.         ('(Class ', self class, ') ' , self printString ) print
  291. |
  292.     hash
  293.         ^ <13 self>
  294. |
  295.     isMemberOf: aClass
  296.         ^ self class == aClass
  297. |
  298.     isNil
  299.         ^ false
  300. |
  301.     isKindOf: aClass
  302.         self class upSuperclassChain:
  303.             [:x | (x == aClass) ifTrue: [ ^ true ] ].
  304.         ^ false
  305. |
  306.     new
  307.         " default initialization protocol"
  308.         ^ self
  309. |
  310.     notNil
  311.         ^ true
  312. |
  313.     print
  314.         self printString print 
  315. |
  316.     printString
  317.         ^ self class printString
  318. |
  319.     respondsTo: message
  320.         self class upSuperclassChain: 
  321.             [:c | (c methodNamed: message) notNil
  322.                     ifTrue: [ ^ true ]].
  323.         ^ false
  324. |
  325.     shallowCopy    | newObj |
  326.         newObj <- self class new.
  327.         (1 to: self basicSize) do: 
  328.             [:i | newObj basicAt: i put: (self basicAt: i) ].
  329.         ^ newObj
  330. ]
  331. Methods Smalltalk 'all'
  332.     perform: message withArguments: args ifError: aBlock    
  333.             | receiver method |
  334.         receiver <- args at: 1 ifAbsent: [ ^ aBlock value ].
  335.         method <- receiver class methodNamed: message.
  336.         ^ method notNil 
  337.             ifTrue: [ method executeWith: args ]
  338.             ifFalse: aBlock
  339. |
  340.     perform: message withArguments: args
  341.         ^ self perform: message withArguments: args
  342.             ifError: [ self error: 'cant perform' ]
  343. |
  344.     watch
  345.         ^ <5>
  346. ]
  347. Methods True 'all'
  348.     ifTrue: trueBlock ifFalse: falseBlock
  349.         ^ trueBlock value
  350. |
  351.     not
  352.         ^ false
  353. |
  354.     xor: aBoolean
  355.         ^ aBoolean not
  356. |
  357.     printString
  358.         ^ 'true'
  359. ]
  360. Methods Switch 'all'
  361.     key: value
  362.         const <- value.
  363.         notdone <- true.
  364. |
  365.     ifMatch: key do: block
  366.         (notdone and: [ const = key ])
  367.             ifTrue: [ notdone <- false. block value ]
  368. |
  369.     else: block
  370.         notdone ifTrue: [ notdone <- false. block value ]
  371. ]
  372. Methods Symbol 'all'
  373.         apply: args
  374.         ^ self apply: args ifError: [ 'does not apply' ]
  375. |
  376.         apply: args ifError: aBlock
  377.         ^ smalltalk perform: self withArguments: args ifError: aBlock
  378. |
  379.     assign: value
  380.         <27 self value>. ^ value
  381. |
  382.     asString
  383.         " catenation makes string and copy automatically "
  384.         ^ <24 self ''>
  385. |
  386.     copy
  387.         ^ self
  388. |
  389.     printString
  390.         ^ '#' , self asString
  391. |
  392.     respondsTo
  393.         ^ classes inject: Set new
  394.             into: [:x :y | ((y methodNamed: self) notNil)
  395.                         ifTrue: [ x add: y]. x]
  396. |
  397.     value
  398.         ^ <87 self>
  399. ]
  400. Methods UndefinedObject 'all'
  401.     isNil
  402.         ^ true
  403. |
  404.     notNil
  405.         ^ false
  406. |
  407.     printString
  408.         ^ 'nil'
  409. ]
  410. Methods Object 'errors'
  411.     message: m notRecognizedWithArguments: a
  412.         ^ smalltalk error: 'not recognized ', (self class printString),
  413.             ' ', (m printString)
  414. ]
  415.