on 43d4388 help sign in
elk/src/Compile.hs
60 lines 1.8 kB history raw
1module Compile (compile) where
3import qualified Data.Map as Map
5import qualified AST.Module as Module
6import qualified Canonicalize
7import Elm.Utils ((|>))
8import qualified Nitpick.TopLevelTypes as Nitpick
9import qualified Parse.Helpers as Parse
10import qualified Parse.Parse as Parse
11import qualified Reporting.Error as Error
12import qualified Reporting.Result as Result
13import qualified Reporting.Warning as Warning
14import qualified Type.Inference as TI
17compile
18 :: String
19 -> String
20 -> Module.Interfaces
21 -> String
22 -> Result.Result Warning.Warning Error.Error Module.CanonicalModule
24compile user projectName interfaces source =
25 do
26 -- determine if default imports should be added
27 -- only elm-lang/core is exempt
28 let needsDefaults =
29 not (user == "elm-lang" && projectName == "core")
31 -- Parse the source code
32 validModule <-
33 Result.mapError Error.Syntax $
34 Parse.program needsDefaults (getOpTable interfaces) source
36 -- Canonicalize all variables, pinning down where they came from.
37 canonicalModule <-
38 Canonicalize.module' interfaces validModule
40 -- Run type inference on the program.
41 types <-
42 Result.from Error.Type $
43 TI.infer interfaces canonicalModule
45 -- One last round of checks
46 Result.mapError Error.Type $
47 Nitpick.topLevelTypes types (Module.body validModule)
49 -- Add the real list of types
50 let body = (Module.body canonicalModule) { Module.types = types }
52 return $ canonicalModule { Module.body = body }
55getOpTable :: Module.Interfaces -> Parse.OpTable
56getOpTable interfaces =
57 Map.elems interfaces
58 |> concatMap Module.iFixities
59 |> map (\(assoc,lvl,op) -> (op,(lvl,assoc)))
60 |> Map.fromList