PetitCommonMark/software/petitcompiler/PPCMethod.class.st

281 lines
6.7 KiB
Smalltalk

Class {
#name : 'PPCMethod',
#superclass : 'Object',
#instVars : [
'selector',
'source',
'category',
'variableForReturn'
],
#category : 'PetitCompiler-Compiler-Codegen'
}
{ #category : 'as yet unclassified' }
PPCMethod class >> new [
"return an initialized instance"
^ self basicNew initialize.
]
{ #category : 'as yet unclassified' }
PPCMethod >> add: string [
source add: string
"Modified: / 01-06-2015 / 21:09:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'as yet unclassified' }
PPCMethod >> addOnLine: string [
source addOnLine: string
"Modified: / 01-06-2015 / 21:09:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'code generation - variables' }
PPCMethod >> allocateReturnVariable [
^ variableForReturn isNil ifTrue:[
variableForReturn := self allocateTemporaryVariableNamed: 'retval'
] ifFalse:[
variableForReturn
].
"Created: / 23-04-2015 / 18:03:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'code generation - variables' }
PPCMethod >> allocateReturnVariableNamed: name [
"Allocate temporary variable used for storing a parser's return value (the parsed object)"
variableForReturn notNil ifTrue:[
self error: 'Return variable already allocated!'.
^ self.
].
variableForReturn := self allocateTemporaryVariableNamed: name.
^ variableForReturn
"Created: / 15-06-2015 / 17:52:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'code generation - variables' }
PPCMethod >> allocateTemporaryVariableNamed:preferredName [
"Allocate a new variable with (preferably) given name.
Returns a real variable name that should be used."
^ source allocateTemporaryVariableNamed: preferredName
"Created: / 23-04-2015 / 17:37:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
"Modified: / 01-06-2015 / 21:04:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'accessing' }
PPCMethod >> bridge [
^ PPCBridge on: self methodName.
]
{ #category : 'as yet unclassified' }
PPCMethod >> call [
^ 'self ', self methodName, '.'.
]
{ #category : 'as yet unclassified' }
PPCMethod >> callOn: receiver [
^ receiver, ' ', self methodName.
]
{ #category : 'accessing' }
PPCMethod >> category [
^ category isNil
ifTrue: [ category := 'generated' ]
ifFalse: [ category ]
]
{ #category : 'accessing' }
PPCMethod >> category: value [
category := value
]
{ #category : 'accessing' }
PPCMethod >> code [
^ String streamContents: [ :s |
s nextPutAll: self methodName; cr.
source codeOn: s.
]
"Modified: / 01-06-2015 / 21:24:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'code generation' }
PPCMethod >> code: aStringOrBlockOrRBParseNode [
source code: aStringOrBlockOrRBParseNode.
"Created: / 01-06-2015 / 22:31:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
"Modified (format): / 01-06-2015 / 23:50:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'code generation' }
PPCMethod >> codeBlock: contents [
| outerBlock innerBlock |
outerBlock := source.
innerBlock := PPCCodeBlock new.
innerBlock indentationLevel: outerBlock indentationLevel + 1.
[
outerBlock addOnLine: '['.
source := innerBlock.
self codeOnLine: contents.
] ensure:[
outerBlock
codeOnLine: (String streamContents:[:s | innerBlock sourceOn:s]);
add: ']'.
source := outerBlock.
]
"Created: / 01-06-2015 / 22:33:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
"Modified: / 03-06-2015 / 06:11:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'code generation' }
PPCMethod >> codeOnLine: aStringOrBlockOrRBParseNode [
source codeOnLine: aStringOrBlockOrRBParseNode.
"Created: / 01-06-2015 / 22:31:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
"Modified (format): / 01-06-2015 / 23:50:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'code generation - indenting' }
PPCMethod >> dedent [
source dedent
"Created: / 01-06-2015 / 21:32:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'accessing' }
PPCMethod >> id: value [
selector := value
]
{ #category : 'code generation - indenting' }
PPCMethod >> indent [
source indent
"Created: / 01-06-2015 / 21:32:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'accessing' }
PPCMethod >> indentationLevel [
^ source indentationLevel
"Created: / 01-06-2015 / 21:38:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'accessing' }
PPCMethod >> indentationLevel: anInteger [
source indentationLevel: anInteger
"Created: / 01-06-2015 / 21:38:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'initialization' }
PPCMethod >> initialize [
source := PPCCodeBlock new.
"Modified: / 01-06-2015 / 21:33:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'testing' }
PPCMethod >> isInline [
^ false
]
{ #category : 'testing' }
PPCMethod >> isMethod [
^ true
]
{ #category : 'accessing' }
PPCMethod >> methodName [
^ selector
]
{ #category : 'code generation - indenting' }
PPCMethod >> nl [
source nl
"Created: / 01-06-2015 / 21:52:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'printing' }
PPCMethod >> printOn:aStream [
"append a printed representation if the receiver to the argument, aStream"
super printOn:aStream.
aStream nextPutAll:' id: '.
selector printOn:aStream.
"Modified: / 23-04-2015 / 12:32:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'as yet unclassified' }
PPCMethod >> profilingBegin [
self profile ifTrue: [
^ ' context methodInvoked: #', selector, '.'
].
^ ''
]
{ #category : 'as yet unclassified' }
PPCMethod >> profilingEnd [
self profile ifTrue: [
^ ' context methodFinished: #', selector, '.'
].
^ ''
]
{ #category : 'code generation - variables' }
PPCMethod >> returnVariable [
^ variableForReturn
"Created: / 23-04-2015 / 20:50:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
"Modified (format): / 15-06-2015 / 18:12:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'code generation - variables' }
PPCMethod >> returnVariable: aString [
(variableForReturn notNil and:[variableForReturn ~= aString]) ifTrue:[
self error: 'Return variable already allocated with different name (''', variableForReturn , ''' vs ''', aString,''')'.
].
variableForReturn := aString
"Created: / 23-04-2015 / 18:23:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
"Modified: / 15-06-2015 / 18:14:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'accessing' }
PPCMethod >> selector [
^ selector
]
{ #category : 'accessing' }
PPCMethod >> source [
^ source isString ifTrue:[
source
] ifFalse:[
String streamContents: [ :s |
s nextPutAll: self methodName; cr.
source sourceOn:s.
]
].
"Created: / 24-07-2015 / 19:46:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]
{ #category : 'accessing' }
PPCMethod >> source: aString [
source := aString
"Created: / 24-07-2015 / 19:48:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
]