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

Commit cd62a56

Browse files
committed
Conditionals.
1 parent 5e88d14 commit cd62a56

File tree

1 file changed

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

1 file changed

+11
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ l >>> r = T.Term (l :>> r)
9393
noop :: T.Term Python v
9494
noop = T.Term Noop
9595

96+
iff :: T.Term Python v -> T.Term Python v -> T.Term Python v -> T.Term Python v
97+
iff c t e = T.Term (Iff c t e)
98+
9699

97100
-- Parsing
98101

@@ -101,13 +104,20 @@ parse path = do
101104
src <- readFile path
102105
case parseModule src (takeBaseName path) of
103106
Left err -> fail (show err)
104-
Right (Py.Module ss, _) -> foldr (>>>) noop <$> traverse statement ss
107+
Right (Py.Module ss, _) -> suite ss
105108
where
106109
statement :: Py.Statement annot -> IO (T.Term Python Name)
107110
statement = \case
108111
Py.Import is _ -> foldr ((>>>) . T.Term . Import) noop <$> traverse importItem is
112+
Py.Conditional cs e _ -> foldr (\ (c, t) e -> iff <$> expr c <*> suite t <*> e) (suite e) cs
109113
_ -> fail "cannot ingest this Python statement"
114+
expr :: Py.Expr annot -> IO (T.Term Python Name)
115+
expr = \case
116+
Py.Var v _ -> pure (T.Var (name (pack (ident v))))
117+
_ -> fail "cannot ingest this Python expression"
110118
ident :: Py.Ident annot -> String
111119
ident (Py.Ident s _) = s
112120
importItem :: Py.ImportItem annot -> IO (NonEmpty Text)
113121
importItem Py.ImportItem{ Py.import_item_name = ns } = maybe (fail "") pure (nonEmpty (map (pack . ident) ns)) -- FIXME: "as" names
122+
suite :: [Py.Statement annot] -> IO (T.Term Python Name)
123+
suite ss = foldr (>>>) noop <$> traverse statement ss

0 commit comments

Comments
 (0)