-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from moosetechnology/local-resolver
Added package FAST-Core-Tools
- Loading branch information
Showing
8 changed files
with
270 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
Extension { #name : #FASTEntity } | ||
|
||
{ #category : #'*FAST-Core-Tools' } | ||
FASTEntity >> addLocalUse: aFASTNode [ | ||
(self | ||
attributeAt: #localUses | ||
ifAbsent: [ Error signal: 'missing #localUses attribute in ' , self asString ]) | ||
add: aFASTNode | ||
] | ||
|
||
{ #category : #'*FAST-Core-Tools' } | ||
FASTEntity >> localDeclaration [ | ||
^self | ||
attributeAt: #localDeclaration | ||
ifAbsent: [ Error signal: 'missing #localDeclaration attribute in ' , self asString ] | ||
] | ||
|
||
{ #category : #'*FAST-Core-Tools' } | ||
FASTEntity >> localDeclaration: aDeclarationNode [ | ||
self attributeAt: #localDeclaration put: aDeclarationNode | ||
] | ||
|
||
{ #category : #'*FAST-Core-Tools' } | ||
FASTEntity >> localUses [ | ||
^self | ||
attributeAt: #localUses | ||
ifAbsent: [ Error signal: 'missing #localUses attribute in ' , self asString ] | ||
] | ||
|
||
{ #category : #'*FAST-Core-Tools' } | ||
FASTEntity >> localUses: aCollection [ | ||
self attributeAt: #localUses put: aCollection | ||
] | ||
|
||
{ #category : #'*FAST-Core-Tools' } | ||
FASTEntity >> resetLocalUses [ | ||
self attributeAt: #localUses put: OrderedCollection new | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
" | ||
A FASTVisitor that links local variables (structuralEntities) used to their local declaration. | ||
This class is intended to be subclassed for each FAST, language dependant, meta-model. | ||
- In the declaration nodes, `#localUses` lists all referencing nodes | ||
- In the referencing nodes, `#localDeclaration` lists the corresponding declaration node or a FASTNonLocalDeclaration if the declaration node could not be found | ||
Invoking: | ||
``` | ||
FASTLocalResolverVisitor new on: aFASTJavaMethodEntity | ||
``` | ||
" | ||
Class { | ||
#name : #FASTLocalResolverVisitor, | ||
#superclass : #FASTCoreVisitor, | ||
#instVars : [ | ||
'scopes' | ||
], | ||
#category : #'FAST-Core-Tools-Resolver' | ||
} | ||
|
||
{ #category : #accessing } | ||
FASTLocalResolverVisitor >> currentScope [ | ||
|
||
^ scopes top | ||
] | ||
|
||
{ #category : #private } | ||
FASTLocalResolverVisitor >> findDeclaration: aName [ | ||
^self currentScope | ||
at: aName | ||
ifAbsent: [ |scope decl| | ||
scope := self popScope. | ||
self hasScopes | ||
ifTrue: [ decl := self findDeclaration: aName ]. | ||
self pushScope: scope. | ||
decl | ||
] | ||
] | ||
|
||
{ #category : #accessing } | ||
FASTLocalResolverVisitor >> hasScopes [ | ||
^scopes isNotEmpty | ||
] | ||
|
||
{ #category : #initialization } | ||
FASTLocalResolverVisitor >> initialize [ | ||
super initialize. | ||
|
||
scopes := Stack new | ||
] | ||
|
||
{ #category : #private } | ||
FASTLocalResolverVisitor >> localDeclaration: declarationNode for: referingNode [ | ||
referingNode localDeclaration: declarationNode. | ||
declarationNode addLocalUse: referingNode | ||
] | ||
|
||
{ #category : #private } | ||
FASTLocalResolverVisitor >> nonLocalDeclaration: referingNode withName: name [ | ||
self localDeclaration: (self scopeAddNonLocalDeclaration: name) for: referingNode | ||
|
||
] | ||
|
||
{ #category : #api } | ||
FASTLocalResolverVisitor >> on: aFASTBehaviouralEntity [ | ||
|
||
self resetScopes. | ||
aFASTBehaviouralEntity accept: self | ||
] | ||
|
||
{ #category : #accessing } | ||
FASTLocalResolverVisitor >> popScope [ | ||
|
||
^scopes pop | ||
] | ||
|
||
{ #category : #accessing } | ||
FASTLocalResolverVisitor >> pushScope [ | ||
|
||
self pushScope: Dictionary new | ||
] | ||
|
||
{ #category : #accessing } | ||
FASTLocalResolverVisitor >> pushScope: aScope [ | ||
|
||
scopes push: aScope | ||
] | ||
|
||
{ #category : #accessing } | ||
FASTLocalResolverVisitor >> resetScopes [ | ||
|
||
scopes := Stack new. | ||
"first scope for the method" | ||
self pushScope | ||
] | ||
|
||
{ #category : #private } | ||
FASTLocalResolverVisitor >> scopeAdd: aName declaration: aFASTNode [ | ||
|
||
self currentScope | ||
at: aName | ||
ifPresent: [ DuplicatedVariableError signal: | ||
'local variable ' , aName , 'already found in this scope' ] | ||
ifAbsent: [ | ||
aFASTNode resetLocalUses. | ||
self currentScope at: aName put: aFASTNode | ||
]. | ||
|
||
] | ||
|
||
{ #category : #private } | ||
FASTLocalResolverVisitor >> scopeAddNonLocalDeclaration: name [ | ||
"makes a non-local declaration kind of local by adding | ||
a FASTNonLocalDeclaration into the current scope" | ||
|
||
| currentScope nonLocalDeclaration | | ||
currentScope := self popScope. | ||
scopes | ||
ifEmpty: [ | ||
self pushScope: currentScope. | ||
nonLocalDeclaration := FASTNonLocalDeclaration new name: name. | ||
self | ||
scopeAdd: name | ||
declaration: nonLocalDeclaration | ||
] | ||
ifNotEmpty: [ | ||
nonLocalDeclaration := self scopeAddNonLocalDeclaration: name. | ||
self pushScope: currentScope. | ||
]. | ||
^nonLocalDeclaration | ||
|
||
] | ||
|
||
{ #category : #visiting } | ||
FASTLocalResolverVisitor >> visitFASTIdentifierExpression: aFASTIdentifier [ | ||
"reference to an identifier in the AST | ||
- the identifier refers to a variable (structuralEntity) | ||
- look for a definition of this identifier in the hierarchy of scopes | ||
- if not found, creates a NonLocalDeclaration for it | ||
- binds this reference to the declaration" | ||
|
||
(self findDeclaration: aFASTIdentifier name) | ||
ifNil: [ | ||
self | ||
nonLocalDeclaration: aFASTIdentifier | ||
withName: aFASTIdentifier name | ||
] | ||
ifNotNil: [ :decl | | ||
self | ||
localDeclaration: decl | ||
for: aFASTIdentifier | ||
] | ||
] | ||
|
||
{ #category : #visiting } | ||
FASTLocalResolverVisitor >> visitFASTLocalDeclaration: aFASTVariableDeclaration [ | ||
"register a variable (structuralEntity) declaration into the current scope" | ||
|
||
self | ||
scopeAdd: aFASTVariableDeclaration variable name | ||
declaration: aFASTVariableDeclaration. | ||
|
||
aFASTVariableDeclaration variable localDeclaration: aFASTVariableDeclaration. | ||
|
||
] | ||
|
||
{ #category : #visiting } | ||
FASTLocalResolverVisitor >> visitFASTScopeChildren: childrenNodes [ | ||
"creates a new scope and visit its children nodes | ||
Order of nodes is important to ensure declarations are visited before statements" | ||
|
||
self pushScope. | ||
|
||
(childrenNodes sorted: [:a :b | a startPos < b startPos]) | ||
do: [ :node | node accept: self ]. | ||
|
||
^self popScope | ||
] | ||
|
||
{ #category : #generated } | ||
FASTLocalResolverVisitor >> visitFASTTStatementBlock: aFASTJavaStatementBlock [ | ||
self pushScope. | ||
|
||
"visit order is important to ensure declarations are visited before statements" | ||
(aFASTJavaStatementBlock statements sorted: [:a :b | a startPos < b startPos]) | ||
do: [ :node | node accept: self ]. | ||
|
||
^self popScope | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
" | ||
A ""null-object"" signaling the declaration of a 'name' was not found | ||
" | ||
Class { | ||
#name : #FASTNonLocalDeclaration, | ||
#superclass : #FASTJavaEntity, | ||
#instVars : [ | ||
'name' | ||
], | ||
#category : #'FAST-Core-Tools-Resolver' | ||
} | ||
|
||
{ #category : #accessing } | ||
FASTNonLocalDeclaration >> name [ | ||
|
||
^ name | ||
] | ||
|
||
{ #category : #accessing } | ||
FASTNonLocalDeclaration >> name: anObject [ | ||
|
||
name := anObject | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...-Core-Highlighter/MooseModel.extension.st → src/FAST-Core-Tools/MooseModel.extension.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Extension { #name : #MooseModel } | ||
|
||
{ #category : #'*FAST-Core-Highlighter' } | ||
{ #category : #'*FAST-Core-Tools' } | ||
MooseModel >> fastHighligther [ | ||
^ FASTTextHighlighter | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Package { #name : #'FAST-Core-Tools' } |