8000 Implements scopeGraph for SimpleStatements and ExpressionStatement · github/semantic@7fd8888 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 7fd8888

Browse files
Implements scopeGraph for SimpleStatements and ExpressionStatement
Co-Authored-By: Josh Vera <vera@github.com>
1 parent 68bef8d commit 7fd8888

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

semantic-python/src/Language/Python/ScopeGraph.hs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import Data.List.NonEmpty (NonEmpty (..))
3535
import qualified Data.List.NonEmpty as NonEmpty
3636
import qualified Data.ScopeGraph as ScopeGraph
3737
import qualified Data.Text as Text
38+
import Data.Traversable
3839
import Debug.Trace
3940
import GHC.Records
4041
import GHC.TypeLits
@@ -81,13 +82,13 @@ onField =
8182
. getField @field
8283

8384
-- ([(node_for_the_name, ast_for_the_decl)], ...)
84-
type ClassBodyBinders = Py.Assignment
85+
type ClassBodyBinder = Py.Assignment
8586
:+: Py.AugmentedAss 10000 ignment
8687
:+: Py.FunctionDefinition
8788
:+: Py.ClassDefinition
8889
:+: Py.DecoratedDefinition
8990

90-
type BodyStruct a b = ([(Stack.Node, [ClassBodyBinders a])], b)
91+
type BodyStruct a b = ([(Stack.Node, ClassBodyBinder a)], b)
9192

9293
noBindings :: b -> BodyStruct a b
9394
noBindings x = ([],x)
@@ -108,8 +109,14 @@ instance ToScopeGraph Py.Assignment where
108109
scopeGraph asgn@(Py.Assignment ann (SingleIdentifier identifier) (Just val) _typ) = do
109110
-- TODO: What should we do with the type of an assignment?
110111
-- TODO: What should we do with the right hand side of an assignment?
111-
_ <- scopeGraph val
112-
pure (Complete ([_], Stack.Declaration identifier Scope.Identifier ann))
112+
res <- scopeGraph val
113+
let propagateThese = case res of
114+
Complete (Left (Left (bindings, _))) -> bindings
115+
Complete (Left (Right (bindings, _))) -> bindings
116+
Complete (Right (Left (bindings, _))) -> bindings
117+
Complete (Right (Right (bindings, _))) -> bindings
118+
decl = Stack.Declaration identifier Scope.Identifier ann
119+
pure (Complete ((decl,L1 asgn) : propagateThese, decl))
113120
scopeGraph x = todo x
114121

115122
instance ToScopeGraph Py.Await where
@@ -205,49 +212,31 @@ instance ToScopeGraph Py.ClassDefinition where
205212
res <- scopeGraph body
206213
case res of
207214
Complete (bindings, _) -> do
208-
for_ bindings $ \(node, statements) ->
209-
_
210-
{-
211-
do
215+
for_ bindings $ \(node, statement) ->
212216
if isInstanceMember statement then
213217
modify (Stack.addEdge (Stack.InstanceMembers "IM") node)
214218
else if isClassMember statement then
215219
modify (Stack.addEdge (Stack.ClassMembers "CM") node)
216220
else pure ()
217-
-}
218221
pure ()
219222
res -> pure ()
220223

221224
-- TODO: replace R1L1 with injection
222-
pure (Complete ([(declaration,[R1 (R1 (R1 (L1 def)))])], []))
225+
pure (Complete ([(declaration,R1 (R1 (R1 (L1 def))))], []))
223226

224227
-- for_ bindings $ \(node, statement) -> do
225228
-- -- let callNode = Stack.PopSymbol "()"
226229
-- undefined
227230

228-
isInstanceMember :: ClassBodyBinders a -> Bool
229-
isInstanceMember statement = case statement of
230-
L1 (Py.SimpleStatement _) -> False
231-
R1 (Py.CompoundStatement compoundStatement) -> case compoundStatement of
232-
Prj (Py.FunctionDefinition {}) -> True
233-
_ -> False
234-
235-
isClassMember :: ClassBodyBinders a -> Bool
236-
isClassMember statement = case statement of
237-
L1 (Py.SimpleStatement simpleStatement) -> case simpleStatement of
238-
Prj (Py.ExpressionStatement _ expressions) -> all isAssignment expressions
239-
_ -> False
240-
R1 (Py.CompoundStatement compoundStatement) -> case compoundStatement of
241-
_ -> False
242-
243-
isAssignment :: (:+:)
244-
(Py.Expression :+: Py.Assignment)
245-
(Py.AugmentedAssignment :+: Py.Yield)
246-
a -> Bool
247-
isAssignment expressionStatement = case expressionStatement of
248-
Prj (Py.Assignment {}) -> True
249-
Prj (Py.AugmentedAssignment {}) -> True
250-
_ -> False
231+
isInstanceMember :: ClassBodyBinder a -> Bool
232+
isInstanceMember (Prj (Py.FunctionDefinition {})) = True
233+
isInstanceMember _ = False
234+
235+
isClassMember :: ClassBodyBinder a -> Bool
236+
isClassMember (Prj (Py.Assignment {})) = True
237+
isClassMember (Prj (Py.ClassDefinition {})) = True
238+
isClassMember (Prj (Py.DecoratedDefinition {})) = True
239+
isClassMember _ = False
251240

252241
instance ToScopeGraph Py.ConcatenatedString where
253242
type FocalPoint Py.ConcatenatedString a = BodyStruct a Stack.Node
@@ -318,8 +307,14 @@ instance ToScopeGraph Py.ExecStatement where
318307

319308
instance ToScopeGraph Py.ExpressionStatement where
320309
type FocalPoint Py.ExpressionStatement a = BodyStruct a [Stack.Node]
321-
scopeGraph x = do
322-
todo x
310+
scopeGraph (Py.ExpressionStatement _ statements) = do
311+
bindings <- for statements $ \stmt -> do
312+
res <- scopeGraph stmt
313+
let flattenEithers = fst . fromEither . bimap fromEither fromEither
314+
case res of
315+
Complete r -> pure (flattenEithers r)
316+
_ -> pure []
317+
pure (Complete (concat (toList bindings), []))
323318

324319
instance ToScopeGraph Py.ExpressionList where
325320
type FocalPoint Py.ExpressionList a = BodyStruct a [Stack.Node]
@@ -593,7 +588,12 @@ instance ToScopeGraph Py.PrimaryExpression where
593588

594589
instance ToScopeGraph Py.SimpleStatement where
595590
type FocalPoint Py.SimpleStatement a = BodyStruct a [Stack.Node]
596-
scopeGraph = todo
591+
scopeGraph (Py.SimpleStatement stmt) =
592+
fmap (either (either (either id fromEither)
593+
(either fromEither fromEither))
594+
(either (either fromEither fromEither)
595+
(either fromEither fromEither)))
596+
<$> (scopeGraph stmt)
597597

598598
instance ToScopeGraph Py.RaiseStatement where
599599
type FocalPoint Py.RaiseStatement a = BodyStruct a [Stack.Node]

0 commit comments

Comments
 (0)
0