on a5bc960 help sign in
elk/compiler/Metadata/Libraries.hs
59 lines 2.2 kB history raw
1module Metadata.Libraries (libraries, addPrelude) where
3import Control.Applicative ((<$>),(<*>))
4import qualified Data.Map as Map
5import Data.List (inits)
6import Text.JSON
7import Metadata.LoadLibraries as Libs
8import SourceSyntax.Module
10addPrelude :: Module t v -> Module t v
11addPrelude (Module name exs ims stmts) = Module name exs (customIms ++ ims) stmts
12 where customIms = concatMap addModule prelude
14 addModule (n, method) = case lookup n ims of
15 Nothing -> [(n, method)]
16 Just (As m) -> [(n, method)]
17 Just _ -> []
19prelude = text : map (\n -> (n, Hiding [])) modules
20 where
21 text = ("Text", Hiding ["link", "color", "height"])
22 modules = [ "Prelude", "Signal", "List", "Maybe", "Time"
23 , "Graphics.Element", "Color", "Graphics.Collage" ]
25libraries :: Map.Map String (Map.Map String String)
26libraries =
27 case getLibs of
28 Error err -> error err
29 Ok libs -> Map.unionWith Map.union libs nilAndTuples
30 where nilAndTuples = Map.singleton "Prelude" (Map.fromList pairs)
31 pairs =
32 [ ("::", "a -> [a] -> [a]")
33 , ("[]", "[a]")
34 ] ++ map makeTuple (inits ['a'..'i'])
36 makeTuple cs =
37 let name = "_Tuple" ++ show (length cs)
38 in (name, concatMap (\c -> c : " -> ") cs ++
39 name ++ concatMap (\c -> [' ',c]) cs)
41getLibs :: Result (Map.Map String (Map.Map String String))
42getLibs = do
43 obj <- decodeStrict Libs.docs :: Result (JSObject JSValue)
44 modules <- valFromObj "modules" obj :: Result [JSObject JSValue]
45 Map.fromList `fmap` mapM getValues modules
48get :: String -> JSObject JSValue -> Result String
49get = valFromObj
51getValue :: JSObject JSValue -> Result (String,String)
52getValue obj = (,) <$> get "name" obj <*> get "type" obj
54getValues :: JSObject JSValue -> Result (String, Map.Map String String)
55getValues obj = do
56 name <- get "name" obj
57 vs <- valFromObj "values" obj
58 vals <- mapM getValue vs
59 return (name, Map.fromList vals)