-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGtPythonVarNodeVisitor.class.st
More file actions
114 lines (96 loc) · 3.41 KB
/
Copy pathGtPythonVarNodeVisitor.class.st
File metadata and controls
114 lines (96 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
Class {
#name : #GtPythonVarNodeVisitor,
#superclass : #Object,
#traits : 'TPyRootNodeVisitor',
#classTraits : 'TPyRootNodeVisitor classTrait',
#instVars : [
'accessedFirstVariables',
'assignedVariables',
'temporaryVariables'
],
#category : #'Lepiter-Python-Coder'
}
{ #category : #accessing }
GtPythonVarNodeVisitor >> accessedFirstVariableNames [
"Answer the set of variable names where the variable is accessed before being assigned a value (if at all)"
^ accessedFirstVariables
]
{ #category : #generated }
GtPythonVarNodeVisitor >> accessedFirstVariableNames: anObject [
accessedFirstVariables := anObject
]
{ #category : #accessing }
GtPythonVarNodeVisitor >> assignedVariableNames [
"Answer the set of variable names that are assigned to"
^ assignedVariables
]
{ #category : #generated }
GtPythonVarNodeVisitor >> assignedVariables: anObject [
assignedVariables := anObject
]
{ #category : #accessing }
GtPythonVarNodeVisitor >> globalVariableNames [
"Answer the set of variable names that are global, used but not declared tempary"
^ (accessedFirstVariables , assignedVariables) difference: temporaryVariables
]
{ #category : #initialization }
GtPythonVarNodeVisitor >> initialize [
super initialize.
assignedVariables := Set new.
temporaryVariables := Set new.
accessedFirstVariables := Set new.
]
{ #category : #accessing }
GtPythonVarNodeVisitor >> temporaryVariableNames [
"Answer the set of variable names that are declared as temporary"
^ temporaryVariables
]
{ #category : #generated }
GtPythonVarNodeVisitor >> temporaryVariableNames: anObject [
temporaryVariables := anObject
]
{ #category : #accessing }
GtPythonVarNodeVisitor >> undeclaredVariableNames [
"Answer the set of variable names where the variable is assigned a value, but is not a temporary variable"
^ self assignedVariableNames difference: self temporaryVariableNames
]
{ #category : #generated }
GtPythonVarNodeVisitor >> visitAssignmentStatement: anAssignmentStatement [
| result assignmentOperator |
result := self visitStatement: anAssignmentStatement.
assignmentOperator := anAssignmentStatement assigns first value.
assignmentOperator = '='
ifTrue: [ anAssignmentStatement variables first
withAllNodesOfType: PyVariableExpressionNode
do: [ :aNode | assignedVariables add: aNode nameToken value ] ].
^ result
]
{ #category : #generated }
GtPythonVarNodeVisitor >> visitFunctionDefinition: aFunctionDefinition [
| visitor |
visitor := GtPythonVarNodeVisitor new
accessedFirstVariableNames: self accessedFirstVariableNames copy;
assignedVariables: self assignedVariableNames copy;
temporaryVariableNames: self temporaryVariableNames copy.
visitor visitStatement: aFunctionDefinition.
self accessedFirstVariableNames: visitor accessedFirstVariableNames.
temporaryVariables add: aFunctionDefinition fname value.
^ self
]
{ #category : #generated }
GtPythonVarNodeVisitor >> visitParameter: aParameter [
aParameter name
ifNotNil: [ :name |
name
withAllNodesOfType: PyVariableExpressionNode
do: [ :node | temporaryVariables add: node nameToken value ] ].
^ self visitRoot: aParameter
]
{ #category : #generated }
GtPythonVarNodeVisitor >> visitVariableExpression: aVariableExpression [
| name |
name := aVariableExpression nameToken value.
((assignedVariables includes: name) or: [ temporaryVariables includes: name ])
ifFalse: [ accessedFirstVariables add: name ].
^ self visitExpression: aVariableExpression
]