@@ -8,7 +8,7 @@ import Elara.Core.Analysis (exprType)
8
8
import Elara.Core.Generic (Bind (.. ))
9
9
import Elara.Data.Pretty
10
10
import Elara.Data.Unique
11
- import Elara.Logging (StructuredDebug , debugWith )
11
+ import Elara.Logging (StructuredDebug , debug , debugWith )
12
12
import Polysemy
13
13
14
14
{- | Convert a Core expression to ANF
@@ -26,12 +26,19 @@ becomes:
26
26
-- toANF :: Core.CoreExpr -> Sem r (ANF.Expr Core.Var)
27
27
-- toANF expr = (toANF' expr pure)
28
28
29
- type ToANF r = (Members [UniqueGen , StructuredDebug ] r , Pretty (Core. Expr Core. Var ))
29
+ type ToANF r =
30
+ ( Members [UniqueGen , StructuredDebug ] r
31
+ , Pretty (Core. Expr Core. Var )
32
+ , Pretty Core. Var
33
+ , Pretty (ANF. AExpr Core. Var )
34
+ , Pretty (ANF. CExpr Core. Var )
35
+ , Pretty (ANF. Expr Core. Var )
36
+ )
30
37
31
38
toANF :: ToANF r => Core. CoreExpr -> Sem r (ANF. Expr Core. Var )
32
39
toANF expr =
33
40
debugWith (" toANF " <> pretty expr) $
34
- toANF' expr (\ e -> pure ( ANF. CExpr $ ANF. AExpr e) )
41
+ toANFRec expr (pure . ANF. CExpr )
35
42
36
43
toANFCont :: ToANF r => Core. CoreExpr -> ContT (ANF. Expr Core. Var ) (Sem r ) (ANF. AExpr Core. Var )
37
44
toANFCont e = ContT $ \ k -> toANF' e k
@@ -78,10 +85,12 @@ toANFRec (Core.Match bind as cases) k = evalContT $ do
78
85
e' <- lift $ toANFRec e (pure . ANF. CExpr )
79
86
pure (con, bs, e')
80
87
k $ ANF. Match bind as cases'
81
- toANFRec (Core. Let (NonRecursive (b, e)) body) _ = evalContT $ do
88
+ toANFRec (Core. Let (NonRecursive (b, e)) body) k = evalContT $ do
89
+ lift $ debug $ " ToANF-ing" <> pretty b
82
90
e' <- toANFCont e
83
- body' <- toANFCont body
84
- pure $ ANF. Let (NonRecursive (b, ANF. AExpr e')) (ANF. CExpr $ ANF. AExpr body')
91
+ body' <- lift $ toANFRec body k
92
+ lift $ debug $ " Let " <> pretty b <> " = " <> pretty e' <> " in " <> pretty body'
93
+ pure $ ANF. Let (NonRecursive (b, ANF. AExpr e')) (body')
85
94
toANFRec (Core. Let (Recursive bs) body) _ = evalContT $ do
86
95
bs' <- for bs $ \ (b, e) -> do
87
96
e' <- toANFCont e
0 commit comments