Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding class OCCompilerDynamicASTPlugin (and its test class), allowi… #1920

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/OpalCompiler-Core/OCCompilerASTPlugin.class.st
@@ -1,5 +1,5 @@
"
I am an abtract superclass for compiler plugings.
I am an abtract superclass for compiler plugins.

The compiler consists of multiple passes:

Expand Down
52 changes: 52 additions & 0 deletions src/OpalCompiler-Core/OCCompilerDynamicASTPlugin.class.st
@@ -0,0 +1,52 @@
"
Look at the OCCompilerASTPlugin class comment for information about compiler plugins.

I allow the dynamic creation of compiler plugins.

Instanciate me using """"newFromTransformBlock: aBlock andPriority: aPriority"""" to get a compiler plugin you can add to an OpalCompiler instance with OpalCompiler>>addPlugin:.
- aBlock must be a block of the form [ :ast | do something to ast ] that returns the modified ast.
- aPriority is the priority the created plugin should have

Check out my test class (OCCompilerDynamicASTPluginTest) for a usage example.
"
Class {
#name : #OCCompilerDynamicASTPlugin,
#superclass : #Object,
#instVars : [
'priority',
'transformBlock'
],
#category : #'OpalCompiler-Core-Translator'
}

{ #category : #'instance creation' }
OCCompilerDynamicASTPlugin class >> newFromTransformBlock: aBlock andPriority: aPriority [
^ self new
transformBlock: aBlock;
priority: aPriority.
]

{ #category : #accessing }
OCCompilerDynamicASTPlugin >> priority [
^ priority
]

{ #category : #accessing }
OCCompilerDynamicASTPlugin >> priority: anObject [
priority := anObject
]

{ #category : #accessing }
OCCompilerDynamicASTPlugin >> transform: ast [
^ transformBlock value: ast.
]

{ #category : #accessing }
OCCompilerDynamicASTPlugin >> transformBlock [
^ transformBlock
]

{ #category : #accessing }
OCCompilerDynamicASTPlugin >> transformBlock: anObject [
transformBlock := anObject
]
19 changes: 19 additions & 0 deletions src/OpalCompiler-Tests/OCCompilerDynamicASTPluginTest.class.st
@@ -0,0 +1,19 @@
Class {
#name : #OCCompilerDynamicASTPluginTest,
#superclass : #TestCase,
#category : #'OpalCompiler-Tests-Plugins'
}

{ #category : #tests }
OCCompilerDynamicASTPluginTest >> testCreatingAndUsingDynamicCompilerPlugin [
| result |
result :=
Object compiler
addPlugin:
(OCCompilerDynamicASTPlugin
newFromTransformBlock: [ :ast | (RBParseTreeRewriter replaceLiteral: 42 with: 'meaning of life') executeTree: ast. ast. ]
andPriority: 0
);
evaluate: '42'.
self assert: result equals: 'meaning of life'.
]