@@ -13,7 +13,8 @@ module Analysis.Syntax.Python
13
13
, parse
14
14
) where
15
15
16
- import Analysis.Effect.Domain hiding ((:>>>) )
16
+ import Analysis.Effect.Domain hiding ((:>>>) , (>>>) )
17
+ import qualified Analysis.Effect.Domain as D
17
18
import qualified Analysis.Effect.Statement as S
18
19
import Analysis.Name
19
20
import Analysis.Reference
@@ -22,10 +23,9 @@ import Analysis.VM
22
23
import Control.Effect.Labelled
23
24
import Control.Effect.Reader
24
25
import Data.Function (fix )
25
- import Data.List.NonEmpty (NonEmpty )
26
- import Data.Text (Text )
26
+ import Data.List.NonEmpty (NonEmpty ( .. ), nonEmpty )
27
+ import Data.Text (Text , pack )
27
28
import qualified Language.Python.Common.AST as Py
28
- import qualified Language.Python.Common.SrcLocation as Py
29
29
import Language.Python.Version3.Parser
30
30
import Source.Span (Span )
31
31
import System.FilePath (takeBaseName )
@@ -76,7 +76,7 @@ eval eval = \case
76
76
t :>> u -> do
77
77
t' <- eval t
78
78
u' <- eval u
79
- t' >>> u'
79
+ t' D. >>> u'
80
80
Import ns -> S. simport ns >> dunit
81
81
Function n ps b -> letrec n (dabs ps (foldr (\ (p, a) m -> let' p a m) (eval b) . zip ps))
82
82
Call f as -> do
@@ -87,12 +87,27 @@ eval eval = \case
87
87
where
88
88
setSpan s r = r{ refSpan = s }
89
89
90
+ (>>>) :: T. Term Python v -> T. Term Python v -> T. Term Python v
91
+ l >>> r = T. Term (l :>> r)
92
+
93
+ noop :: T. Term Python v
94
+ noop = T. Term Noop
95
+
90
96
91
97
-- Parsing
92
98
93
- parse :: FilePath -> IO (Py. Module Py. SrcSpan )
99
+ parse :: FilePath -> IO (T. Term Python Name )
94
100
parse path = do
95
101
src <- readFile path
96
102
case parseModule src (takeBaseName path) of
97
- Left err -> fail (show err)
98
- Right (m, _) -> pure m
103
+ Left err -> fail (show err)
104
+ Right (Py. Module ss, _) -> foldr (>>>) noop <$> traverse statement ss
105
+ where
106
+ statement :: Py. Statement annot -> IO (T. Term Python Name )
107
+ statement = \ case
108
+ Py. Import is _ -> foldr ((>>>) . T. Term . Import ) noop <$> traverse importItem is
109
+ _ -> fail " cannot ingest this Python statement"
110
+ ident :: Py. Ident annot -> String
111
+ ident (Py. Ident s _) = s
112
+ importItem :: Py. ImportItem annot -> IO (NonEmpty Text )
113
+ importItem Py. ImportItem { Py. import_item_name = ns } = maybe (fail " " ) pure (nonEmpty (map (pack . ident) ns)) -- FIXME: "as" names
0 commit comments