Skip to content

Commit 9c96707

Browse files
committed
Begin management of the return statement (tests fail locally but I think my image is busted :( )
1 parent 756d7ac commit 9c96707

6 files changed

Lines changed: 162 additions & 23 deletions

File tree

‎src/FAST-Python-Model-Generator/FASTPythonMetamodelGenerator.class.st‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ Class {
139139
'import',
140140
'importedEntity',
141141
'fromModule',
142-
'tInvocation'
142+
'tInvocation',
143+
'tStatementBlock',
144+
'tReturnStatement',
145+
'tStatement'
143146
],
144147
#category : 'FAST-Python-Model-Generator',
145148
#package : 'FAST-Python-Model-Generator'
@@ -177,7 +180,7 @@ FASTPythonMetamodelGenerator >> defineClasses [
177180
augmentedAssignment := builder ensureClassNamed: #AugmentedAssignment."Todo"
178181
await := builder ensureClassNamed: #Await."Todo"
179182
binaryOperator := builder ensureClassNamed: #BinaryOperator.
180-
block := builder ensureClassNamed: #Block."Todo"
183+
block := builder ensureClassNamed: #Block."To review"
181184
boolean := builder ensureClassNamed: #Boolean.
182185
booleanOperator := builder ensureClassNamed: #BooleanOperator.
183186
breakStatement := builder ensureClassNamed: #BreakStatement."Todo"
@@ -301,18 +304,21 @@ FASTPythonMetamodelGenerator >> defineHierarchy [
301304
super defineHierarchy.
302305

303306
entity --|> tEntity.
304-
self flag: #todo. "The next line is WRONG and NEEDS to be reviewed but we currently need it because else everything will break until the MM is in a better state."
307+
self flag: #todo. "The next two line are WRONG and NEEDS to be reviewed but we currently need it because else everything will break until the MM is in a better state."
305308
entity --|> tExpression.
309+
entity --|> tStatement.
306310

307311
assignment --|> tAssignment.
308312

309313
attribute --|> structuralEntity.
310314

311-
booleanOperator --|> operator.
312-
booleanOperator --|> tBinaryExpression. "I am not 100% sure this is the best trait to use but it seems to work well for now"
313-
314315
binaryOperator --|> operator.
315316
binaryOperator --|> tBinaryExpression.
317+
318+
block --|> tStatementBlock.
319+
320+
booleanOperator --|> operator.
321+
booleanOperator --|> tBinaryExpression. "I am not 100% sure this is the best trait to use but it seems to work well for now"
316322

317323
boolean --|> literal.
318324
boolean --|> tBooleanLiteral.
@@ -355,6 +361,8 @@ FASTPythonMetamodelGenerator >> defineHierarchy [
355361

356362
operator --|> expression.
357363

364+
returnStatement --|> tReturnStatement.
365+
358366
string --|> literal.
359367
string --|> tStringLiteral.
360368

@@ -428,6 +436,9 @@ FASTPythonMetamodelGenerator >> defineTraits [
428436
tComment := self remoteTrait: #TComment withPrefix: #FAST.
429437
tExpression := self remoteTrait: #TExpression withPrefix: #FAST.
430438
tInvocation := self remoteTrait: #TInvocation withPrefix: #FAST.
439+
tReturnStatement := self remoteTrait: #TReturnStatement withPrefix: #FAST.
440+
tStatement := self remoteTrait: #TStatement withPrefix: #FAST.
441+
tStatementBlock := self remoteTrait: #TStatementBlock withPrefix: #FAST.
431442
tLiteral := self remoteTrait: #TLiteral withPrefix: #FAST.
432443
tNamedBehaviouralEntity := self remoteTrait: #TNamedBehaviouralEntity withPrefix: #FAST.
433444
tNamedEntity := self remoteTrait: #TNamedEntity withPrefix: #FAST.

‎src/FAST-Python-Model/FASTPyBlock.class.st‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
1+
"
2+
## Relations
3+
======================
4+
5+
### Parents
6+
| Relation | Origin | Opposite | Type | Comment |
7+
|---|
8+
| `fastBehaviouralParent` | `FASTTStatementBlock` | `statementBlock` | `FASTTBehaviouralEntity` | Behavioural entity containing the statement block.|
9+
| `parentLoopStatement` | `FASTTStatement` | `body` | `FASTTLoopStatement` | Optional loop of which this statement is the body|
10+
| `statementContainer` | `FASTTStatement` | `statements` | `FASTTStatementBlock` | Block containing this statement.|
11+
12+
### Children
13+
| Relation | Origin | Opposite | Type | Comment |
14+
|---|
15+
| `statements` | `FASTTStatementBlock` | `statementContainer` | `FASTTStatement` | Statements enclosed in this block|
16+
17+
18+
## Properties
19+
======================
20+
21+
| Name | Type | Default value | Comment |
22+
|---|
23+
| `endPos` | `Number` | nil | |
24+
| `startPos` | `Number` | nil | |
25+
26+
"
127
Class {
228
#name : 'FASTPyBlock',
329
#superclass : 'FASTPyEntity',
30+
#traits : 'FASTTStatementBlock',
31+
#classTraits : 'FASTTStatementBlock classTrait',
432
#category : 'FAST-Python-Model-Entities',
533
#package : 'FAST-Python-Model',
634
#tag : 'Entities'

‎src/FAST-Python-Model/FASTPyEntity.class.st‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
| `parentExpression` | `FASTTExpression` | `expression` | `FASTTUnaryExpression` | Parent (unary) expression|
1515
| `parentExpressionLeft` | `FASTTExpression` | `leftOperand` | `FASTTBinaryExpression` | Parent (binary) expression of which I am left side|
1616
| `parentExpressionRight` | `FASTTExpression` | `rightOperand` | `FASTTBinaryExpression` | Parent (binary) expression of which I am right side|
17+
| `parentLoopStatement` | `FASTTStatement` | `body` | `FASTTLoopStatement` | Optional loop of which this statement is the body|
1718
| `parentUnaryOperator` | `FASTTExpression` | `argument` | `FASTPyTUnaryOperator` | Parent unary operator in which I am.|
1819
| `returnOwner` | `FASTTExpression` | `expression` | `FASTTReturnStatement` | The return statement that own the expression (if it's the case)|
20+
| `statementContainer` | `FASTTStatement` | `statements` | `FASTTStatementBlock` | Block containing this statement.|
1921
2022
### Children
2123
| Relation | Origin | Opposite | Type | Comment |
@@ -35,8 +37,8 @@
3537
Class {
3638
#name : 'FASTPyEntity',
3739
#superclass : 'MooseEntity',
38-
#traits : 'FASTTEntity + FASTTExpression',
39-
#classTraits : 'FASTTEntity classTrait + FASTTExpression classTrait',
40+
#traits : 'FASTTEntity + FASTTExpression + FASTTStatement',
41+
#classTraits : 'FASTTEntity classTrait + FASTTExpression classTrait + FASTTStatement classTrait',
4042
#instVars : [
4143
'#genericChildren => FMMany type: #FASTPyEntity opposite: #genericParent',
4244
'#genericParent => FMOne type: #FASTPyEntity opposite: #genericChildren'
@@ -122,6 +124,13 @@ FASTPyEntity >> genericParentGroup [
122124
^ MooseSpecializedGroup with: self genericParent
123125
]
124126

127+
{ #category : 'testing' }
128+
FASTPyEntity >> hasImmediateSource [
129+
130+
<generated>
131+
^ false
132+
]
133+
125134
{ #category : 'testing' }
126135
FASTPyEntity >> isAssignment [
127136

@@ -171,13 +180,27 @@ FASTPyEntity >> isLiteral [
171180
^ false
172181
]
173182

183+
{ #category : 'testing' }
184+
FASTPyEntity >> isReturnStatement [
185+
186+
<generated>
187+
^ false
188+
]
189+
174190
{ #category : 'testing' }
175191
FASTPyEntity >> isScopableEntity [
176192

177193
<generated>
178194
^ false
179195
]
180196

197+
{ #category : 'testing' }
198+
FASTPyEntity >> isStatementBlock [
199+
200+
<generated>
201+
^ false
202+
]
203+
181204
{ #category : 'testing' }
182205
FASTPyEntity >> isStringLiteral [
183206

‎src/FAST-Python-Model/FASTPyReturnStatement.class.st‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
1+
"
2+
## Relations
3+
======================
4+
5+
### Parents
6+
| Relation | Origin | Opposite | Type | Comment |
7+
|---|
8+
| `parentLoopStatement` | `FASTTStatement` | `body` | `FASTTLoopStatement` | Optional loop of which this statement is the body|
9+
| `statementContainer` | `FASTTStatement` | `statements` | `FASTTStatementBlock` | Block containing this statement.|
10+
11+
### Children
12+
| Relation | Origin | Opposite | Type | Comment |
13+
|---|
14+
| `expression` | `FASTTReturnStatement` | `returnOwner` | `FASTTExpression` | The expression returned|
15+
16+
17+
## Properties
18+
======================
19+
20+
| Name | Type | Default value | Comment |
21+
|---|
22+
| `endPos` | `Number` | nil | |
23+
| `startPos` | `Number` | nil | |
24+
25+
"
126
Class {
227
#name : 'FASTPyReturnStatement',
328
#superclass : 'FASTPyEntity',
29+
#traits : 'FASTTReturnStatement',
30+
#classTraits : 'FASTTReturnStatement classTrait',
431
#category : 'FAST-Python-Model-Entities',
532
#package : 'FAST-Python-Model',
633
#tag : 'Entities'

‎src/FAST-Python-Tools-Tests/FASTPythonImporterTest.class.st‎

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ Class {
1414
}
1515

1616
{ #category : 'test generation' }
17-
FASTPythonImporterTest class >> generateAssertionsFor: entity pushing: pushString on: aStream identation: aNumber [
18-
17+
FASTPythonImporterTest class >> generateAssertionsFor: entity pushing: pushString on: aStream identation: aNumber popAtTheEnd: shouldPop [
18+
19+
| propertiesToAssert |
1920
aStream
2021
cr;
2122
tab: aNumber;
@@ -27,9 +28,9 @@ FASTPythonImporterTest class >> generateAssertionsFor: entity pushing: pushStrin
2728

2829
aStream
2930
nextPutAll: 'self assert: self topEntity sourceCode equals: ';
30-
print: entity sourceCode;
31+
print: entity sourceCode withInternalLineEndings;
3132
nextPutAll: (entity sourceCode lines size > 1
32-
ifTrue: [ ' withPlateformLineEnding.' ]
33+
ifTrue: [ ' withPlatformLineEndings.' ]
3334
ifFalse: [ '.' ]);
3435
cr;
3536
tab: aNumber;
@@ -39,10 +40,12 @@ FASTPythonImporterTest class >> generateAssertionsFor: entity pushing: pushStrin
3940
cr;
4041
tab: aNumber.
4142

42-
(entity mooseDescription allProperties select: [ :property | self shouldSelectProperty: property of: entity ]) do: [ :property |
43-
| result selector |
43+
propertiesToAssert := (entity mooseDescription allProperties select: [ :property | self shouldSelectProperty: property of: entity ]).
44+
propertiesToAssert do: [ :property |
45+
| result selector skipPop |
4446
result := property getRawFrom: entity.
4547
selector := property implementingSelector.
48+
skipPop := shouldPop and: [ property = propertiesToAssert last ].
4649
(property isMultivalued and: [ property type isPrimitive not ]) "When we have a relation to a collection of entities we do recursively do the testing."
4750
ifTrue: [
4851
result ifNotEmpty: [
@@ -66,7 +69,8 @@ FASTPythonImporterTest class >> generateAssertionsFor: entity pushing: pushStrin
6669
generateAssertionsFor: child
6770
pushing: '(stack top ' , selector , ' at: ' , index asString , ')'
6871
on: aStream
69-
identation: aNumber + 1 ] ] ]
72+
identation: aNumber + 1
73+
popAtTheEnd: (skipPop not or: [ result size = index ]) ] ] ]
7074
ifFalse: [
7175
(result isMooseObject and: [ result isMooseEntity ])
7276
ifTrue: [
@@ -86,7 +90,8 @@ FASTPythonImporterTest class >> generateAssertionsFor: entity pushing: pushStrin
8690
generateAssertionsFor: result
8791
pushing: 'self topEntity ' , selector
8892
on: aStream
89-
identation: aNumber + 1 ]
93+
identation: aNumber + 1
94+
popAtTheEnd: skipPop ]
9095
ifFalse: [
9196
aStream
9297
nextPutAll: 'self assert: self topEntity ';
@@ -97,10 +102,11 @@ FASTPythonImporterTest class >> generateAssertionsFor: entity pushing: pushStrin
97102
cr;
98103
tab: aNumber ] ] ].
99104

100-
aStream
101-
nextPutAll: 'stack pop.';
102-
cr;
103-
tab: aNumber
105+
shouldPop ifTrue: [
106+
aStream
107+
nextPutAll: 'stack pop.';
108+
cr;
109+
tab: aNumber]
104110
]
105111

106112
{ #category : 'test generation' }
@@ -145,7 +151,7 @@ FASTPythonImporterTest class >> generateTestNamed: aString fromCode: code protoc
145151

146152
model rootEntities size = 1 ifFalse: [ self error: 'FAST generator was thought with only 1 top entity?' ].
147153

148-
self generateAssertionsFor: model rootEntities anyOne genericChildren first pushing: 'fast rootEntities anyOne genericChildren first' on: aStream identation: 1 ].
154+
self generateAssertionsFor: model rootEntities anyOne genericChildren first pushing: 'fast rootEntities anyOne genericChildren first' on: aStream identation: 1popAtTheEnd: false ].
149155

150156

151157

@@ -1192,6 +1198,40 @@ FASTPythonImporterTest >> testRawString [
11921198
self assertEmpty: string allContainedEntities
11931199
]
11941200

1201+
{ #category : 'tests' }
1202+
FASTPythonImporterTest >> testReturnEmpty [
1203+
"Generated as regression test by FASTPythonImporterTest class>>#generateTestNamed:fromCode:protocol: "
1204+
1205+
self parse: 'def aFunction():
1206+
return'.
1207+
1208+
self assert: (fast allWithType: FASTPyFunctionDefinition) size equals: 1.
1209+
self assert: (fast allWithType: FASTPyBlock) size equals: 1.
1210+
self assert: (fast allWithType: FASTPyModule) size equals: 1.
1211+
self assert: (fast allWithType: FASTPyReturnStatement) size equals: 1.
1212+
1213+
stack push: fast rootEntities anyOne genericChildren first.
1214+
self assert: self topEntity sourceCode equals: 'def aFunction():
1215+
return' withPlatformLineEndings.
1216+
self assert: (self topEntity isOfType: FASTPyFunctionDefinition).
1217+
self assert: self topEntity name equals: 'aFunction'.
1218+
1219+
"Testing value of monovalue relation statementBlock"
1220+
self assert: self topEntity statementBlock isNotNil.
1221+
1222+
stack push: self topEntity statementBlock.
1223+
self assert: self topEntity sourceCode equals: 'return'.
1224+
self assert: (self topEntity isOfType: FASTPyBlock).
1225+
1226+
"Testing value of multivalued relation statements"
1227+
self assert: self topEntity statements size equals: 1.
1228+
1229+
stack push: (stack top statements at: 1).
1230+
self assert: self topEntity sourceCode equals: 'return'.
1231+
self assert: (self topEntity isOfType: FASTPyReturnStatement).
1232+
1233+
]
1234+
11951235
{ #category : 'tests - literals' }
11961236
FASTPythonImporterTest >> testSingleQuotedString [
11971237

‎src/FAST-Python-Tools/TSFASTPythonImporter.class.st‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ TSFASTPythonImporter >> visitBinaryOperator: aTSNode [
6262
^ self manageBinaryOperator: aTSNode ofKind: FASTPyBinaryOperator
6363
]
6464

65+
{ #category : 'visiting' }
66+
TSFASTPythonImporter >> visitBlock: aTSNode [
67+
68+
self onNextContextDo: [ :contextEntry | contextEntry add: 'statements' asAliasOfField: '<unnamedChild>' ].
69+
70+
^ self createEntityForNode: aTSNode kind: FASTPyBlock
71+
]
72+
6573
{ #category : 'visiting' }
6674
TSFASTPythonImporter >> visitBooleanOperator: aTSNode [
6775

@@ -128,9 +136,11 @@ TSFASTPythonImporter >> visitFunctionDefinition: aNode [
128136
"TreeSitter has the same nodes for method and function definitions but in FAST we want to disambiguate."
129137

130138
| definition |
139+
self onNextContextDo: [ :contextEntry | contextEntry add: 'statementBlock' asAliasOfField: 'body' ].
140+
131141
definition := (context anySatisfy: [ :entry | entry fastEntity isClassDefinition ])
132-
ifTrue: [ self createEntityForNode: aNode kind: FASTPyMethodDefinition ]
133-
ifFalse: [ self createEntityForNode: aNode kind: FASTPyFunctionDefinition ]
142+
ifTrue: [ self createEntityForNode: aNode kind: FASTPyMethodDefinition ]
143+
ifFalse: [ self createEntityForNode: aNode kind: FASTPyFunctionDefinition ]
134144
]
135145

136146
{ #category : 'visiting' }

0 commit comments

Comments
 (0)