on 1d2b3fc help sign in
elk/compiler/Model/Libraries.hs
64 lines 2.3 kB history raw
1module Libraries (libraries, addPrelude) where
3import Ast
4import qualified Data.Map as Map
5import Data.List (inits)
6import Text.JSON
7import LoadLibraries (docs)
9addPrelude :: Module -> Module
10addPrelude (Module name exs ims stmts) = Module name exs (customIms ++ ims) stmts
11 where customIms = concatMap addModule prelude
13 addModule (n, method) = case lookup n ims of
14 Nothing -> [(n, method)]
15 Just (As m) -> [(n, method)]
16 Just _ -> []
18prelude = text : map (\n -> (n, Hiding [])) modules
19 where
20 text = ("Graphics.Text", Hiding ["link", "color", "height"])
21 modules = [ "Prelude", "Signal", "List", "Maybe", "Time"
22 , "Graphics.Element", "Graphics.Color"
23 , "Graphics.Collage", "Graphics.Geometry" ]
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 docs :: Result (JSObject JSValue)
44 modules <- valFromObj "modules" obj :: Result [JSObject JSValue]
45 Map.fromList `fmap` mapM getValues modules
48getName :: JSObject JSValue -> Result String
49getName obj = valFromObj "name" obj
51getType :: JSObject JSValue -> Result String
52getType obj = valFromObj "type" obj
54getValue :: JSObject JSValue -> Result (String,String)
55getValue obj = do n <- getName obj
56 t <- getType obj
57 return (n,t)
59getValues :: JSObject JSValue -> Result (String, Map.Map String String)
60getValues obj = do
61 name <- getName obj
62 vs <- valFromObj "values" obj
63 vals <- mapM getValue vs
64 return (name, Map.fromList vals)