on master help sign in
Get the API for Language.Elm in better shape. Trying to get elm-lang.org working again.
commit1af2ff9a01eff9c4f875fbdf8718980a8b00f843 authorevancz <info@elm-lang.org> dateThu 4 Apr 2013, 10:37 (14 years ago) parent82f888c
3 files changed+29−16
compiler/Compiler.hs+1−8 compiler/Language/Elm.hs+19−7 compiler/Model/Libraries.hs+9−1
compiler/Compiler.hs+1−8
@@ -79,7 +79,7 @@ fileTo flags what rtLoc file = do
7979 let path = fromMaybe "" (output_directory flags) </> file
8080 js = replaceExtension path ".js"
8181 html = replaceExtension path ".html"
82− ms = if no_prelude flags then ms' else map addPrelude ms'
82+ ms = if no_prelude flags then ms' else map Libraries.addPrelude ms'
8383 txt = jss ++ concatMap jsModule ms
8484 in case what of
8585 JS -> writeFile js (formatJS txt)
@@ -88,10 +88,3 @@ fileTo flags what rtLoc file = do
8888 Split ->
8989 do writeFile html . renderHtml $ linkedHtml rtLoc js ms
9090 writeFile js (formatJS txt)
91−
92−addPrelude (Module name exs ims stmts) = Module name exs (prelude ++ ims) stmts
93− where prelude = concatMap addModule Libraries.prelude
94−
95− addModule (n, method) = case lookup n ims of
96− Nothing -> [(n, method)]
97− Just _ -> []
compiler/Language/Elm.hs+19−7
@@ -15,9 +15,13 @@
1515-}
1616module Language.Elm (
1717 compile, toHtml,
18− runtimeLocation
18+ moduleName,
19+ runtime, docs
1920 ) where
2021
22+import qualified Ast as Ast
23+import qualified Libraries as Libraries
24+import Data.List (intercalate)
2125import Data.Version (showVersion)
2226import CompileToJS
2327import ExtractNoscript
@@ -30,13 +34,17 @@ import Paths_Elm
3034import qualified Data.Text as TS
3135import qualified Data.Text.Lazy as TL
3236
33−-- |This function compiles Elm code to three separate parts: HTML, CSS,
34−-- and JavaScript. The HTML is only the contents of the body, so the three
35−-- parts must be combined in a basic HTML skeleton.
37+-- |This function compiles Elm code to JavaScript.
3638compile :: String -> String
37−compile source = either showErr jsModule modul
39+compile source = either showErr (jsModule . Libraries.addPrelude) modul
3840 where modul = buildFromSource source
3941
42+-- |This function extracts the module name of a given source program.
43+moduleName :: String -> String
44+moduleName source = either (const "Main") getName modul
45+ where modul = buildFromSource source
46+ getName (Ast.Module names _ _ _) = intercalate "." names
47+
4048-- |This function compiles Elm code into a full HTML page.
4149toHtml :: String -- ^ Location of elm-min.js as expected by the browser
4250 -> String -- ^ The page title
@@ -45,6 +53,10 @@ toHtml :: String -- ^ Location of elm-min.js as expected by the browser
4553toHtml = generateHtml
4654
4755-- |The absolute path to Elm's runtime system.
48−runtimeLocation :: IO FilePath
49−runtimeLocation = getDataFileName "elm-runtime.js"
56+runtime :: IO FilePath
57+runtime = getDataFileName "elm-runtime.js"
58+
59+-- |The absolute path to Elm's core library documentation.
60+docs :: IO FilePath
61+docs = getDataFileName "docs.json"
5062
compiler/Model/Libraries.hs+9−1
@@ -1,4 +1,4 @@
1−module Libraries (libraries, prelude) where
1+module Libraries (libraries, addPrelude, prelude) where
22
33import Ast
44import qualified Data.Map as Map
@@ -6,6 +6,14 @@ import Data.List (inits)
66import Text.JSON
77import LoadLibraries (docs)
88
9+
10+addPrelude (Module name exs ims stmts) = Module name exs (customIms ++ ims) stmts
11+ where customIms = concatMap addModule prelude
12+
13+ addModule (n, method) = case lookup n ims of
14+ Nothing -> [(n, method)]
15+ Just _ -> []
16+
917prelude = text : map (\n -> (n, Hiding [])) modules
1018 where
1119 text = ("Graphics.Text", Hiding ["link", "color"])