2013-12-11 05:57:26 +00:00
|
|
|
{- | This module exports functions for compiling Elm to JS
|
2013-09-28 19:39:52 +00:00
|
|
|
and some utilities for making it easier to find some Elm-related files.
|
2012-05-28 16:33:15 +00:00
|
|
|
|
2013-12-11 05:57:26 +00:00
|
|
|
Learning resources for Elm are available at
|
|
|
|
<http://elm-lang.org.elm/Learn.elm> and many interactive examples are
|
2013-03-11 01:52:16 +00:00
|
|
|
available at <http://elm-lang.org/Examples.elm>
|
2012-05-28 16:02:02 +00:00
|
|
|
-}
|
2013-12-11 05:56:59 +00:00
|
|
|
module Language.Elm (compile, moduleName, runtime, docs) where
|
2013-05-28 13:47:57 +00:00
|
|
|
|
2013-07-21 04:08:08 +00:00
|
|
|
import qualified Data.List as List
|
2013-12-15 07:29:39 +00:00
|
|
|
import qualified Generate.JavaScript as JS
|
|
|
|
import qualified Build.Source as Source
|
|
|
|
import qualified Parse.Module as Parser
|
|
|
|
import qualified SourceSyntax.Module as M
|
2013-07-16 19:42:37 +00:00
|
|
|
import qualified Text.PrettyPrint as P
|
2013-07-31 16:31:48 +00:00
|
|
|
import qualified Metadata.Prelude as Prelude
|
2013-12-15 07:29:39 +00:00
|
|
|
import qualified Paths_Elm as This
|
2013-12-11 05:56:59 +00:00
|
|
|
import System.IO.Unsafe
|
2012-05-29 19:48:24 +00:00
|
|
|
|
2013-06-06 23:11:23 +00:00
|
|
|
-- |This function compiles Elm code to JavaScript. It will return either
|
|
|
|
-- an error message or the compiled JS code.
|
2013-12-11 05:56:59 +00:00
|
|
|
compile :: String -> Either String String
|
|
|
|
compile source =
|
2013-12-15 07:29:39 +00:00
|
|
|
case Source.build False interfaces source of
|
2013-12-11 05:56:59 +00:00
|
|
|
Left docs -> Left . unlines . List.intersperse "" $ map P.render docs
|
2013-12-15 07:29:39 +00:00
|
|
|
Right modul -> Right $ JS.generate modul
|
2013-11-11 15:22:33 +00:00
|
|
|
|
2013-12-11 05:56:59 +00:00
|
|
|
{-# NOINLINE interfaces #-}
|
2013-12-15 07:29:39 +00:00
|
|
|
interfaces :: M.Interfaces
|
2013-12-11 05:56:59 +00:00
|
|
|
interfaces = unsafePerformIO $ Prelude.interfaces
|
2013-03-21 09:29:23 +00:00
|
|
|
|
2013-04-04 17:37:43 +00:00
|
|
|
-- |This function extracts the module name of a given source program.
|
2013-06-06 23:11:23 +00:00
|
|
|
moduleName :: String -> Maybe String
|
2013-12-15 07:29:39 +00:00
|
|
|
moduleName = Parser.getModuleName
|
2013-04-04 17:37:43 +00:00
|
|
|
|
2012-10-03 19:31:38 +00:00
|
|
|
-- |The absolute path to Elm's runtime system.
|
2013-04-04 17:37:43 +00:00
|
|
|
runtime :: IO FilePath
|
2013-12-15 07:29:39 +00:00
|
|
|
runtime = This.getDataFileName "elm-runtime.js"
|
2013-04-04 17:37:43 +00:00
|
|
|
|
|
|
|
-- |The absolute path to Elm's core library documentation.
|
|
|
|
docs :: IO FilePath
|
2013-12-15 07:29:39 +00:00
|
|
|
docs = This.getDataFileName "docs.json"
|