Skip to content

Commit 70aabbd

Browse files
committed
Start to manage local variables
1 parent 1bfed42 commit 70aabbd

2 files changed

Lines changed: 101 additions & 1 deletion

File tree

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,96 @@ x'.
3737
self assert: usedVariable equals: assignedVariable localDeclaration localUses second
3838
]
3939

40+
{ #category : 'tests' }
41+
FASTPythonLocalResolverTest >> testGlobalAndTemporaryOfTheSameNameInFunction [
42+
43+
| globalAssignment assignedGlobal usedGlobal temporaryAssignment assignedTemporary temporaryUsage1 temporaryUsage2 |
44+
self parseAndResolve: 'glob = 1
45+
46+
def fun():
47+
glob = 3
48+
print(glob)
49+
return glob
50+
51+
fun()
52+
print(glob)'.
53+
54+
globalAssignment := model module statements first.
55+
assignedGlobal := globalAssignment left.
56+
self assert: assignedGlobal localDeclaration equals: globalAssignment.
57+
self assert: assignedGlobal localDeclaration left name equals: 'glob'.
58+
self deny: assignedGlobal localDeclaration isNonLocalDeclaration.
59+
self assert: assignedGlobal localDeclaration localUses size equals: 2.
60+
self assert: assignedGlobal localDeclaration localUses first equals: assignedGlobal.
61+
62+
usedGlobal := model module statements fourth arguments first.
63+
self assert: usedGlobal localDeclaration equals: globalAssignment.
64+
self assert: usedGlobal equals: assignedGlobal localDeclaration localUses second.
65+
66+
temporaryAssignment := model module statements second statements first.
67+
assignedTemporary := temporaryAssignment left.
68+
self assert: assignedTemporary localDeclaration equals: temporaryAssignment.
69+
self assert: assignedTemporary localDeclaration left name equals: 'glob'.
70+
self deny: assignedTemporary localDeclaration isNonLocalDeclaration.
71+
self assert: assignedTemporary localDeclaration localUses size equals: 3.
72+
self assert: assignedTemporary localDeclaration localUses first equals: assignedTemporary.
73+
74+
temporaryUsage1 := model module statements second statements second arguments first.
75+
self assert: temporaryUsage1 localDeclaration equals: temporaryAssignment.
76+
self assert: temporaryUsage1 equals: assignedTemporary localDeclaration localUses second.
77+
78+
temporaryUsage2 := model module statements second statements third expression.
79+
self assert: temporaryUsage2 localDeclaration equals: temporaryAssignment.
80+
self assert: temporaryUsage2 equals: assignedTemporary localDeclaration localUses third
81+
]
82+
83+
{ #category : 'tests' }
84+
FASTPythonLocalResolverTest >> testGlobalUsedInFunction [
85+
86+
| assignment assignedVariable usedVariable |
87+
self parseAndResolve: 'glob = 1
88+
89+
def fun():
90+
return glob
91+
92+
print(fun())'.
93+
94+
assignment := model module statements first.
95+
assignedVariable := assignment left.
96+
self assert: assignedVariable localDeclaration equals: assignment.
97+
self assert: assignedVariable localDeclaration left name equals: 'glob'.
98+
self deny: assignedVariable localDeclaration isNonLocalDeclaration.
99+
self assert: assignedVariable localDeclaration localUses size equals: 2.
100+
self assert: assignedVariable localDeclaration localUses first equals: assignedVariable.
101+
102+
usedVariable := model module statements second statements first expression.
103+
self assert: usedVariable localDeclaration equals: assignment.
104+
self assert: usedVariable equals: assignedVariable localDeclaration localUses second
105+
]
106+
107+
{ #category : 'tests' }
108+
FASTPythonLocalResolverTest >> testLocalVariableInFunction [
109+
110+
| assignment assignedVariable usedVariable |
111+
self parseAndResolve: 'def f():
112+
x = 1
113+
return x
114+
115+
print(f())'.
116+
117+
assignment := model module statements first statements first.
118+
assignedVariable := assignment left.
119+
self assert: assignedVariable localDeclaration equals: assignment.
120+
self assert: assignedVariable localDeclaration left name equals: 'x'.
121+
self deny: assignedVariable localDeclaration isNonLocalDeclaration.
122+
self assert: assignedVariable localDeclaration localUses size equals: 2.
123+
self assert: assignedVariable localDeclaration localUses first equals: assignedVariable.
124+
125+
usedVariable := model module statements first statements second expression.
126+
self assert: usedVariable localDeclaration equals: assignment.
127+
self assert: usedVariable equals: assignedVariable localDeclaration localUses second
128+
]
129+
40130
{ #category : 'tests' }
41131
FASTPythonLocalResolverTest >> testTwoAssignationsCreateOneDeclaration [
42132

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ FASTPythonLocalResolverVisitor >> visitFASTPyIdentifier: anIdentifier [
6363

6464
{ #category : 'visiting' }
6565
FASTPythonLocalResolverVisitor >> visitFASTPyVariable: aVariable [
66-
6766
"We are declaring a variable in an assignation"
67+
6868
aVariable parentAssignmentLeft ifNotNil: [ :assignment |
6969
[
7070
scoper scopeAdd: aVariable name declaration: assignment.
@@ -74,3 +74,13 @@ FASTPythonLocalResolverVisitor >> visitFASTPyVariable: aVariable [
7474

7575
super visitFASTPyVariable: aVariable
7676
]
77+
78+
{ #category : 'visiting' }
79+
FASTPythonLocalResolverVisitor >> visitFASTTStatementBlock: aFASTJavaStatementBlock [
80+
81+
scoper pushScope.
82+
83+
super visitFASTTStatementBlock: aFASTJavaStatementBlock.
84+
85+
^ scoper popScope
86+
]

0 commit comments

Comments
 (0)