You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 1, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: semantic-analysis/src/Analysis/Syntax/Python.hs
+23-1Lines changed: 23 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,7 @@
4
4
moduleAnalysis.Syntax.Python
5
5
( -- * Syntax
6
6
Term(..)
7
+
, subterms
7
8
-- * Abstract interpretation
8
9
, eval0
9
10
, eval
@@ -23,6 +24,7 @@ import Data.Foldable (for_)
23
24
importData.Function (fix)
24
25
importData.List.NonEmpty (nonEmpty)
25
26
importData.Maybe (mapMaybe)
27
+
importqualifiedData.SetasSet
26
28
importData.Text (pack)
27
29
importqualifiedLanguage.Python.CommonasPy
28
30
importLanguage.Python.Version3.Parser
@@ -37,6 +39,25 @@ data Term
37
39
| Expr (Py.ExprPy.SrcSpan)
38
40
deriving (Eq, Ord, Show)
39
41
42
+
--| Non-generic production of the recursive set of subterms.
43
+
--
44
+
-- This should be exactly the set of nodes which 'eval' can visit, i.e. it excludes comments, etc.
45
+
subterms::Term->Set.SetTerm
46
+
subterms t =Set.insert t $case t of
47
+
Module (Py.Module ss) -> suite ss
48
+
Statement (Py.Conditional cts e _) ->foldMap (\ (c, t) -> subterms (Expr c) <> suite t) cts <> suite e
49
+
Statement (Py.Raise (Py.RaiseV3 e) _) ->maybeSet.empty (subterms .Expr.fst) e
50
+
-- FIXME: Py.RaiseV2
51
+
-- FIXME: whatever the second field is
52
+
Statement (Py.StmtExpr e _) -> subterms (Expr e)
53
+
Statement (Py.Fun _ _ _ ss _) -> suite ss
54
+
-- FIXME: include 'subterms' of any default values
55
+
Expr (Py.Call f as _) -> subterms (Expr f) <>foldMap (\case { Py.ArgExpr e _ -> subterms (Expr e) ; _ ->Set.empty }) as
56
+
-- FIXME: support keyword args &c.
57
+
_ ->Set.empty -- TBD, and terminals
58
+
where
59
+
suite =foldMap (subterms .Statement)
60
+
40
61
41
62
-- Abstract interpretation
42
63
@@ -61,7 +82,8 @@ eval eval = \case
61
82
Statement (Py.Raise (Py.RaiseV3 e) sp) -> setSpan sp $case e of
62
83
Just (e, _) -> eval (Expr e) >>= ddie -- FIXME: from clause
63
84
Nothing-> dunit >>= ddie
64
-
-- FIXME: RaiseV2
85
+
-- FIXME: RaiseV2
86
+
-- FIXME: whatever the second field is
65
87
Statement (Py.StmtExpr e sp) -> setSpan sp (eval (Expr e))
66
88
Statement (Py.Fun n ps _r ss sp) ->let ps' = mapMaybe (\ p ->case p of { Py.Param n _ _ _ ->Just (ident n) ; _ ->Nothing}) ps in setSpan sp $ letrec (ident n) (dabs ps' (foldr (\ (p, a) m -> let' p a m) (suite ss) .zip ps'))
67
89
Expr (Py.Var n sp) -> setSpan sp $let n' = ident n in lookupEnv n' >>=maybe (dvar n') fetch
0 commit comments