10000 Subterms. · github/semantic@e4cf5c1 · 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 e4cf5c1

Browse files
committed
Subterms.
😭 non-generic.
1 parent b88c143 commit e4cf5c1

File tree

1 file changed

+23
-1
lines changed
  • semantic-analysis/src/Analysis/Syntax

1 file changed

+23
-1
lines changed

semantic-analysis/src/Analysis/Syntax/Python.hs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module Analysis.Syntax.Python
55
( -- * Syntax
66
Term(..)
7+
, subterms
78
-- * Abstract interpretation
89
, eval0
910
, eval
@@ -23,6 +24,7 @@ import Data.Foldable (for_)
2324
import Data.Function (fix)
2425
import Data.List.NonEmpty (nonEmpty)
2526
import Data.Maybe (mapMaybe)
27+
import qualified Data.Set as Set
2628
import Data.Text (pack)
2729
import qualified Language.Python.Common as Py
2830
import Language.Python.Version3.Parser
@@ -37,6 +39,25 @@ data Term
3739
| Expr (Py.Expr Py.SrcSpan)
3840
deriving (Eq, Ord, Show)
3941

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.Set Term
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) _) -> maybe Set.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+
4061

4162
-- Abstract interpretation
4263

@@ -61,7 +82,8 @@ eval eval = \case
6182
Statement (Py.Raise (Py.RaiseV3 e) sp) -> setSpan sp $ case e of
6283
Just (e, _) -> eval (Expr e) >>= ddie -- FIXME: from clause
6384
Nothing -> dunit >>= ddie
64-
-- FIXME: RaiseV2
85+
-- FIXME: RaiseV2
86+
-- FIXME: whatever the second field is
6587
Statement (Py.StmtExpr e sp) -> setSpan sp (eval (Expr e))
6688
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'))
6789
Expr (Py.Var n sp) -> setSpan sp $ let n' = ident n in lookupEnv n' >>= maybe (dvar n') fetch

0 commit comments

Comments
 (0)
0