summaryrefslogtreecommitdiff
path: root/src/LambdaCube/Compiler
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-05-13 22:48:03 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-05-13 22:48:17 +0200
commit8f2e602cada9141b653802cf1084b9bdfd6e0d06 (patch)
treec1351e9fcb0341af482f8da4a8859e2046445188 /src/LambdaCube/Compiler
parent8ac42fa1bccb554de833ea7d8070cb5112e01aee (diff)
refactoring & fix build
Diffstat (limited to 'src/LambdaCube/Compiler')
-rw-r--r--src/LambdaCube/Compiler/DesugaredSource.hs12
-rw-r--r--src/LambdaCube/Compiler/Infer.hs6
-rw-r--r--src/LambdaCube/Compiler/InferMonad.hs12
-rw-r--r--src/LambdaCube/Compiler/Statements.hs14
4 files changed, 34 insertions, 10 deletions
diff --git a/src/LambdaCube/Compiler/DesugaredSource.hs b/src/LambdaCube/Compiler/DesugaredSource.hs
index 72327b99..3982d897 100644
--- a/src/LambdaCube/Compiler/DesugaredSource.hs
+++ b/src/LambdaCube/Compiler/DesugaredSource.hs
@@ -78,14 +78,18 @@ data FileInfo = FileInfo
78instance Eq FileInfo where (==) = (==) `on` fileId 78instance Eq FileInfo where (==) = (==) `on` fileId
79instance Ord FileInfo where compare = compare `on` fileId 79instance Ord FileInfo where compare = compare `on` fileId
80 80
81instance PShow FileInfo where pShow = text . (++ ".lc") . fileModule 81instance PShow FileInfo where pShow = text . filePath --(++ ".lc") . fileModule
82 82
83showPos :: FileInfo -> SPos -> Doc 83showPos :: FileInfo -> SPos -> Doc
84showPos n p = pShow n <> ":" <> pShow p 84showPos n p = pShow n <> ":" <> pShow p
85 85
86-------------------------------------------------------------------------------- range 86-------------------------------------------------------------------------------- range
87 87
88data Range = Range !FileInfo !SPos !SPos 88data Range = Range
89 { rangeFile :: !FileInfo
90 , rangeStart :: !SPos
91 , rangeStop :: !SPos
92 }
89 deriving (Eq, Ord) 93 deriving (Eq, Ord)
90 94
91instance Show Range where show = ppShow 95instance Show Range where show = ppShow
@@ -96,6 +100,8 @@ instance PShow Range
96 : map text (drop (r - 1) $ take r' $ lines $ fileContent n) 100 : map text (drop (r - 1) $ take r' $ lines $ fileContent n)
97 ++ [text $ replicate (c - 1) ' ' ++ replicate (c' - c) '^' | r' == r] 101 ++ [text $ replicate (c - 1) ' ' ++ replicate (c' - c) '^' | r' == r]
98 102
103showRangeWithoutFileName (Range _ b e) = pShow b <> "-" <> pShow e
104
99joinRange :: Range -> Range -> Range 105joinRange :: Range -> Range -> Range
100joinRange (Range n b e) (Range n' b' e') {- | n == n' -} = Range n (min b b') (max e e') 106joinRange (Range n b e) (Range n' b' e') {- | n == n' -} = Range n (min b b') (max e e')
101 107
@@ -553,7 +559,7 @@ instance PShow Stmt where
553 559
554instance DeBruijnify SIName Stmt where 560instance DeBruijnify SIName Stmt where
555 deBruijnify_ k v = \case 561 deBruijnify_ k v = \case
556 StLet sn mt e -> StLet sn (deBruijnify_ k v <$> mt) (deBruijnify_ k v e) 562 StmtLet sn e -> StmtLet sn (deBruijnify_ k v e)
557 x@PrecDef{} -> x 563 x@PrecDef{} -> x
558 x -> error $ "deBruijnify @ " ++ ppShow x 564 x -> error $ "deBruijnify @ " ++ ppShow x
559 565
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs
index c2657f0e..ecd18b46 100644
--- a/src/LambdaCube/Compiler/Infer.hs
+++ b/src/LambdaCube/Compiler/Infer.hs
@@ -271,6 +271,12 @@ inferN_ tellTrace = infer where
271 = infer te $ x `SAppV` SLamV (SLamV (STyped (subst (n'+2) (Var 1) $ up1_ (n'+3) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` SVar siv v 271 = infer te $ x `SAppV` SLamV (SLamV (STyped (subst (n'+2) (Var 1) $ up1_ (n'+3) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` SVar siv v
272-} 272-}
273 | SRHS x <- e = checkN (ERHS te) x t 273 | SRHS x <- e = checkN (ERHS te) x t
274{- TODO
275 | SAnn v a <- e = do
276 let x = t
277 let same = checkSame te a x
278 if same then checkN te v x else error $ "checkSame:\n" ++ ppShow a ++ "\nwith\n" ++ showEnvExp te (ET x TType)
279-}
274 | SLHS n x <- e = checkN (ELHS n te) x t 280 | SLHS n x <- e = checkN (ELHS n te) x t
275 | SApp_ si h a b <- e = infer (CheckAppType si h t te b) a 281 | SApp_ si h a b <- e = infer (CheckAppType si h t te b) a
276 | SLam h a b <- e, Pi h' x y <- t, h == h' = do 282 | SLam h a b <- e, Pi h' x y <- t, h == h' = do
diff --git a/src/LambdaCube/Compiler/InferMonad.hs b/src/LambdaCube/Compiler/InferMonad.hs
index b084647f..e2895389 100644
--- a/src/LambdaCube/Compiler/InferMonad.hs
+++ b/src/LambdaCube/Compiler/InferMonad.hs
@@ -17,6 +17,7 @@ module LambdaCube.Compiler.InferMonad where
17 17
18import Data.Monoid 18import Data.Monoid
19import Data.List 19import Data.List
20import Data.Maybe
20import qualified Data.Set as Set 21import qualified Data.Set as Set
21import qualified Data.Map as Map 22import qualified Data.Map as Map
22 23
@@ -80,15 +81,16 @@ throwError' e = tell [IError e] >> throwError e
80mkInfoItem (RangeSI r) i = [Info r i] 81mkInfoItem (RangeSI r) i = [Info r i]
81mkInfoItem _ _ = mempty 82mkInfoItem _ _ = mempty
82 83
83listAllInfos m = h "trace" (listTraceInfos m) 84listAllInfos f m
84 ++ h "tooltips" [ nest 4 $ shortForm $ pShow r <$$> hsep (intersperse "|" is) | (r, is) <- listTypeInfos m ] 85 = h "trace" (listTraceInfos m) ++ listAllInfos' f m
85 ++ h "warnings" [ pShow w | ParseWarning w <- m ]
86 where 86 where
87 h x [] = [] 87 h x [] = []
88 h x xs = ("------------" <+> x) : xs 88 h x xs = ("------------" <+> x) : xs
89 89
90listAllInfos' m = h "tooltips" [ nest 4 $ shortForm $ pShow r <$$> hsep (intersperse "|" is) | (r, is) <- listTypeInfos m ] 90listAllInfos' f m
91 ++ h "warnings" [ pShow w | ParseWarning w <- m ] 91 = h "tooltips" [ nest 4 $ shortForm $ showRangeWithoutFileName r <$$> hsep (intersperse "|" is)
92 | (r, is) <- listTypeInfos m, maybe False (rangeFile r ==) f ]
93 ++ h "warnings" [ pShow w | ParseWarning w <- m ]
92 where 94 where
93 h x [] = [] 95 h x [] = []
94 h x xs = ("------------" <+> x) : xs 96 h x xs = ("------------" <+> x) : xs
diff --git a/src/LambdaCube/Compiler/Statements.hs b/src/LambdaCube/Compiler/Statements.hs
index 01add12c..3cd1edf1 100644
--- a/src/LambdaCube/Compiler/Statements.hs
+++ b/src/LambdaCube/Compiler/Statements.hs
@@ -49,7 +49,7 @@ mkLets_ mkLet = mkLets' mkLet . concatMap desugarMutual . sortDefs
49 49
50mkLets' mkLet = f where 50mkLets' mkLet = f where
51 f [] e = e 51 f [] e = e
52 f (StLet n mt x: ds) e = mkLet n (maybe id (flip SAnn) mt x) (deBruijnify [n] $ f ds e) 52 f (StmtLet n x: ds) e = mkLet n x (deBruijnify [n] $ f ds e)
53 f (PrecDef{}: ds) e = f ds e 53 f (PrecDef{}: ds) e = f ds e
54 f (x: ds) e = error $ "mkLets: " ++ ppShow x 54 f (x: ds) e = error $ "mkLets: " ++ ppShow x
55 55
@@ -106,7 +106,7 @@ compileStmt lhs compilegt ds = \case
106 | n `elem` [n' | TypeFamily n' _ <- ds] -> return [] 106 | n `elem` [n' | TypeFamily n' _ <- ds] -> return []
107 | otherwise -> do 107 | otherwise -> do
108 cf <- compilegt (SIName_ (mconcat [sourceInfo n | FunAlt n _ _ <- fs]) (nameFixity n) $ sName n) vs [gt | FunAlt _ _ gt <- fs] 108 cf <- compilegt (SIName_ (mconcat [sourceInfo n | FunAlt n _ _ <- fs]) (nameFixity n) $ sName n) vs [gt | FunAlt _ _ gt <- fs]
109 return [StLet n (listToMaybe [t | TypeAnn n' t <- ds, n' == n]) $ lhs n cf] 109 return [StLet n (listToMaybe [t | TypeAnn n' t <- ds, n' == n]{-TODO: fail if more-}) $ lhs n cf]
110 fs -> fail $ "different number of arguments of " ++ sName n ++ ":\n" ++ show (vcat $ pShow . sourceInfo . snd . head <$> fs) 110 fs -> fail $ "different number of arguments of " ++ sName n ++ ":\n" ++ show (vcat $ pShow . sourceInfo . snd . head <$> fs)
111 [Stmt x] -> return [x] 111 [Stmt x] -> return [x]
112 where 112 where
@@ -127,6 +127,7 @@ desugarValueDef p e = sequence
127 dns = reverse $ getPVars p 127 dns = reverse $ getPVars p
128 n = mangleNames dns 128 n = mangleNames dns
129 129
130--getLet (StmtLet x dx) = Just (x, dx)
130getLet (StLet x mt dx) = Just (x, mt, dx) 131getLet (StLet x mt dx) = Just (x, mt, dx)
131getLet _ = Nothing 132getLet _ = Nothing
132 133
@@ -137,10 +138,14 @@ desugarMutual [x@Primitive{}] = [x]
137desugarMutual [x@Data{}] = [x] 138desugarMutual [x@Data{}] = [x]
138desugarMutual [x@PrecDef{}] = [x] 139desugarMutual [x@PrecDef{}] = [x]
139desugarMutual [StLet n nt nd] = [StLet n nt $ addFix n nt nd] 140desugarMutual [StLet n nt nd] = [StLet n nt $ addFix n nt nd]
141--desugarMutual [StmtLet n nd] = [StmtLet n $ addFix n nd]
140desugarMutual (traverse getLet -> Just (unzip3 -> (ns, ts, ds))) = fst' $ runWriter $ do 142desugarMutual (traverse getLet -> Just (unzip3 -> (ns, ts, ds))) = fst' $ runWriter $ do
143--desugarMutual (traverse getLet -> Just (unzip -> (ns, ds))) = fst' $ runWriter $ do
141 ss <- compileStmt'_ sLHS SRHS SRHS =<< desugarValueDef (foldr cHCons cHNil $ PVarSimp <$> ns) (SGlobal xy) 144 ss <- compileStmt'_ sLHS SRHS SRHS =<< desugarValueDef (foldr cHCons cHNil $ PVarSimp <$> ns) (SGlobal xy)
142 return $ 145 return $
146-- StLet xy ty (addFix xy $ mkLets' SLet ss $ foldr HCons HNil ds) : ss
143 StLet xy ty (addFix xy ty $ mkLets' SLet ss $ foldr HCons HNil ds) : ss 147 StLet xy ty (addFix xy ty $ mkLets' SLet ss $ foldr HCons HNil ds) : ss
148
144 where 149 where
145 ty = Nothing -- TODO: Just $ HList $ foldr BCons BNil $ const (Wildcard SType) <$> ts 150 ty = Nothing -- TODO: Just $ HList $ foldr BCons BNil $ const (Wildcard SType) <$> ts
146 xy = mangleNames ns 151 xy = mangleNames ns
@@ -148,6 +153,11 @@ desugarMutual xs = error "desugarMutual"
148 153
149addFix n nt x 154addFix n nt x
150 | usedS n x = SBuiltin FprimFix `SAppV` SLam Visible (maybe (Wildcard SType) id nt) (deBruijnify [n] x) 155 | usedS n x = SBuiltin FprimFix `SAppV` SLam Visible (maybe (Wildcard SType) id nt) (deBruijnify [n] x)
156
157{-
158addFix n x
159 | usedS n x = SBuiltin FprimFix `SAppV` SLamV (deBruijnify [n] x)
160-}
151 | otherwise = x 161 | otherwise = x
152 162
153mangleNames xs = SIName (foldMap sourceInfo xs) $ "_" ++ intercalate "_" (sName <$> xs) 163mangleNames xs = SIName (foldMap sourceInfo xs) $ "_" ++ intercalate "_" (sName <$> xs)