compiler/PPCMethod.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Tue, 12 May 2015 01:33:33 +0100
changeset 460 87a3d30ab570
parent 453 bd5107faf4d6
parent 459 4751c407bb40
child 465 f729f6cd3c76
permissions -rw-r--r--
Merge

"{ Package: 'stx:goodies/petitparser/compiler' }"

"{ NameSpace: Smalltalk }"

Object subclass:#PPCMethod
	instanceVariableNames:'buffer variables indentation id profile variableForReturn
		category'
	classVariableNames:''
	poolDictionaries:''
	category:'PetitCompiler-Core'
!


!PPCMethod class methodsFor:'as yet unclassified'!

new
    "return an initialized instance"

    ^ self basicNew initialize.
! !

!PPCMethod methodsFor:'accessing'!

body
    ^ buffer contents
!

bridge
    ^ PPCBridge on: self methodName.
!

category
    ^ category isNil 
        ifTrue: [ category := 'generated' ]
        ifFalse: [ category ]

    "Modified (format): / 12-05-2015 / 01:21:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

category: value
    category := value
!

code
    ^ self methodName, Character cr asString,  
        self variables, Character cr asString,
        self profilingBegin, Character cr asString,
        self body, Character cr asString
"               self profilingEnd"

    "Modified: / 23-04-2015 / 19:26:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

id: value
    id := value
!

methodName
    ^ id
!

profile
    ^ profile
!

profile: aBoolean
    profile := aBoolean 
! !

!PPCMethod methodsFor:'as yet unclassified'!

add: string
    self nl.
    indentation timesRepeat: [ buffer nextPut: Character tab  ].
    self addOnLine: string.
!

addOnLine: string
    buffer nextPutAll: string.
!

call
    ^ 'self ', self methodName, '.'.
!

nl
    ^ buffer nextPut: Character cr
!

profilingBegin
    self profile ifTrue: [ 
 				^ '  context methodInvoked: #', id, '.'	
    ].
    ^ ''
!

profilingEnd
    self profile ifTrue: [ 
 				^ '  context methodFinished: #', id, '.'	
    ].
    ^ ''
! !

!PPCMethod methodsFor:'code generation - variables'!

addVariable: name
    (variables includes: name) ifTrue:[ 
        self error:'Duplicate variable name, must rename'.
    ].
    variables add: name.

    "Modified: / 23-04-2015 / 12:29:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

allocateReturnVariable
    
	^ variableForReturn isNil ifTrue:[ 
		variableForReturn := self allocateTemporaryVariableNamed: 'retval'  
	] ifFalse:[ 
		variableForReturn
	].

    "Created: / 23-04-2015 / 18:03:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

allocateTemporaryVariableNamed:preferredName 
    "Allocate a new variable with (preferably) given name.
     Returns a real variable name that should be used."
    
    (variables includes:preferredName) ifFalse:[
	variables add:preferredName.
	^ preferredName
    ] ifTrue:[
	| name |

	name := preferredName , '_' , (variables size + 1) printString.
	variables add:name.
	^ name
    ].

    "Created: / 23-04-2015 / 17:37:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

returnVariable
    ^  variableForReturn

    "Created: / 23-04-2015 / 20:50:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

returnVariable: aString
    ^ variableForReturn := aString

    "Created: / 23-04-2015 / 18:23:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 23-04-2015 / 21:08:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

variables
    ^ '  | ', (variables inject: '' into: [ :s :e | s, ' ', e]), ' |'
! !

!PPCMethod methodsFor:'indentation'!

dedent
    indentation := indentation - 1
!

indent 
    indentation := indentation + 1
!

indentationLevel
    ^ indentation
!

indentationLevel: value
    indentation := value
! !

!PPCMethod methodsFor:'initialization'!

initialize
    buffer := WriteStream on: ''.
    indentation := 1.
    variables := OrderedCollection new.
! !

!PPCMethod methodsFor:'printing'!

printOn:aStream
    "append a printed representation if the receiver to the argument, aStream"

    super printOn:aStream.
    aStream nextPutAll:' id: '.
    id printOn:aStream.

    "Modified: / 23-04-2015 / 12:32:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!PPCMethod methodsFor:'testing'!

isInline
    ^ false
!

isMethod
    ^ true
! !

!PPCMethod class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
! !