compiler/PPCMethod.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 15 Jun 2015 19:13:49 +0100
changeset 488 19a9c25960ef
parent 481 34ee0d3c72e7
child 503 ff58cd9f1f3c
permissions -rw-r--r--
Avoid creation of intermediate collection for mapped action nodes.

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

"{ NameSpace: Smalltalk }"

Object subclass:#PPCMethod
	instanceVariableNames:'buffer id variableForReturn category'
	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
! !

!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.  
    [ 
        buffer addOnLine:'['; nl; codeIndent.
        buffer := innerBlock.
        self code: contents.
    ] ensure:[
        buffer := outerBlock.
        buffer 
            code: (String streamContents:[:s | innerBlock codeOn: s]);
            nl;
            codeIndent.

        buffer addOnLine:']'.
    ]

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

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