compiler/PPCMethod.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Thu, 21 May 2015 14:12:22 +0100
changeset 464 f6d77fee9811
parent 459 4751c407bb40
child 465 f729f6cd3c76
child 502 1e45d3c96ec5
permissions -rw-r--r--
Updated to PetitCompiler-JanKurs.118, PetitCompiler-Tests-JanKurs.46, PetitCompiler-Extras-Tests-JanKurs.11, and PetitCompiler-Benchmarks-JanKurs.11 Name: PetitCompiler-JanKurs.118 Author: JanKurs Time: 13-05-2015, 03:59:01.292 PM UUID: 4a8ccd94-3131-4cc7-9098-528f8e5ea0b5 Name: PetitCompiler-Tests-JanKurs.46 Author: JanKurs Time: 04-05-2015, 04:25:06.162 PM UUID: 9f4cf8b7-876e-4a13-9579-b833f016db66 Name: PetitCompiler-Extras-Tests-JanKurs.11 Author: JanKurs Time: 13-05-2015, 04:27:27.940 PM UUID: e9f30c31-fbd0-4e96-ad2a-868f88d20ea8 Name: PetitCompiler-Benchmarks-JanKurs.11 Author: JanKurs Time: 13-05-2015, 02:21:49.932 PM UUID: 6a23fd1e-a86f-46db-8221-cc41b778d32c

"{ 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 ]
                          
!

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