on cbecda2 help sign in
elk/elm/src/Compiler.hs
99 lines 3.7 kB history raw
1{-# LANGUAGE DeriveDataTypeable #-}
2module Main where
4import Data.Either (lefts, rights)
5import Data.List (intersect, intercalate)
6import Data.Maybe (fromMaybe)
7import System.Console.CmdArgs
8import Text.Blaze.Html.Renderer.String (renderHtml)
10import Ast
11import Initialize
12import CompileToJS
13import GenerateHtml
14import Paths_Elm
17data ELM =
18 ELM { make :: Bool
19 , files :: [FilePath]
20 , runtime :: Maybe FilePath
21 , separate_js :: Bool
22 , only_js :: Bool
23 , import_js :: [FilePath]
24 }
25 deriving (Data,Typeable,Show,Eq)
27elm = ELM { make = False &= help "automatically compile dependencies."
28 , files = def &= args &= typ "FILES"
29 , runtime = Nothing &= typFile &=
30 help "Specify a custom location for Elm's runtime system."
31 , separate_js = False &= help "Compile to separate HTML and JS files."
32 , only_js = False &= help "Compile only to JavaScript."
33 , import_js = [] &= typFile &= help "Include a JavaScript file before the body of the Elm program. Can be used many times. Files will be included in the given order."
34 } &=
35 help "Compile Elm programs to HTML, CSS, and JavaScript." &=
36 summary "The Elm Compiler v0.3.5, (c) Evan Czaplicki"
38main = do
39 args <- cmdArgs elm
40 mini <- getDataFileName "elm-runtime-0.3.5.js"
41 compileArgs mini args
43compileArgs mini (ELM _ [] _ _ _ _) =
44 putStrLn "Usage: elm [OPTIONS] [FILES]\nFor more help: elm --help"
45compileArgs mini (ELM make files rtLoc split only js) =
46 mapM_ (fileTo get what js $ fromMaybe mini rtLoc) files
47 where get = if make then getModules [] else getModule
48 what = if only then JS else
49 if split then Split else HTML
51data What = JS | HTML | Split
53fileTo get what jsFiles rtLoc file = do
54 ems <- get file
55 jss <- concat `fmap` mapM readFile jsFiles
56 case ems of
57 Left err -> putStrLn $ "Error while compiling " ++ file ++ ":\n" ++ err
58 Right ms ->
59 let name = reverse . tail . dropWhile (/='.') $ reverse file
60 js = name ++ ".js"
61 html = name ++ ".html"
62 in case what of
63 JS -> writeFile js $ jss ++ concatMap jsModule ms
64 HTML -> writeFile html . renderHtml $ modulesToHtml "" rtLoc jss ms
65 Split -> do
66 writeFile html . renderHtml $ linkedHtml rtLoc js ms
67 writeFile js $ jss ++ concatMap jsModule ms
69getModules :: [String] -> FilePath -> IO (Either String [Module])
70getModules uses file = do
71 code <- readFile file
72 case initialize code of
73 Left err -> return . Left $ "Error in " ++ file ++ ":\n" ++ err
74 Right modul@(Module _ _ imports _ _) ->
75 let imps = filter (`notElem` builtInModules) $ map fst imports in
76 case intersect uses imps of
77 x:_ -> return . Left $ "Error: Cyclic dependency. Module " ++
78 x ++ " depends on itself."
79 [] -> do
80 ems <- mapM (getModules (uses ++ imps) . toFilePath) imps
81 return $ case lefts ems of
82 [] -> Right $ concat (rights ems) ++ [modul]
83 errs -> Left $ intercalate "\n" errs
85getModule :: FilePath -> IO (Either String [Module])
86getModule file = do
87 code <- readFile file
88 return . fmap (:[]) $ initialize code
90toFilePath :: String -> FilePath
91toFilePath modul = map (\c -> if c == '.' then '/' else c) modul ++ ".elm"
93builtInModules =
94 concat [ map ("Data."++) [ "List", "Char", "Maybe" ]
95 , map ("Signal."++) [ "Mouse", "Keyboard.Raw"
96 , "Window", "Time", "HTTP", "Input", "Random" ]
97 , [ "Element", "Text", "Color", "Line" ]
98 , [ "Prelude" ]
99 ]