elm/compiler/Language/Elm.hs

46 lines
1.6 KiB
Haskell
Raw Normal View History

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