compiler/PPCMethod.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 24 Jul 2015 15:06:54 +0100
changeset 502 1e45d3c96ec5
parent 464 f6d77fee9811
child 503 ff58cd9f1f3c
child 515 b5316ef15274
permissions -rw-r--r--
Updated to PetitCompiler-JanVrany.135, PetitCompiler-Tests-JanKurs.93, PetitCompiler-Extras-Tests-JanVrany.16, PetitCompiler-Benchmarks-JanKurs.12 Name: PetitCompiler-JanVrany.135 Author: JanVrany Time: 22-07-2015, 06:53:29.127 PM UUID: 890178b5-275d-46af-a2ad-1738998f07cb Ancestors: PetitCompiler-JanVrany.134 Name: PetitCompiler-Tests-JanKurs.93 Author: JanKurs Time: 20-07-2015, 11:30:10.283 PM UUID: 6473e671-ad70-42ca-b6c3-654b78edc531 Ancestors: PetitCompiler-Tests-JanKurs.92 Name: PetitCompiler-Extras-Tests-JanVrany.16 Author: JanVrany Time: 22-07-2015, 05:18:22.387 PM UUID: 8f6f9129-dbba-49b1-9402-038470742f98 Ancestors: PetitCompiler-Extras-Tests-JanKurs.15 Name: PetitCompiler-Benchmarks-JanKurs.12 Author: JanKurs Time: 06-07-2015, 02:10:06.901 PM UUID: cb24f1ac-46a4-494d-9780-64576f0f0dba Ancestors: PetitCompiler-Benchmarks-JanKurs.11, PetitCompiler-Benchmarks-JanVrany.e29bd90f388e.20150619081300

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

"{ NameSpace: Smalltalk }"

Object subclass:#PPCMethod
	instanceVariableNames:'buffer id variableForReturn category profile'
	classVariableNames:''
	poolDictionaries:''
	category:'PetitCompiler-Compiler-Codegen'
!


!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
    ^ String streamContents: [ :s |
        s nextPutAll: self methodName; cr.
        buffer codeOn: s.    
    ]

    "Modified: / 01-06-2015 / 21:24:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

id: value
    id := value
!

indentationLevel
    ^ buffer indentationLevel

    "Created: / 01-06-2015 / 21:38:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

indentationLevel: anInteger
    buffer indentationLevel: anInteger

    "Created: / 01-06-2015 / 21:38:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

methodName
    ^ id
!

profile
    ^ profile
!

profile: aBoolean
    profile := aBoolean 
! !

!PPCMethod methodsFor:'as yet unclassified'!

add: string
    buffer add: string

    "Modified: / 01-06-2015 / 21:09:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

addOnLine: string
    buffer addOnLine: string

    "Modified: / 01-06-2015 / 21:09:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

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

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

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

!PPCMethod methodsFor:'code generation'!

code: aStringOrBlockOrRBParseNode
    buffer 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>"
!

codeBlock: contents
    | outerBlock innerBlock |

    outerBlock := buffer.
    innerBlock := PPCCodeBlock new.
    innerBlock indentationLevel: outerBlock indentationLevel + 1.  
    [ 
        outerBlock addOnLine:'['.
        buffer := innerBlock.
        self code: contents.
    ] ensure:[
        outerBlock 
            code: (String streamContents:[:s | innerBlock codeOn: s]);
            add:']'.
        buffer := 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>"
! !

!PPCMethod methodsFor:'code generation - indenting'!

dedent
    buffer dedent

    "Created: / 01-06-2015 / 21:32:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

indent
    buffer indent

    "Created: / 01-06-2015 / 21:32:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

nl

    buffer nl

    "Created: / 01-06-2015 / 21:52:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!PPCMethod methodsFor:'code generation - variables'!

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

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

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

allocateTemporaryVariableNamed:preferredName 
    "Allocate a new variable with (preferably) given name.
     Returns a real variable name that should be used."

    ^ buffer 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>"
!

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

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

!PPCMethod methodsFor:'initialization'!

initialize
    buffer := PPCCodeBlock new.

    "Modified: / 01-06-2015 / 21:33:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

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