compiler/PPCMethod.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Tue, 12 May 2015 01:24:03 +0100
changeset 459 4751c407bb40
parent 452 9f4558b3be66
child 460 87a3d30ab570
child 464 f6d77fee9811
permissions -rw-r--r--
Merged with PetitCompiler-JanKurs.20150510144201, PetitCompiler-Tests-JanKurs.20150510144201, PetitCompiler-Extras-Tests-JanKurs.20150510144201, PetitCompiler-Benchmarks-JanKurs.20150510144201 Name: PetitCompiler-JanKurs.20150510144201 Author: JanKurs Time: 10-05-2015, 04:42:29.192 PM UUID: 58a4786b-1182-4904-8b44-a13d3918f244 Name: PetitCompiler-Tests-JanKurs.20150510144201 Author: JanKurs Time: 10-05-2015, 04:32:12.870 PM UUID: 2a8fd41a-331b-4dcf-a7a3-752a50ce86e7 Name: PetitCompiler-Extras-Tests-JanKurs.20150510144201 Author: JanKurs Time: 10-05-2015, 04:59:25.308 PM UUID: ef43bd1a-be60-4e88-b749-8b635622c969 Name: PetitCompiler-Benchmarks-JanKurs.20150510144201 Author: JanKurs Time: 10-05-2015, 05:04:54.561 PM UUID: d8e764fd-016b-46e2-9fc1-17c38c18f0e5

"{ 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> $'
! !