compiler/tests/extras/PPCResources.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 515 b5316ef15274
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/tests/extras' }"

"{ NameSpace: Smalltalk }"

TestResource subclass:#PPCResources
	instanceVariableNames:'cache'
	classVariableNames:'javaCache'
	poolDictionaries:''
	category:'PetitCompiler-Extras-Tests-Support'
!

!PPCResources methodsFor:'expressions'!

expressionOfSize: size
    | stream |
    stream := WriteStream on: (String new: size * 5).
    self expressionOfSize: size stream: stream.
    ^ stream contents
!

expressionOfSize: size stream: stream
    | index rand |
    index := 0.
    rand := Random new.
    
    [index < size] whileTrue: [ 
 		(rand next < 0.1) ifTrue: [  
            | subSize |
            subSize := rand nextInt: (size - index - 1) + 1.
            stream nextPutAll: ' ('.
            self expressionOfSize: subSize stream: stream.
            stream nextPutAll: ') '.
            index := index + subSize.
        ] ifFalse: [ 
            stream nextPutAll: (rand nextInt: 10) asString.
            index := index + 1.
        ].
    
        (index < size) ifTrue: [ 
 			(rand next < 0.5) 
                ifTrue: [  stream nextPutAll: ' + ' ] 
                ifFalse: [ stream nextPutAll: ' * ' ]
        ]
    ]
!

expressionSourcesBig
    | sources |
    
    cache at: #expressionSourcesBig ifAbsentPut: [ 
        sources := OrderedCollection new.
        
        2000 timesRepeat: [ 
            sources add: (self expressionOfSize: 200).
        ].
        sources	
    ].

    ^ cache at: #expressionSourcesBig
    
!

expressionSourcesMedium
    | sources |
    
    cache at: #expressionSourcesMedium ifAbsentPut: [ 
        sources := OrderedCollection new.
        
        1000 timesRepeat: [ 
            sources add: (self expressionOfSize: 100).
        ].
        sources	
    ].

    ^ cache at: #expressionSourcesMedium
    
! !

!PPCResources methodsFor:'initialization'!

initialize
    super initialize.
    cache := IdentityDictionary new
! !

!PPCResources methodsFor:'java'!

javaInDirectory: directory
    | files |
    files := self readDirectory: directory.
    files := self files: files withExtension: 'java'.
    
    ^ files collect: [ :f | (FileStream fileNamed: f) contents ]
!

javaLangMath
    ^ (FileStream fileNamed: '../java-src/java/lang/Math.java') contents asString

    "Modified: / 10-05-2015 / 14:11:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

javaSourcesBig
    ^ self javaInDirectory: '../java-src/java/util'.
    "^ self workingJavaInDirectory: '../java-src/java/util'"
!

javaUtilTimer
    ^ (FileStream fileNamed: '../java-src/java/util/Timer.java') contents asString

    "Modified: / 10-05-2015 / 14:11:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

workingJavaInDirectory: directory
    | sources parser |
    "return only such a files, that can be parsed by PPJavaSyntax"

    javaCache ifNil: [ javaCache := Dictionary new ].
    
    ^ javaCache at: directory ifAbsentPut: [ 
        sources := self javaInDirectory: directory.
        parser := PPJavaSyntax new.
    
        sources select: [ :source | ([parser parse: source ] on: Error do: [ PPFailure new ]) isPetitFailure not ]	
    ]
! !

!PPCResources methodsFor:'private utilities'!

files: files withExtension: extension
    ^ files select: [ :f | f extension = extension ] 
!

readDirectory: directory
    | file |

    ( (Smalltalk respondsTo: #isSmalltalkX) and:[ Smalltalk isSmalltalkX ] ) ifTrue:[ 
        file := directory asFilename.
        file exists ifFalse:[  
            self error: 'Directory does not exist'.
        ].
        ^ file recursiveDirectoryContentsAsFilenames select:[:each | each isRegularFile ]
    ] ifFalse:[ 
        "Assuming Pharo..."

        file := directory asFileReference.
        file exists ifFalse: [ 
            self error: 'Directory does not exist'.
        ].
        ^ file allFiles
    ]

    "Modified: / 10-05-2015 / 07:54:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!PPCResources methodsFor:'smalltalk'!

smalltalkClassMethods
    ^ self smalltalkInDirectory: '../smalltalk-src/Class/'
    
!

smalltalkInDirectory: directory
    | files |
    files := self readDirectory: directory.
    files := self files: files withExtension: 'st'.
    
    ^ files collect: [ :f | (FileStream fileNamed: f) contents ]
!

smalltalkObjectMethods
    ^ self smalltalkInDirectory: '../smalltalk-src/Object/'
    
!

smalltalkSourcesBig
    ^ self smalltalkInDirectory: '../smalltalk-src/'
!

smalltalkSourcesSmall
    ^ (self smalltalkInDirectory: '../smalltalk-src/') copyFrom: 1 to: 1000.
! !