Skip to content

Commit fd3afcd

Browse files
committed
Manage global and nonlocal statements
1 parent be61f1d commit fd3afcd

6 files changed

Lines changed: 305 additions & 12 deletions

File tree

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ FASTPythonMetamodelGenerator >> defineClasses [
230230
functionDefinition := builder ensureClassNamed: #FunctionDefinition."ToReview"
231231
generatorExpression := builder ensureClassNamed: #GeneratorExpression."Todo"
232232
genericType := builder ensureClassNamed: #GenericType."Todo"
233-
globalStatement := builder ensureClassNamed: #GlobalStatement."Todo"
233+
globalStatement := builder ensureClassNamed: #GlobalStatement.
234234
identifier := builder ensureClassNamed: #Identifier."Todo"
235235
ifClause := builder ensureClassNamed: #IfClause."Todo"
236236
ifStatement := builder ensureClassNamed: #IfStatement."Todo"
@@ -258,7 +258,7 @@ FASTPythonMetamodelGenerator >> defineClasses [
258258
module := builder ensureClassNamed: #Module."ToReview"
259259
namedExpression := builder ensureClassNamed: #NamedExpression."Todo"
260260
none := builder ensureClassNamed: #None.
261-
nonlocalStatement := builder ensureClassNamed: #NonlocalStatement."Todo"
261+
nonlocalStatement := builder ensureClassNamed: #NonlocalStatement.
262262
notOperator := builder ensureClassNamed: #NotOperator.
263263
operator := builder ensureClassNamed: #Operator.
264264
pair := builder ensureClassNamed: #Pair.
@@ -354,6 +354,8 @@ FASTPythonMetamodelGenerator >> defineHierarchy [
354354

355355
functionDefinition --|> tNamedBehaviouralEntity.
356356

357+
globalStatement --|> statement.
358+
357359
integer --|> literal.
358360

359361
list --|> collectionInitializer.
@@ -362,6 +364,8 @@ FASTPythonMetamodelGenerator >> defineHierarchy [
362364

363365
none --|> literal.
364366

367+
nonlocalStatement --|> statement.
368+
365369
pair --|> expression.
366370

367371
parameter --|> structuralEntity.
@@ -430,23 +434,29 @@ FASTPythonMetamodelGenerator >> defineRelations [
430434

431435
((assertStatement property: #expression) comment: 'The expression asserted')
432436
<>- ((tExpression property: #assertOwner) comment: 'The assert statement that own the expression (if it''s the case)').
433-
434-
((deleteStatement property: #expression) comment: 'The expression to apply the delete on')
435-
<>- ((tExpression property: #deleteOwner) comment: 'The delete statement that own the expression (if it''s the case)').
437+
438+
((collectionInitializer property: #initializers) comment: 'Each initializer defines one element.')
439+
<>-* ((tExpression property: #collectionInitializer) comment: 'The owner of the expression').
436440

437441
((comparisonOperator property: #operands) comment:
438442
'List of the operands of the comparison operands. For example if we have `a == b != c`, it will be a, b and c.')
439443
<>-* ((tExpression property: #parentComparisonOperator) comment: 'Parent comparison expression of which I am.').
440444

445+
((deleteStatement property: #expression) comment: 'The expression to apply the delete on')
446+
<>- ((tExpression property: #deleteOwner) comment: 'The delete statement that own the expression (if it''s the case)').
447+
448+
((globalStatement property: #variable) comment: 'The variable scoped')
449+
<>- ((variable property: #globalOwner) comment: 'The global statement defining my scope (if it''s the case)').
450+
441451
((import property: #importedEntities) comment: 'List of the imported entities.')
442452
<>-* ((importedEntity property: #import) comment: 'Import declaring me as imported entity.').
443453

444454
((importFromStatement property: #fromModule) comment:
445455
'Module in which the imported entities should be looked for. `from datetime import date` has `datetime` as fromModule..')
446456
<>- ((fromModule property: #import) comment: 'Import declaring me as from import module.').
447-
448-
((collectionInitializer property: #initializers) comment: 'Each initializer defines one element.')
449-
<>-* ((tExpression property: #collectionInitializer) comment: 'The owner of the expression').
457+
458+
((nonlocalStatement property: #variable) comment: 'The variable scoped')
459+
<>- ((variable property: #nonlocalOwner) comment: 'The global statement defining my scope (if it''s the case)').
450460

451461
((tUnaryOperator property: #argument) comment: 'Expression influanced by the unary operator..')
452462
<>- ((tExpression property: #parentUnaryOperator) comment: 'Parent unary operator in which I am.')
Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1+
"
2+
## Relations
3+
======================
4+
5+
### Children
6+
| Relation | Origin | Opposite | Type | Comment |
7+
|---|
8+
| `variable` | `FASTPyGlobalStatement` | `globalOwner` | `FASTPyVariable` | The variable scoped|
9+
10+
11+
12+
"
113
Class {
214
#name : 'FASTPyGlobalStatement',
3-
#superclass : 'FASTPyEntity',
15+
#superclass : 'FASTPyStatement',
16+
#instVars : [
17+
'#variable => FMOne type: #FASTPyVariable opposite: #globalOwner'
18+
],
419
#category : 'FAST-Python-Model-Entities',
520
#package : 'FAST-Python-Model',
621
#tag : 'Entities'
@@ -9,8 +24,31 @@ Class {
924
{ #category : 'meta' }
1025
FASTPyGlobalStatement class >> annotation [
1126

12-
<FMClass: #GlobalStatement super: #FASTPyEntity>
27+
<FMClass: #GlobalStatement super: #FASTPyStatement>
1328
<package: #'FAST-Python-Model'>
1429
<generated>
1530
^ self
1631
]
32+
33+
{ #category : 'accessing' }
34+
FASTPyGlobalStatement >> variable [
35+
"Relation named: #variable type: #FASTPyVariable opposite: #globalOwner"
36+
37+
<generated>
38+
<FMComment: 'The variable scoped'>
39+
^ variable
40+
]
41+
42+
{ #category : 'accessing' }
43+
FASTPyGlobalStatement >> variable: anObject [
44+
45+
<generated>
46+
variable := anObject
47+
]
48+
49+
{ #category : 'navigation' }
50+
FASTPyGlobalStatement >> variableGroup [
51+
<generated>
52+
<navigation: 'Variable'>
53+
^ MooseSpecializedGroup with: self variable
54+
]
Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1+
"
2+
## Relations
3+
======================
4+
5+
### Children
6+
| Relation | Origin | Opposite | Type | Comment |
7+
|---|
8+
| `variable` | `FASTPyNonlocalStatement` | `nonlocalOwner` | `FASTPyVariable` | The variable scoped|
9+
10+
11+
12+
"
113
Class {
214
#name : 'FASTPyNonlocalStatement',
3-
#superclass : 'FASTPyEntity',
15+
#superclass : 'FASTPyStatement',
16+
#instVars : [
17+
'#variable => FMOne type: #FASTPyVariable opposite: #nonlocalOwner'
18+
],
419
#category : 'FAST-Python-Model-Entities',
520
#package : 'FAST-Python-Model',
621
#tag : 'Entities'
@@ -9,8 +24,31 @@ Class {
924
{ #category : 'meta' }
1025
FASTPyNonlocalStatement class >> annotation [
1126

12-
<FMClass: #NonlocalStatement super: #FASTPyEntity>
27+
<FMClass: #NonlocalStatement super: #FASTPyStatement>
1328
<package: #'FAST-Python-Model'>
1429
<generated>
1530
^ self
1631
]
32+
33+
{ #category : 'accessing' }
34+
FASTPyNonlocalStatement >> variable [
35+
"Relation named: #variable type: #FASTPyVariable opposite: #nonlocalOwner"
36+
37+
<generated>
38+
<FMComment: 'The variable scoped'>
39+
^ variable
40+
]
41+
42+
{ #category : 'accessing' }
43+
FASTPyNonlocalStatement >> variable: anObject [
44+
45+
<generated>
46+
variable := anObject
47+
]
48+
49+
{ #category : 'navigation' }
50+
FASTPyNonlocalStatement >> variableGroup [
51+
<generated>
52+
<navigation: 'Variable'>
53+
^ MooseSpecializedGroup with: self variable
54+
]

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1+
"
2+
## Relations
3+
======================
4+
5+
### Parents
6+
| Relation | Origin | Opposite | Type | Comment |
7+
|---|
8+
| `globalOwner` | `FASTPyVariable` | `variable` | `FASTPyGlobalStatement` | The global statement defining my scope (if it's the case)|
9+
| `nonlocalOwner` | `FASTPyVariable` | `variable` | `FASTPyNonlocalStatement` | The global statement defining my scope (if it's the case)|
10+
11+
12+
13+
"
114
Class {
215
#name : 'FASTPyVariable',
316
#superclass : 'FASTPyStructuralEntity',
17+
#instVars : [
18+
'#globalOwner => FMOne type: #FASTPyGlobalStatement opposite: #variable',
19+
'#nonlocalOwner => FMOne type: #FASTPyNonlocalStatement opposite: #variable'
20+
],
421
#category : 'FAST-Python-Model-Entities',
522
#package : 'FAST-Python-Model',
623
#tag : 'Entities'
@@ -14,3 +31,53 @@ FASTPyVariable class >> annotation [
1431
<generated>
1532
^ self
1633
]
34+
35+
{ #category : 'accessing' }
36+
FASTPyVariable >> globalOwner [
37+
"Relation named: #globalOwner type: #FASTPyGlobalStatement opposite: #variable"
38+
39+
<generated>
40+
<FMComment: 'The global statement defining my scope (if it''s the case)'>
41+
<container>
42+
<derived>
43+
^ globalOwner
44+
]
45+
46+
{ #category : 'accessing' }
47+
FASTPyVariable >> globalOwner: anObject [
48+
49+
<generated>
50+
globalOwner := anObject
51+
]
52+
53+
{ #category : 'navigation' }
54+
FASTPyVariable >> globalOwnerGroup [
55+
<generated>
56+
<navigation: 'GlobalOwner'>
57+
^ MooseSpecializedGroup with: self globalOwner
58+
]
59+
60+
{ #category : 'accessing' }
61+
FASTPyVariable >> nonlocalOwner [
62+
"Relation named: #nonlocalOwner type: #FASTPyNonlocalStatement opposite: #variable"
63+
64+
<generated>
65+
<FMComment: 'The global statement defining my scope (if it''s the case)'>
66+
<container>
67+
<derived>
68+
^ nonlocalOwner
69+
]
70+
71+
{ #category : 'accessing' }
72+
FASTPyVariable >> nonlocalOwner: anObject [
73+
74+
<generated>
75+
nonlocalOwner := anObject
76+
]
77+
78+
{ #category : 'navigation' }
79+
FASTPyVariable >> nonlocalOwnerGroup [
80+
<generated>
81+
<navigation: 'NonlocalOwner'>
82+
^ MooseSpecializedGroup with: self nonlocalOwner
83+
]

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

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,66 @@ FASTPythonImporterTest >> testFutureImport [
946946

947947
]
948948

949+
{ #category : 'tests - statements' }
950+
FASTPythonImporterTest >> testGlobalStatement [
951+
"Generated as regression test by FASTPyImporterTestGenerator>>#generateTestNamed:fromCode:protocol: "
952+
953+
self parse: 'def aFunction():
954+
global counter
955+
return counter'.
956+
957+
self assert: (fast allWithType: FASTPyModule) size equals: 1.
958+
self assert: (fast allWithType: FASTPyGlobalStatement) size equals: 1.
959+
self assert: (fast allWithType: FASTPyBlock) size equals: 1.
960+
self assert: (fast allWithType: FASTPyReturnStatement) size equals: 1.
961+
self assert: (fast allWithType: FASTPyIdentifier) size equals: 1.
962+
self assert: (fast allWithType: FASTPyFunctionDefinition) size equals: 1.
963+
self assert: (fast allWithType: FASTPyVariable) size equals: 1.
964+
965+
stack push: fast rootEntities anyOne genericChildren first.
966+
self assert: self topEntity sourceCode equals: 'def aFunction():
967+
global counter
968+
return counter' withPlatformLineEndings.
969+
self assert: (self topEntity isOfType: FASTPyFunctionDefinition).
970+
self assert: self topEntity name equals: 'aFunction'.
971+
972+
"Testing value of monovalue relation statementBlock"
973+
self assert: self topEntity statementBlock isNotNil.
974+
975+
stack push: self topEntity statementBlock.
976+
self assert: self topEntity sourceCode equals: 'global counter
977+
return counter' withPlatformLineEndings.
978+
self assert: (self topEntity isOfType: FASTPyBlock).
979+
980+
"Testing value of multivalued relation statements"
981+
self assert: self topEntity statements size equals: 2.
982+
983+
stack push: (stack top statements at: 1).
984+
self assert: self topEntity sourceCode equals: 'global counter'.
985+
self assert: (self topEntity isOfType: FASTPyGlobalStatement).
986+
987+
"Testing value of monovalue relation variable"
988+
self assert: self topEntity variable isNotNil.
989+
990+
stack push: self topEntity variable.
991+
self assert: self topEntity sourceCode equals: 'counter'.
992+
self assert: (self topEntity isOfType: FASTPyVariable).
993+
stack pop.
994+
stack pop.
995+
996+
stack push: (stack top statements at: 2).
997+
self assert: self topEntity sourceCode equals: 'return counter'.
998+
self assert: (self topEntity isOfType: FASTPyReturnStatement).
999+
1000+
"Testing value of monovalue relation expression"
1001+
self assert: self topEntity expression isNotNil.
1002+
1003+
stack push: self topEntity expression.
1004+
self assert: self topEntity sourceCode equals: 'counter'.
1005+
self assert: (self topEntity isOfType: FASTPyIdentifier).
1006+
1007+
]
1008+
9491009
{ #category : 'tests - imports' }
9501010
FASTPythonImporterTest >> testImport [
9511011
"Generated as regression test by FASTPythonImporterTest class>>#generateTestNamed:fromCode: "
@@ -1196,6 +1256,66 @@ FASTPythonImporterTest >> testNone [
11961256
self assert: (boolean isOfType: FASTTLiteral)
11971257
]
11981258

1259+
{ #category : 'tests - statements' }
1260+
FASTPythonImporterTest >> testNonlocalStatement [
1261+
"Generated as regression test by FASTPyImporterTestGenerator>>#generateTestNamed:fromCode:protocol: "
1262+
1263+
self parse: 'def aFunction():
1264+
nonlocal counter
1265+
return counter'.
1266+
1267+
self assert: (fast allWithType: FASTPyModule) size equals: 1.
1268+
self assert: (fast allWithType: FASTPyNonlocalStatement) size equals: 1.
1269+
self assert: (fast allWithType: FASTPyBlock) size equals: 1.
1270+
self assert: (fast allWithType: FASTPyReturnStatement) size equals: 1.
1271+
self assert: (fast allWithType: FASTPyIdentifier) size equals: 1.
1272+
self assert: (fast allWithType: FASTPyFunctionDefinition) size equals: 1.
1273+
self assert: (fast allWithType: FASTPyVariable) size equals: 1.
1274+
1275+
stack push: fast rootEntities anyOne genericChildren first.
1276+
self assert: self topEntity sourceCode equals: 'def aFunction():
1277+
nonlocal counter
1278+
return counter' withPlatformLineEndings.
1279+
self assert: (self topEntity isOfType: FASTPyFunctionDefinition).
1280+
self assert: self topEntity name equals: 'aFunction'.
1281+
1282+
"Testing value of monovalue relation statementBlock"
1283+
self assert: self topEntity statementBlock isNotNil.
1284+
1285+
stack push: self topEntity statementBlock.
1286+
self assert: self topEntity sourceCode equals: 'nonlocal counter
1287+
return counter' withPlatformLineEndings.
1288+
self assert: (self topEntity isOfType: FASTPyBlock).
1289+
1290+
"Testing value of multivalued relation statements"
1291+
self assert: self topEntity statements size equals: 2.
1292+
1293+
stack push: (stack top statements at: 1).
1294+
self assert: self topEntity sourceCode equals: 'nonlocal counter'.
1295+
self assert: (self topEntity isOfType: FASTPyNonlocalStatement).
1296+
1297+
"Testing value of monovalue relation variable"
1298+
self assert: self topEntity variable isNotNil.
1299+
1300+
stack push: self topEntity variable.
1301+
self assert: self topEntity sourceCode equals: 'counter'.
1302+
self assert: (self topEntity isOfType: FASTPyVariable).
1303+
stack pop.
1304+
stack pop.
1305+
1306+
stack push: (stack top statements at: 2).
1307+
self assert: self topEntity sourceCode equals: 'return counter'.
1308+
self assert: (self topEntity isOfType: FASTPyReturnStatement).
1309+
1310+
"Testing value of monovalue relation expression"
1311+
self assert: self topEntity expression isNotNil.
1312+
1313+
stack push: self topEntity expression.
1314+
self assert: self topEntity sourceCode equals: 'counter'.
1315+
self assert: (self topEntity isOfType: FASTPyIdentifier).
1316+
1317+
]
1318+
11991319
{ #category : 'tests - operators' }
12001320
FASTPythonImporterTest >> testNotOperator [
12011321

0 commit comments

Comments
 (0)