on master help sign in
Shuffle code to make Elm.Compiler work again
commit22777457cb4f78b2cd2a8b82037a8108cf17a417 authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 12 Dec 2017, 13:32 (9 years ago) parent57e03b9
6 files changed+127−122
src/Compile.hs+48−4 src/Elm/Compiler.hs+39−85 src/Elm/Compiler/Objects.hs+8−6 src/Elm/Docs.hs+3−3 src/Generate/JavaScript.hs+27−22 src/Reporting/Result.hs+2−2
src/Compile.hs+48−4
@@ -1,6 +1,8 @@
11{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
22module Compile
3− ( compile
3+ ( DocsFlag(..)
4+ , compile
5+ , Artifacts(..)
46 )
57 where
68
@@ -12,6 +14,7 @@ import qualified AST.Canonical as Can
1214import qualified AST.Optimized as Opt
1315import qualified AST.Module.Name as ModuleName
1416import qualified Canonicalize.Module as Canonicalize
17+import qualified Elm.Docs as Docs
1518import qualified Elm.Interface as I
1619import qualified Elm.Name as N
1720import qualified Elm.Package as Pkg
@@ -40,8 +43,16 @@ type ImportDict =
4043 Map.Map N.Name ModuleName.Canonical
4144
4245
43−compile :: Pkg.Name -> ImportDict -> I.Interfaces -> BS.ByteString -> Result i (I.Interface, Opt.Graph)
44−compile pkg importDict interfaces source =
46+data Artifacts =
47+ Artifacts
48+ { _elmi :: I.Interface
49+ , _elmo :: Opt.Graph
50+ , _docs :: Maybe Docs.Module
51+ }
52+
53+
54+compile :: DocsFlag -> Pkg.Name -> ImportDict -> I.Interfaces -> BS.ByteString -> Result i Artifacts
55+compile flag pkg importDict interfaces source =
4556 do
4657 valid <- Result.mapError Error.Syntax $
4758 Parse.program source
@@ -60,7 +71,19 @@ compile pkg importDict interfaces source =
6071
6172 let (Optimize.Graph fields graph) = Optimize.optimize canonical
6273
63− Result.ok (I.fromModule annotations canonical, Opt.Graph mains graph fields)
74+ documentation <-
75+ genaretaDocs flag canonical
76+
77+ Result.ok $
78+ Artifacts
79+ { _elmi = I.fromModule annotations canonical
80+ , _elmo = Opt.Graph mains graph fields
81+ , _docs = documentation
82+ }
83+
84+
85+
86+-- TYPE INFERENCE
6487
6588
6689runTypeInference :: Can.Module -> Result i (Map.Map N.Name Can.Annotation)
@@ -73,6 +96,10 @@ runTypeInference canonical =
7396 Result.ok annotations
7497
7598
99+
100+-- EXHAUSTIVENESS CHECK
101+
102+
76103exhaustivenessCheck :: Can.Module -> Result i ()
77104exhaustivenessCheck canonical =
78105 case PatternMatches.check canonical of
@@ -81,3 +108,20 @@ exhaustivenessCheck canonical =
81108
82109 Right () ->
83110 Result.ok ()
111+
112+
113+
114+-- DOCUMENTATION
115+
116+
117+data DocsFlag = YesDocs | NoDocs
118+
119+
120+genaretaDocs :: DocsFlag -> Can.Module -> Result.Result i w Error.Error (Maybe Docs.Module)
121+genaretaDocs flag modul =
122+ case flag of
123+ NoDocs ->
124+ Result.ok Nothing
125+
126+ YesDocs ->
127+ Just <$> Docs.fromModule modul
src/Elm/Compiler.hs+39−85
@@ -1,36 +1,31 @@
11{-# OPTIONS_GHC -Wall #-}
22module Elm.Compiler
33 ( version
4− , compile, Context(..), Result(..)
5− , generate, JS.Target(..)
6− , Localizer, dummyLocalizer
7− , I.Error, errorToDoc, errorToJson
8− , I.Warning, warningToDoc, warningToJson
4+ , Context(..)
5+ , Compile.Artifacts(..)
6+ , compile
7+ , Localizer
8+ , dummyLocalizer
9+ , Error.Error
10+ , errorToDoc
11+ , errorToJson
12+ , Warning.Warning
13+ , warningToDoc
14+ , warningToJson
915 )
1016 where
1117
1218import qualified Data.Aeson as Json
19+import qualified Data.ByteString as BS
1320import qualified Data.Map as Map
14−import Data.Text (Text)
15−import qualified Data.ByteString.Builder as BS
1621import Text.PrettyPrint.ANSI.Leijen (Doc)
1722
18−import qualified AST.Module as Module
19−import qualified AST.Module.Name as ModuleName
2023import qualified Compile
21−import qualified Docs.Check as Docs
22−import qualified Elm.Compiler.Internals as I
2324import qualified Elm.Compiler.Module as M
24−import qualified Elm.Compiler.Objects.Internal as Obj
2525import qualified Elm.Compiler.Version
26−import qualified Elm.Docs as Docs
27−import qualified Elm.Package as Package
28−import qualified Generate.JavaScript as JS
29−import qualified Reporting.Annotation as A
30−import qualified Reporting.Bag as Bag
26+import qualified Elm.Package as Pkg
3127import qualified Reporting.Error as Error
3228import qualified Reporting.Render.Type as RenderType
33−import qualified Reporting.Report as Report
3429import qualified Reporting.Result as Result
3530import qualified Reporting.Warning as Warning
3631
@@ -39,75 +34,34 @@ import qualified Reporting.Warning as Warning
3934-- VERSION
4035
4136
42−version :: Package.Version
37+version :: Pkg.Version
4338version =
4439 Elm.Compiler.Version.version
4540
4641
4742
48−-- COMPILATION
49−
50−
51−{-| Compiles Elm source code to JavaScript. -}
52−compile :: Context -> Text -> (Localizer, [I.Warning], Either [I.Error] Result)
53−compile (Context packageName exposed importDict interfaces) source =
54− let
55− (Result.Result oneLocalizer warnings answer) =
56− do modul <- Compile.compile packageName importDict interfaces source
57− docs <- Result.format id (docsGen exposed modul)
58−
59− let iface = Module.toInterface modul
60− let objs = Obj.fromModule modul
61−
62− return (Result docs iface objs)
63− in
64− ( Result.oneToValue dummyLocalizer Localizer oneLocalizer
65− , Bag.toList I.Warning warnings
66− , Result.answerToEither I.Error id answer
67− )
43+-- COMPILE
6844
6945
7046data Context =
7147 Context
72− { _package :: Package.Name
48+ { _package :: Pkg.Name
7349 , _exposed :: Bool
7450 , _imports :: Map.Map M.Raw M.Canonical
7551 , _interfaces :: M.Interfaces
7652 }
7753
7854
79−data Result =
80− Result
81− { _docs :: Maybe Docs.Module
82− , _iface :: M.Interface
83− , _objs :: Obj.Graph
84− }
85−
86−
87−
88−-- DOCUMENTATION
89−
90−
91−docsGen :: Bool -> Module.Optimized -> Result.Result () w Error.Error (Maybe Docs.Module)
92−docsGen isExposed (Module.Module name info) =
93− if not isExposed then
94− Result.ok Nothing
95−
96− else
97− let
98− toDocs checked =
99− Just (Docs.Module (ModuleName._module name) checked)
100− in
101− toDocs <$> Docs.check (Module.exports info) (Module.docs info)
102−
103−
104−
105−-- CODE GENERATION
106−
55+compile :: Context -> BS.ByteString -> (Localizer, [Warning.Warning], Either [Error.Error] Compile.Artifacts)
56+compile (Context pkg exposed importDict interfaces) source =
57+ let
58+ docsFlag =
59+ if exposed then Compile.YesDocs else Compile.NoDocs
10760
108−generate :: Bool -> JS.Target -> Obj.Graph -> Obj.Roots -> BS.Builder
109−generate =
110− JS.generate
61+ (warnings, errorOrArtifacts) =
62+ Result.run $ Compile.compile docsFlag pkg importDict interfaces source
63+ in
64+ ( dummyLocalizer, warnings, errorOrArtifacts )
11165
11266
11367
@@ -115,37 +69,37 @@ generate =
11569
11670
11771newtype Localizer =
118− Localizer RenderType.Localizer
72+ Localizer RenderType.Localizer
11973
12074
12175dummyLocalizer :: Localizer
12276dummyLocalizer =
123− Localizer Map.empty
77+ Localizer Map.empty
12478
12579
12680
12781-- ERRORS
12882
12983
130−errorToDoc :: Localizer -> String -> Text -> I.Error -> Doc
131−errorToDoc (Localizer localizer) location source (I.Error (A.A region err)) =
132− Report.toDoc location region (Error.toReport localizer err) source
84+errorToDoc :: Localizer -> String -> String -> Error.Error -> Doc
85+errorToDoc (Localizer localizer) location source err =
86+ error "TODO Report.toDoc" location (Error.toReport localizer err) source
13387
13488
135−errorToJson :: Localizer -> String -> I.Error -> Json.Value
136−errorToJson (Localizer localizer) location (I.Error err) =
137− Error.toJson localizer location err
89+errorToJson :: Localizer -> String -> Error.Error -> Json.Value
90+errorToJson (Localizer localizer) location err =
91+ error "TODO Error.toJson" localizer location err
13892
13993
14094
14195-- WARNINGS
14296
14397
144−warningToDoc :: Localizer -> String -> Text -> I.Warning -> Doc
145−warningToDoc (Localizer localizer) location source (I.Warning (A.A region wrn)) =
146− Report.toDoc location region (Warning.toReport localizer wrn) source
98+warningToDoc :: Localizer -> String -> String -> Warning.Warning -> Doc
99+warningToDoc (Localizer localizer) location source warning =
100+ error "TODO Report.toDoc" location (Warning.toReport localizer warning) source
147101
148102
149−warningToJson :: Localizer -> String -> I.Warning -> Json.Value
150−warningToJson (Localizer localizer) location (I.Warning wrn) =
151− Warning.toJson localizer location wrn
103+warningToJson :: Localizer -> String -> Warning.Warning -> Json.Value
104+warningToJson (Localizer localizer) location warning =
105+ error "TODO Warning.toJson" localizer location warning
src/Elm/Compiler/Objects.hs+8−6
@@ -1,6 +1,8 @@
11{-# OPTIONS_GHC -Wall #-}
22module Elm.Compiler.Objects
3− ( Gen.Roots
3+ ( JS.Mode(..)
4+ , JS.generate
5+ , JS.Roots
46 , mains
57 , value
68 , Opt.Graph
@@ -18,21 +20,21 @@ import qualified Data.Text as Text
1820
1921import qualified AST.Optimized as Opt
2022import qualified AST.Module.Name as ModuleName
21−import qualified Generate.JavaScript as Gen
23+import qualified Generate.JavaScript as JS
2224
2325
2426
2527-- ROOTS
2628
2729
28−mains :: [ModuleName.Canonical] -> Gen.Roots
30+mains :: [ModuleName.Canonical] -> JS.Roots
2931mains =
30− Gen.Mains
32+ JS.Mains
3133
3234
33−value :: ModuleName.Canonical -> String -> Gen.Roots
35+value :: ModuleName.Canonical -> String -> JS.Roots
3436value home name =
35− Gen.Value home (Text.pack name)
37+ JS.Value home (Text.pack name)
3638
3739
3840
src/Elm/Docs.hs+3−3
@@ -4,7 +4,7 @@ module Elm.Docs
44 ( Documentation
55 , toDict
66 , Module(..)
7− , fromCanonical
7+ , fromModule
88 , Union(..)
99 , Alias(..)
1010 , Value(..)
@@ -270,8 +270,8 @@ precDecoder =
270270-- FROM MODULE
271271
272272
273−fromCanonical :: Can.Module -> Result.Result i w Error.Error Module
274−fromCanonical (Can.Module name docs exports decls unions aliases binops effects) =
273+fromModule :: Can.Module -> Result.Result i w Error.Error Module
274+fromModule (Can.Module name docs exports decls unions aliases binops effects) =
275275 case exports of
276276 Can.ExportEverything ->
277277 Result.throw $ Error.Docs $ E.ImplicitExposing
src/Generate/JavaScript.hs+27−22
@@ -1,8 +1,9 @@
11{-# OPTIONS_GHC -Wall #-}
22{-# LANGUAGE OverloadedStrings #-}
33module Generate.JavaScript
4− ( generate
4+ ( Mode(..)
55 , Roots(..)
6+ , generate
67 )
78 where
89
@@ -28,29 +29,34 @@ import qualified Generate.JavaScript.Name as Name
2829-- GENERATE
2930
3031
32+data Mode = Debug | Prod
33+
34+
3135data Roots
3236 = Mains [ModuleName.Canonical]
3337 | Value ModuleName.Canonical N.Name
3438
3539
36−generate :: Bool -> Name.Target -> Opt.Graph -> Roots -> Either [ModuleName.Canonical] B.Builder
37−generate debug target (Opt.Graph mains nodes fields) roots =
40+generate :: Mode -> Name.Target -> Opt.Graph -> Roots -> Either [ModuleName.Canonical] B.Builder
41+generate mode target (Opt.Graph mains nodes fields) roots =
3842 case roots of
3943 Value home name ->
4044 Right $ stateToBuilder $
41− addGlobal (toMode debug target fields) nodes emptyState (Opt.Global home name)
45+ addGlobal (toRealMode mode target fields) nodes emptyState (Opt.Global home name)
4246
4347 Mains modules ->
44− generateMain (toMode debug target fields) nodes
48+ generateMain (toRealMode mode target fields) nodes
4549 <$> verifyMains mains modules
4650
4751
48−toMode :: Bool -> Name.Target -> Map.Map N.Name Int -> Name.Mode
49−toMode debug target fields =
50− if debug then
51− Name.Debug target
52− else
53− Name.Prod target (Name.shortenFieldNames fields)
52+toRealMode :: Mode -> Name.Target -> Map.Map N.Name Int -> Name.Mode
53+toRealMode mode target fields =
54+ case mode of
55+ Debug ->
56+ Name.Debug target
57+
58+ Prod ->
59+ Name.Prod target (Name.shortenFieldNames fields)
5460
5561
5662
@@ -78,11 +84,10 @@ stateToBuilder (State revBuilders _) =
7884-- ADD DEPENDENCIES
7985
8086
81−type Mode = Name.Mode
8287type Graph = Map.Map Opt.Global Opt.Node
8388
8489
85−addGlobal :: Mode -> Graph -> State -> Opt.Global -> State
90+addGlobal :: Name.Mode -> Graph -> State -> Opt.Global -> State
8691addGlobal mode graph state@(State builders seen) global =
8792 if Set.member global seen then
8893 state
@@ -91,7 +96,7 @@ addGlobal mode graph state@(State builders seen) global =
9196 State builders (Set.insert global seen)
9297
9398
94−addGlobalHelp :: Mode -> Graph -> Opt.Global -> State -> State
99+addGlobalHelp :: Name.Mode -> Graph -> Opt.Global -> State -> State
95100addGlobalHelp mode graph global state =
96101 let
97102 addDeps deps someState =
@@ -163,7 +168,7 @@ var (Opt.Global home name) code =
163168-- GENERATE CYCLES
164169
165170
166−generateCycle :: Mode -> Opt.Global -> [(N.Name, Opt.Expr)] -> JS.Stmt
171+generateCycle :: Name.Mode -> Opt.Global -> [(N.Name, Opt.Expr)] -> JS.Stmt
167172generateCycle mode (Opt.Global home _) cycle =
168173 let
169174 safeDefs = map (generateSafeCycle mode home) cycle
@@ -173,7 +178,7 @@ generateCycle mode (Opt.Global home _) cycle =
173178 JS.Block (safeDefs ++ realDefs)
174179
175180
176−generateSafeCycle :: Mode -> ModuleName.Canonical -> (N.Name, Opt.Expr) -> JS.Stmt
181+generateSafeCycle :: Name.Mode -> ModuleName.Canonical -> (N.Name, Opt.Expr) -> JS.Stmt
177182generateSafeCycle mode home (name, expr) =
178183 JS.FunctionStmt (Name.fromCycle home name) [] $
179184 Expr.codeToStmtList (Expr.generate mode expr)
@@ -196,12 +201,12 @@ generateRealCycle home (name, _) =
196201-- GENERATE KERNEL
197202
198203
199−generateKernel :: Mode -> [Opt.KChunk] -> B.Builder
204+generateKernel :: Name.Mode -> [Opt.KChunk] -> B.Builder
200205generateKernel mode chunks =
201206 List.foldl' (addChunk mode) mempty chunks
202207
203208
204−addChunk :: Mode -> B.Builder -> Opt.KChunk -> B.Builder
209+addChunk :: Name.Mode -> B.Builder -> Opt.KChunk -> B.Builder
205210addChunk mode builder chunk =
206211 case chunk of
207212 Opt.JS javascript ->
@@ -240,7 +245,7 @@ addChunk mode builder chunk =
240245-- GENERATE PORTS
241246
242247
243−generatePort :: Mode -> Opt.Global -> N.Name -> Opt.Expr -> JS.Stmt
248+generatePort :: Name.Mode -> Opt.Global -> N.Name -> Opt.Expr -> JS.Stmt
244249generatePort mode (Opt.Global home name) makePort converter =
245250 let
246251 definition =
@@ -257,7 +262,7 @@ generatePort mode (Opt.Global home name) makePort converter =
257262
258263
259264
260−generateManager :: Mode -> Graph -> Opt.Global -> Opt.EffectsType -> State -> State
265+generateManager :: Name.Mode -> Graph -> Opt.Global -> Opt.EffectsType -> State -> State
261266generateManager mode graph (Opt.Global home@(ModuleName.Canonical _ moduleName) _) effectsType state =
262267 let
263268 managerLVar =
@@ -327,7 +332,7 @@ type Mains =
327332 Map.Map ModuleName.Canonical Opt.Main
328333
329334
330−generateMain :: Mode -> Graph -> Mains -> B.Builder
335+generateMain :: Name.Mode -> Graph -> Mains -> B.Builder
331336generateMain mode graph mains =
332337 mconcat
333338 [ stateToBuilder $ Map.foldrWithKey (addMain mode graph) emptyState mains
@@ -335,7 +340,7 @@ generateMain mode graph mains =
335340 ]
336341
337342
338−addMain :: Mode -> Graph -> ModuleName.Canonical -> main -> State -> State
343+addMain :: Name.Mode -> Graph -> ModuleName.Canonical -> main -> State -> State
339344addMain mode graph home _ state =
340345 addGlobal mode graph state (Opt.Global home "main")
341346
src/Reporting/Result.hs+2−2
@@ -33,10 +33,10 @@ newtype Result info warnings error a =
3333 )
3434
3535
36−run :: Result () [w] e a -> ([w], Either (OneOrMore.OneOrMore e) a)
36+run :: Result () [w] e a -> ([w], Either [e] a)
3737run (Result k) =
3838 k () []
39− (\() w e -> (reverse w, Left e))
39+ (\() w e -> (reverse w, Left (OneOrMore.toList e)))
4040 (\() w a -> (reverse w, Right a))
4141
4242