@@ -1,9 +1,103 @@
1+{-# LANGUAGE PatternGuards #-}
1module IRTS.Exports(findExports) where
2module IRTS.Exports(findExports) where
3import Idris.AbsSyntax
4import Idris.AbsSyntax
6+import Idris.Core.CaseTree
7+import Idris.Core.Evaluate
4import IRTS.Lang
9import IRTS.Lang
6findExports :: Idris [ExportIFace]
11findExports :: Idris [ExportIFace]
7findExports = do exps <- getExports
12findExports = do exps <- getExports
13+ es <- mapM toIFace exps
14+ logLvl 2 $ "Exporting " ++ show es
8− return []
15+ return es
17+toIFace :: Name -> Idris ExportIFace
18+toIFace n = do i <- getIState
20+ def <- case lookupDefExact n ctxt of
21+ Just (CaseOp _ _ _ _ _ cs)
22+ -> getExpList (snd (cases_compiletime cs))
23+ Nothing -> ifail "Can't happen [toIFace]"
24+ case lookupTyExact n ctxt of
25+ Just ty -> toIFaceTyVal ty def
26+ Nothing -> ifail "Can't happen [toIFace]"
28+ getExpList (STerm t) = return t
29+ getExpList _ = ifail "Badly formed export list"
31+toIFaceTyVal :: Type -> Term -> Idris ExportIFace
33+ | (P _ exp _, [P _ ffi _, Constant (Str hdr), _]) <- unApply ty
34+ = do tm' <- toIFaceVal tm
35+ return $ Export ffi hdr tm'
37+toIFaceVal :: Term -> Idris [Export]
39+ | (P _ end _, _) <- unApply tm,
40+ end == sNS (sUN "End") ["FFI_Export"] = return []
41+ | (P _ fun _, [_,_,_,_,(P _ fn _),extnm,prf,rest]) <- unApply tm,
42+ fun == sNS (sUN "Fun") ["FFI_Export"]
43+ = do rest' <- toIFaceVal rest
45+ ExportFun fn (toFDesc extnm) (toFDescRet prf) (toFDescArgs prf)
47+ | (P _ dat _, [_,_,_,_,d,rest]) <- unApply tm,
48+ dat == sNS (sUN "Data") ["FFI_Export"]
49+ = do rest' <- toIFaceVal rest
50+ return $ ExportData (toFDesc d) : rest'
51+ | otherwise = ifail $ "Badly formed export list " ++ show tm
53+toFDesc :: Term -> FDesc
54+toFDesc (Constant (Str str)) = FStr str
56+ | (P _ n _, []) <- unApply tm = FCon (deNS n)
57+ | (P _ n _, as) <- unApply tm = FApp (deNS n) (map toFDesc as)
60+toFDescRet :: Term -> FDesc
62+ | (P _ ret _, [_,_,_,b]) <- unApply tm,
63+ ret == sNS (sUN "FFI_Ret") ["FFI_Export"]
65+ | (P _ io _, [_,_,_,b]) <- unApply tm,
66+ io == sNS (sUN "FFI_IO") ["FFI_Export"]
68+ | (P _ fun _, [_,_,_,_,_,t]) <- unApply tm,
69+ fun == sNS (sUN "FFI_Fun") ["FFI_Export"]
71+ | otherwise = error "Badly formed export proof"
73+toFDescBase :: Term -> FDesc
75+ | (P _ prim _, [_, _, _, pdesc]) <- unApply tm,
76+ prim == sNS (sUN "FFI_Prim") ["FFI_Export"]
78+ | (P _ prim _, [_, _, _, ddesc, _]) <- unApply tm,
79+ prim == sNS (sUN "FFI_ExpType") ["FFI_Export"]
81+ | otherwise = error "Badly formed export type"
83+toFDescArgs :: Term -> [FDesc]
85+ | (P _ io _, [_,_,_,b]) <- unApply tm,
86+ io == sNS (sUN "FFI_IO") ["FFI_Export"]
87+ = [FWorld] -- World, unused but needed explicitly as an argument
88+ | (P _ fun _, [_,_,_,_,b,t]) <- unApply tm,
89+ fun == sNS (sUN "FFI_Fun") ["FFI_Export"]
90+ = toFDescBase b : toFDescArgs t
93+toFDescPrim (Constant (Str str)) = FStr str
95+ | (P _ n _, []) <- unApply tm = FCon (deNS n)
96+ | (P _ n _, as) <- unApply tm = FApp (deNS n) (map toFDescPrim as)
97+toFDescPrim _ = FUnknown