on 4bb23bc help sign in
elk/compiler/Model/Libraries.hs
59 lines 2.2 kB history raw
1module Libraries (libraries, addPrelude) where
3import Ast
4import Control.Applicative ((<$>),(<*>))
5import qualified Data.Map as Map
6import Data.List (inits)
7import Text.JSON
8import LoadLibraries as Libs
10addPrelude :: Module -> Module
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 [ ("Cons", "a -> [a] -> [a]")
33 , ("Nil", "[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)