elm/compiler/Model/Libraries.hs
evancz 7dbb052b22 Load json representation of types with Template Haskell, so library files are the base truth of type information.
Make minor fixes in libraries, such as adding the Char labrary and fixing the types in the Dict library.
2013-03-16 22:24:18 -07:00

37 lines
No EOL
1 KiB
Haskell

{-# LANGUAGE TemplateHaskell #-}
module Libraries (libraries) where
import qualified Data.Map as Map
import Text.JSON
import LoadLibraries
libraries :: Map.Map String (Map.Map String String)
libraries = case getLibs of
Ok libs -> libs
Error err -> error err
getLibs :: Result (Map.Map String (Map.Map String String))
getLibs = do
obj <- decodeStrict $(docs) :: Result (JSObject JSValue)
modules <- valFromObj "modules" obj :: Result [JSObject JSValue]
Map.fromList `fmap` mapM getValues modules
getName :: JSObject JSValue -> Result String
getName obj = valFromObj "name" obj
getType :: JSObject JSValue -> Result String
getType obj = valFromObj "type" obj
getValue :: JSObject JSValue -> Result (String,String)
getValue obj = do n <- getName obj
t <- getType obj
return (n,t)
getValues :: JSObject JSValue -> Result (String, Map.Map String String)
getValues obj = do
name <- getName obj
vs <- valFromObj "values" obj
vals <- mapM getValue vs
return (name, Map.fromList vals)