@@ -4,32 +4,57 @@ import qualified Data.Map as Map
44import Text.PrettyPrint (Doc)
55
66import qualified AST.Module as Module
7+import qualified Parse.Helpers as Parse
78import qualified Parse.Parse as Parse
89import qualified Transform.AddDefaultImports as DefaultImports
910import qualified Transform.Check as Check
1011import qualified Type.Inference as TI
1112import qualified Transform.Canonicalize as Canonical
13+import Elm.Utils ((|>))
1214
1315
14−compile :: Bool -> Module.Interfaces -> String -> Either [Doc] Module.CanonicalModule
15−compile noPrelude interfaces source =
16− do let infixes = Map.fromList . map (\(assoc,lvl,op) -> (op,(lvl,assoc)))
17− . concatMap Module.iFixities $ Map.elems interfaces
16+compile
17+ :: String
18+ -> String
19+ -> Module.Interfaces
20+ -> String
21+ -> Either [Doc] Module.CanonicalModule
1822
19− -- Parse the source code and validate that it is well formed.
20− validModule <- do
21− modul <- DefaultImports.add noPrelude `fmap` Parse.program infixes source
22− case Check.mistakes (Module.body modul) of
23− [] -> return modul
24− ms -> Left ms
23+compile user projectName interfaces source =
24+ do
25+ -- Parse the source code
26+ parsedModule <-
27+ Parse.program (getOpTable interfaces) source
2528
26− -- Canonicalize all variables, pinning down where they came from.
27− canonicalModule <- Canonical.module' interfaces validModule
29+ -- determine if default imports should be added
30+ -- only elm-lang/core is exempt
31+ let needsDefaults =
32+ not (user == "elm-lang" && projectName == "core")
2833
29− -- Run type inference on the program.
30− types <- TI.infer interfaces canonicalModule
34+ -- add default imports if necessary
35+ let rawModule =
36+ DefaultImports.add needsDefaults parsedModule
3137
32− -- Add the real list of tyes
33− let body = (Module.body canonicalModule) { Module.types = types }
38+ -- validate module (e.g. all variables exist)
39+ case Check.mistakes (Module.body rawModule) of
40+ [] -> return ()
41+ ms -> Left ms
3442
35− return $ canonicalModule { Module.body = body }
43+ -- Canonicalize all variables, pinning down where they came from.
44+ canonicalModule <- Canonical.module' interfaces rawModule
45+
46+ -- Run type inference on the program.
47+ types <- TI.infer interfaces canonicalModule
48+
49+ -- Add the real list of tyes
50+ let body = (Module.body canonicalModule) { Module.types = types }
51+
52+ return $ canonicalModule { Module.body = body }
53+
54+
55+getOpTable :: Module.Interfaces -> Parse.OpTable
56+getOpTable interfaces =
57+ Map.elems interfaces
58+ |> concatMap Module.iFixities
59+ |> map (\(assoc,lvl,op) -> (op,(lvl,assoc)))
60+ |> Map.fromList