on ccc138c help sign in
elk/src/Compile.hs
61 lines 1.9 kB history raw
1module Compile (compile) where
3import qualified Data.Map as Map
5import qualified AST.Module as Module
6import Elm.Utils ((|>))
7import qualified Parse.Helpers as Parse
8import qualified Parse.Parse as Parse
9import qualified Reporting.Error as Error
10import qualified Reporting.Result as Result
11import qualified Reporting.Warning as Warning
12import qualified Reporting.Annotation as A
13import qualified Type.Inference as TI
14import qualified Canonicalize
15import qualified CheckMatch
18compile
19 :: String
20 -> String
21 -> Module.Interfaces
22 -> String
23 -> Result.Result Warning.Warning Error.Error Module.CanonicalModule
25compile user projectName interfaces source =
26 do
27 -- determine if default imports should be added
28 -- only elm-lang/core is exempt
29 let needsDefaults =
30 not (user == "elm-lang" && projectName == "core")
32 -- Parse the source code
33 validModule <-
34 Result.mapError Error.Syntax $
35 Parse.program needsDefaults (getOpTable interfaces) source
37 -- Canonicalize all variables, pinning down where they came from.
38 canonicalModule <-
39 Canonicalize.module' interfaces validModule
41 -- Run type inference on the program.
42 types <-
43 Result.from Error.Type $
44 TI.infer interfaces canonicalModule
46 -- Add the real list of tyes
47 let body = (Module.body canonicalModule) { Module.types = types }
49 case CheckMatch.checkBody interfaces body of
50 Left (A.A r matchError) -> Result.throw r (Error.CheckMatch matchError)
51 Right matchWarnings -> Result.addWarnings (map (A.map Warning.MatchWarning) matchWarnings) (return ())
53 return $ canonicalModule { Module.body = body }
56getOpTable :: Module.Interfaces -> Parse.OpTable
57getOpTable interfaces =
58 Map.elems interfaces
59 |> concatMap Module.iFixities
60 |> map (\(assoc,lvl,op) -> (op,(lvl,assoc)))
61 |> Map.fromList