Skip to content

Commit f018636

Browse files
committed
Start to implement the CFG
1 parent 20d782f commit f018636

6 files changed

Lines changed: 80 additions & 6 deletions

File tree

src/FAST-Python-Model/FASTPyExpression.class.st

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ FASTPyExpression >> collectionInitializer: anObject [
126126
collectionInitializer := anObject
127127
]
128128

129+
{ #category : 'testing' }
130+
FASTPyExpression >> isExpressionStatement [
131+
132+
self containersDo: [ :container | (container isOfType: FASTTStatementBlock) ifTrue: [ ^ true ] ].
133+
134+
^ false
135+
]
136+
129137
{ #category : 'accessing' }
130138
FASTPyExpression >> parentAssertStatement [
131139
"Relation named: #parentAssertStatement type: #FASTPyAssertStatement opposite: #expressions"

src/FAST-Python-Model/FASTPyModel.class.st

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ FASTPyModel class >> annotation [
2020
<package: #'FAST-Python-Model'>
2121
<generated>
2222
]
23+
24+
{ #category : 'as yet unclassified' }
25+
FASTPyModel >> allFunctionDefinitions [
26+
27+
^ self allWithType: FASTPyFunctionDefinition
28+
]

src/FAST-Python-Tools-Tests/FASTPythonCFGTest.class.st

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ Class {
1212
{ #category : 'running' }
1313
FASTPythonCFGTest >> buildCFGFor: aString [
1414

15-
startBlock := builder buildCFGForModel: (self parse: aString) rootEntities anyOne
15+
self flag: #todo. "What should be the entry point?"
16+
startBlock := builder buildCFGForModel: (self parse: aString) allFunctionDefinitions first
1617
]
1718

1819
{ #category : 'running' }
@@ -30,11 +31,20 @@ FASTPythonCFGTest >> setUp [
3031
builder := FASTPythonCFGBuilder new
3132
]
3233

34+
{ #category : 'tests' }
35+
FASTPythonCFGTest >> testFunctionWithIf [
36+
37+
self buildCFGFor: 'def funct():
38+
print("Hello")'.
39+
self skip.
40+
self halt
41+
]
42+
3343
{ #category : 'tests' }
3444
FASTPythonCFGTest >> testFunctionWithOneStatement [
3545

3646
self buildCFGFor: 'def funct():
37-
return 3'.
47+
print("Hello")'.
3848

3949
self halt
4050
]

src/FAST-Python-Tools/FASTPythonCFGBuilder.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ Class {
1313
FASTPythonCFGBuilder >> initialize [
1414

1515
super initialize.
16-
cfgVisitor := FASTPythonCFGVisitor new
16+
self cfgVisitor: FASTPythonCFGVisitor new
1717
]
Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,48 @@
11
Class {
22
#name : 'FASTPythonCFGVisitor',
3-
#superclass : 'Object',
4-
#traits : 'FASTTCFGVisitor + FASTPyTVisitor',
5-
#classTraits : 'FASTTCFGVisitor classTrait + FASTPyTVisitor classTrait',
3+
#superclass : 'FASTPythonVisitor',
4+
#traits : 'FASTTCFGVisitor',
5+
#classTraits : 'FASTTCFGVisitor classTrait',
66
#category : 'FAST-Python-Tools-CFG/DataFlow',
77
#package : 'FAST-Python-Tools',
88
#tag : 'CFG/DataFlow'
99
}
10+
11+
{ #category : 'visiting' }
12+
FASTPythonCFGVisitor >> visitFASTPyExpression: aFASTPyExpression [
13+
"We ignore expressions that are not in a statement block"
14+
15+
aFASTPyExpression isExpressionStatement ifTrue: [ ^ super visitFASTPyExpression: aFASTPyExpression ]
16+
]
17+
18+
{ #category : 'visiting' }
19+
FASTPythonCFGVisitor >> visitFASTPyFunctionDefinition: aFunction [
20+
21+
super visitFASTPyFunctionDefinition: aFunction.
22+
23+
cfgBuilder basicBlocks first isStart: true.
24+
25+
"case where method has no return instruction (void signature)"
26+
cfgBuilder currentBlock ifNotNil: [ cfgBuilder chainPendingBlocksTo: FASTNullBlock new ]
27+
]
28+
29+
{ #category : 'visiting' }
30+
FASTPythonCFGVisitor >> visitFASTTStatement: aStatement [
31+
32+
| currentBlock |
33+
cfgBuilder currentBlock ifNil: [ cfgBuilder newBasicBlock: FASTBasicBlock ].
34+
35+
currentBlock := cfgBuilder currentBlock.
36+
37+
super visitFASTTStatement: aStatement.
38+
39+
"only adding statement if no expression made changes to the CFG"
40+
currentBlock = cfgBuilder currentBlock ifTrue: [ cfgBuilder currentBlock addStatement: aStatement ]
41+
]
42+
43+
{ #category : 'visiting' }
44+
FASTPythonCFGVisitor >> visitFASTTStatementBlock: aFASTTStatementBlock [
45+
"We do not consider the block as a statemert."
46+
47+
self visitCollection: aFASTTStatementBlock statements
48+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"
2+
I am a basic top down visitor for a FAST Python model.
3+
"
4+
Class {
5+
#name : 'FASTPythonVisitor',
6+
#superclass : 'Object',
7+
#traits : 'FASTPyTVisitor',
8+
#classTraits : 'FASTPyTVisitor classTrait',
9+
#category : 'FAST-Python-Visitor',
10+
#package : 'FAST-Python-Visitor'
11+
}

0 commit comments

Comments
 (0)