Skip to content

Commit

Permalink
Merge pull request #15 from moosetechnology/local-resolver
Browse files Browse the repository at this point in the history
Added package FAST-Core-Tools
  • Loading branch information
NicolasAnquetil authored Aug 29, 2023
2 parents b5cb636 + 546840d commit 9b9db2f
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/BaselineOfFAST/BaselineOfFAST.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,25 @@ BaselineOfFAST >> defineDependencies: spec [

{ #category : #baselines }
BaselineOfFAST >> defineGroups: spec [
spec
group: 'core'
with: #( 'FAST-Core-Model' 'FAST-Core-Model-Extension' 'FAST-Model-Generator' 'FAST-Core-Visitor' ) ;

group: 'tools'
with: #( 'FAST-Core-Tools' 'FAST-Core-Tools-Tests' ) ;

group: 'default' with: #( 'core' )

]

{ #category : #baselines }
BaselineOfFAST >> definePackages: spec [
spec
package: 'FAST-Core-Model';
package: 'FAST-Core-Visitor';
package: 'FAST-Core-Model-Extension' with: [ spec requires: #('FAST-Core-Model') ];
package: 'FAST-Model-Generator';
package: 'FAST-Core-Highlighter';
package: 'FAST-Core-Model' ;
package: 'FAST-Core-Model-Extension' with: [ spec requires: #('FAST-Core-Model') ] ;
package: 'FAST-Core-Visitor' ;
package: 'FAST-Model-Generator' ;
package: 'FAST-Core-Tools' with: [ spec requires: #('FAST-Core-Visitor') ] ;
package: 'FAST-Core-Tools-Tests' with: [ spec requires: #('FAST-Core-Tools') ] ;
package: 'FAST-Core-Deprecated'
]
1 change: 0 additions & 1 deletion src/FAST-Core-Highlighter/package.st

This file was deleted.

38 changes: 38 additions & 0 deletions src/FAST-Core-Tools/FASTEntity.extension.st
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
]
191 changes: 191 additions & 0 deletions src/FAST-Core-Tools/FASTLocalResolverVisitor.class.st
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
]
23 changes: 23 additions & 0 deletions src/FAST-Core-Tools/FASTNonLocalDeclaration.class.st
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
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Class {
#instVars : [
'attributeMapper'
],
#category : #'FAST-Core-Highlighter'
#category : #'FAST-Core-Tools-Highlighter'
}

{ #category : #public }
Expand Down
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
]
1 change: 1 addition & 0 deletions src/FAST-Core-Tools/package.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : #'FAST-Core-Tools' }

0 comments on commit 9b9db2f

Please sign in to comment.