home *** CD-ROM | disk | FTP | other *** search
- *
- * Little Smalltalk, version 2
- * Written by Tim Budd, Oregon State University, July 1987
- *
- * methods for the unix front end - single process version
- *
- * (override previous declaration, adding new instance variable)
- Declare Smalltalk Object errorRecoveryBlock
- Declare File Object name pointer
- * (better override global variable as well )
- Instance Smalltalk smalltalk
- * make global variables for standard files
- Instance File stdin
- Instance File stdout
- Instance File stderr
- *
- Class File
- asString | text line |
- text <- ''.
- [line <- self getString. line notNil]
- whileTrue: [ text <- text , line ].
- ^ text
- |
- name: string
- name <- string
- |
- name
- ^ name
- |
- scratchFile
- name <- 'junk.tmp'
- |
- open: mode
- pointer <- <120 name mode>.
- pointer isNil
- ifTrue: [ smalltalk error: 'open failed']
- |
- close
- (pointer notNil)
- ifTrue: [<121 pointer>].
- pointer <- nil.
- |
- delete
- ('delete ', name) unixCommand
- |
- fileIn | line |
- [ line <- self getString. line notNil ]
- whileTrue: [ line <- line words:
- [:x | x isAlphabetic ] .
- Switch new; key: (line at: 1);
- ifMatch: 'Class' do: [self fileInClass: line ] ;
- ifMatch: 'Method' do:
- [ self fileInMethod: line ] ;
- else: [ ^ smalltalk error:
- 'invalid format for fileIn'] ]
- |
- fileInClass: commandLine | name |
- name <- (commandLine at: 2
- ifAbsent: [^ smalltalk error:
- 'missing class name in Class directive'])
- asSymbol.
- globalNames at: name put: ( Class new;
- name: name;
- superClass: (globalNames at: (
- commandLine at: 3
- ifAbsent: [ ^ smalltalk error:
- 'missing superclass name'])
- asSymbol
- ifAbsent: [ ^ smalltalk error:
- 'unknown class']);
- variables: (commandLine copyFrom: 4 to:
- commandLine size ) )
- |
- fileInMethod: commandLine
- (commandLine size ~= 2)
- ifTrue: [ ^ smalltalk error:
- 'invalid Method command line '].
- (globalNames at: (commandLine at: 2) asSymbol
- ifAbsent: [ ^ smalltalk error:
- 'unknown class in Method header'])
- fileInMethod: self
- |
- getString
- ^ (pointer notNil)
- ifTrue: [<125 pointer>]
- |
- getPrompt: aString
- stdout printNoReturn: aString.
- ^ self getString
- |
- inquire: aString | response |
- response <- self getPrompt: aString.
- response isNil
- ifTrue: [ ^ false ].
- ^ 'yY' includes: (response at: 1 ifAbsent: [])
- |
- print: aString
- (pointer notNil)
- ifTrue: [<129 pointer aString>]
- ifFalse: [smalltalk error: 'file not open']
- |
- printNoReturn: aString
- (pointer notNil)
- ifTrue: [<128 pointer aString>]
- ifFalse: [smalltalk error: 'file not open']
- |
- readUntil: conditionBlock doing: actionBlock | line |
- [ line <- self getString. line notNil]
- whileTrue: [ (conditionBlock value: line)
- ifTrue: [ ^ line ].
- actionBlock value: line ].
- ^ nil
- |
- saveImage
- (pointer notNil)
- ifTrue: [<127 pointer>]
- ifFalse: [smalltalk error: 'file not open']
- ]
- Class Method
- executeWith: arguments
- ^ ( Context new ; method: self ;
- temporaries: ( Array new: temporarySize) ;
- arguments: arguments )
- executeFrom: 0 creator: nil
- ]
- Class Class
- addMethod
- self doEdit: ''
- |
- addSubClass | name |
- name <- (stdin getPrompt: 'Class Name? ') asSymbol.
- globalNames at: name put:
- ( Class new; name: name ; superClass: self ;
- readInstanceVariables; readMethods )
- |
- addMethodText: text | theMethod |
- theMethod <- Method new; text: text.
- (theMethod compileWithClass: self)
- ifTrue: [ methods at: theMethod name put: theMethod.
- smalltalk flushMessageCache.
- ^ true ].
- ^ false
- |
- doEdit: startingText | text |
- text <- startingText.
- [ text <- text edit.
- (self addMethodText: text)
- ifTrue: [ false ]
- ifFalse: [ stdin inquire: 'edit again (yn) ? ' ]
- ] whileTrue
- |
- display
- ('Class name: ', name asString) print.
- (superClass notNil)
- ifTrue: [ ('Superclass: ', superClass ) print ].
- 'Instance Variables:' print.
- variables isNil
- ifTrue: [ 'no instance variables ' print ]
- ifFalse: [ variables display ].
- 'Subclasses: ' print.
- self subClasses display
- |
- editMethod: name
- self doEdit: ( methods at: name
- ifAbsent: [ 'no such method ' print. ^ nil ] ) text
- |
- fileInMethod: file | text line |
- text <- ''.
- line <- file readUntil: [:x | '|[' includes:
- (x at: 1 ifAbsent: [] ) ]
- doing: [:x | text <- text , x].
- self addMethodText: text.
- ^ line
- |
- fileOut: file
- file printNoReturn: 'Class ', name asString.
- file printNoReturn: ' ', superClass name asString.
- variables do: [:x | file printNoReturn: ' ', x ].
- file print: ''.
- methods do: [:x | self fileOutMethod: x name to: file ]
- |
- fileOutMethod: method to: file
- file print: 'Method ', name asString.
- file print: (methods at: method
- ifAbsent: [^ smalltalk error:
- 'no such method' ]) text.
- file print: '|'
- |
- readInstanceVariables
- self variables:
- ((stdin getPrompt: 'Instance Variables? ')
- words: [:x | x isAlphabetic ])
- |
- readMethods
- [ stdin inquire: 'Add a method (yn) ? ' ]
- whileTrue: [ self addMethod ]
- |
- viewMethod: name
- (methods at: name
- ifAbsent: [ 'no such method ' print. ^ nil ]) text print
- ]
- Class Smalltalk
- error: aString
- stderr print: 'Error: ',aString.
- errorRecoveryBlock value
- |
- openFiles
- stdin name: 'stdin'.
- stdin open: 'r'.
- stdout name: 'stdout'.
- stdout open: 'w'.
- stderr name: 'stderr'.
- stderr open: 'w'.
- |
- commandLoop | string |
- self openFiles.
- [ string <- stdin getPrompt: '> '. string notNil ]
- whileTrue: [ (string size strictlyPositive)
- ifTrue: [ self doIt: string ] ]
- |
- doIt: aString | method |
- errorRecoveryBlock <- [ ^ nil ].
- method <- Method new.
- method text: ( 'proceed ', aString ).
- (method compileWithClass: Object)
- ifTrue: [ method executeWith: #( 1 ) ]
- |
- saveImage | name |
- name <- stdin getPrompt: 'type image name: '.
- File new;
- name: name;
- open: 'w';
- saveImage;
- close.
- ('image ', name, ' created') print
- ]
- Class String
- edit | file text |
- file <- File new;
- scratchFile;
- open: 'w';
- print: self;
- close.
- (editor, ' ', file name) unixCommand.
- file open: 'r'.
- text <- file asString.
- file close; delete.
- ^ text
- |
- print
- ^ stdout print: self
- |
- unixCommand
- ^ <88 self>
- ]
-