on fb08d70 help sign in
elk/src/Compile.hs
55 lines 1.6 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 Parse.Helpers as Parse
9import qualified Parse.Parse as Parse
10import qualified Reporting.Error as Error
11import qualified Reporting.Result as Result
12import qualified Reporting.Warning as Warning
13import qualified Type.Inference as TI
16compile
17 :: String
18 -> String
19 -> Module.Interfaces
20 -> String
21 -> Result.Result Warning.Warning Error.Error Module.CanonicalModule
23compile user projectName interfaces source =
24 do
25 -- determine if default imports should be added
26 -- only elm-lang/core is exempt
27 let needsDefaults =
28 not (user == "elm-lang" && projectName == "core")
30 -- Parse the source code
31 validModule <-
32 Result.mapError Error.Syntax $
33 Parse.program needsDefaults (getOpTable interfaces) source
35 -- Canonicalize all variables, pinning down where they came from.
36 canonicalModule <-
37 Canonicalize.module' interfaces validModule
39 -- Run type inference on the program.
40 types <-
41 Result.from Error.Type $
42 TI.infer interfaces canonicalModule
44 -- Add the real list of tyes
45 let body = (Module.body canonicalModule) { Module.types = types }
47 return $ canonicalModule { Module.body = body }
50getOpTable :: Module.Interfaces -> Parse.OpTable
51getOpTable interfaces =
52 Map.elems interfaces
53 |> concatMap Module.iFixities
54 |> map (\(assoc,lvl,op) -> (op,(lvl,assoc)))
55 |> Map.fromList