compiler/PPCConfiguration.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Thu, 21 May 2015 14:12:22 +0100
changeset 464 f6d77fee9811
parent 452 9f4558b3be66
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:#PPCConfiguration
	instanceVariableNames:'arguments ir history'
	classVariableNames:''
	poolDictionaries:''
	category:'PetitCompiler-Core'
!

!PPCConfiguration class methodsFor:'as yet unclassified'!

default
    ^ self universal
!

new
    ^ self basicNew
        initialize;
        yourself
!

tokenizing
    ^ PPCTokenizingConfiguration new
!

universal
    ^ PPCUniversalConfiguration new
! !

!PPCConfiguration methodsFor:'accessing'!

arguments
 	arguments isNil ifTrue: [ arguments := self defaultArguments ].
	^ arguments
!

arguments: args
    arguments := args
!

defaultArguments
 	^ PPCArguments default
!

input: whatever
    ir := whatever.
    self remember: #input.
!

ir
    ^ ir
!

ir: whatever
    ir := whatever
! !

!PPCConfiguration methodsFor:'caching'!

cacheFirstSet
    "Creates a PPCNodes from a PPParser"
    | firstSets |
    firstSets := ir firstSets.
    ir allNodesDo: [ :node |
        node firstSet: (firstSets at: node)
    ]
!

cacheFirstSetWithProductions
    "Creates a PPCNodes from a PPParser"
    | firstSets |
    firstSets := ir firstSetsSuchThat: [:e | e name isNil not ].
    ir allNodesDo: [ :node |
        node firstSetWithProductions: (firstSets at: node)
    ]
!

cacheFirstSetWithTokens
    "Creates a PPCNodes from a PPParser"
    | firstSets |
    firstSets := ir firstSetsSuchThat: [:e | e isTerminal or: [ e isTokenNode ] ].
    ir allNodesDo: [ :node |
        node firstSetWithTokens: (firstSets at: node)
    ]
!

cacheFollowSet
    "Creates a PPCNodes from a PPParser"
    | followSets |
    followSets := ir followSets.
    ir allNodesDo: [ :node |
        node followSet: (followSets at: node)
    ]
!

cacheFollowSetWithTokens
    "Creates a PPCNodes from a PPParser"
    | followSets |
    followSets := ir firstSetsSuchThat: [:e | e isTerminal or: [ e isTokenNode ] ].
    ir allNodesDo: [ :node |
        node followSetWithTokens: (followSets at: node)
    ]
! !

!PPCConfiguration methodsFor:'compiling'!

compile: whatever
    self input: whatever.
    self invokePhases.
    ^ ir
!

invokePhases
    self subclassResponsibility
! !

!PPCConfiguration methodsFor:'debugging'!

copy: somethingTransformable
    ^ somethingTransformable transform: [ :e | e copy ]
!

remember: key
    self arguments debug ifTrue: [ 
        history add: key -> (self copy: ir).
    ]
! !

!PPCConfiguration methodsFor:'hooks'!

codeCompilerOn: args
    ^ PPCCompiler on: args
!

codeGeneratorVisitorOn: compiler
    ^ arguments codeGenerator on: compiler
! !

!PPCConfiguration methodsFor:'initialization'!

initialize
    history := OrderedCollection new
! !

!PPCConfiguration methodsFor:'phases'!

cacheFirstFollow
    arguments cacheFirstFollow ifFalse: [ ^ self ] .
    
    self cacheFirstSet.
    self cacheFollowSet.
    self cacheFirstSetWithTokens.
    self cacheFollowSetWithTokens.
!

check
    ir checkTree 
!

createRecognizingComponents
    arguments recognizingComponents ifFalse: [ ^ self ] .
    
    ir :=  PPCRecognizerComponentDetector new
        arguments: arguments;
        visit: ir.
    self remember: #recognizingComponents
!

createTokens
    arguments detectTokens ifFalse: [ ^ self ] .
    
    ir :=  PPCTokenDetector new
        arguments: arguments;
        visit: ir.
    self remember: #createTokens
!

generate
    | compiler rootMethod compiledParser |
    arguments generate ifFalse: [ ^ self ].
    
    compiler := self codeCompilerOn: arguments.
    
    rootMethod := (self codeGeneratorVisitorOn: compiler)
        arguments: arguments;
        visit: ir.
    
    compiler compileParser.
    compiler compiledParser startSymbol: rootMethod methodName.
    compiledParser := compiler compiledParser new.
    
    ir := compiledParser.
!

inline
    arguments inline ifFalse: [ ^ self ].
    
    ir := PPCInliningVisitor new
        arguments: arguments;
        visit: ir.
    self remember: #inline.
!

merge
    "Merge equivalent nodes under one object with single identity"
    arguments merge ifFalse: [ ^ self ].
    
    ir :=  PPCMergingVisitor new
        arguments: arguments;
        visit: ir.
    self remember: #merge
!

specialize
    arguments specialize ifFalse: [ ^ self ].

    " 
        Invokes a visitor that creates specialized nodes
        for some patterns of PPCNodes, 
        
        e.g. $a astar can be represented by PPCCharacterStarNode
    "
    ir :=  (PPCSpecializingVisitor new
        arguments: arguments;
        visit: ir).
    self remember: #specialize
!

toPPCIr
    "Creates a PPCNodes from a PPParser"
    ir := ir asCompilerTree.
    self remember: #ppcNodes
! !