on master help sign in
Redistribute useful code from Elm.Compiler.Objects

Some is moved to AST.Optimized for now

commitae9ecd5103aab29a6a942090b3490a63cef76158 authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 12 Feb 2019, 19:06 (8 years ago) parentbcd1358
2 files changed+33−109
compiler/src/AST/Optimized.hs+33−1 compiler/src/Elm/Compiler/Objects.hs−108
compiler/src/AST/Optimized.hs+33−1
@@ -11,12 +11,16 @@ module AST.Optimized
1111 , Main(..)
1212 , Node(..)
1313 , EffectsType(..)
14+ , empty
15+ , union
16+ , unions
1417 )
1518 where
1619
1720
1821import Control.Monad (liftM, liftM2, liftM3, liftM4)
19−import Data.Binary
22+import Data.Binary (Binary, get, put, getWord8, putWord8)
23+import qualified Data.List as List
2024import qualified Data.Map as Map
2125import Data.Name (Name)
2226import qualified Data.Set as Set
@@ -151,6 +155,34 @@ data EffectsType = Cmd | Sub | Fx
151155
152156
153157
158+-- GRAPHS
159+
160+
161+{-# NOINLINE empty #-}
162+empty :: Graph
163+empty =
164+ Graph Map.empty Map.empty Map.empty
165+
166+
167+union :: Graph -> Graph -> Graph
168+union (Graph mains1 graph1 fields1) (Graph mains2 graph2 fields2) =
169+ Graph
170+ (Map.union mains1 mains2)
171+ (Map.union graph1 graph2)
172+ (Map.union fields1 fields2)
173+
174+
175+unions :: [Graph] -> Graph
176+unions graphs =
177+ case graphs of
178+ [] ->
179+ empty
180+
181+ g:gs ->
182+ List.foldl' union g gs
183+
184+
185+
154186-- BINARY
155187
156188
compiler/src/Elm/Compiler/Objects.hsdeleted−108
@@ -1,108 +0,0 @@
1−{-# OPTIONS_GHC -Wall #-}
2−module Elm.Compiler.Objects
3− ( JS.Output(..)
4− , JS.generate
5− , JS.generateForRepl
6− , Opt.Graph
7− , empty
8− , union
9− , unions
10− , Kernel(..)
11− , fromKernels
12− )
13− where
14−
15−
16−import qualified Data.List as List
17−import qualified Data.Map as Map
18−import qualified Data.Name as Name
19−
20−import qualified AST.Optimized as Opt
21−import qualified Elm.ModuleName as ModuleName
22−import qualified Elm.Package as Pkg
23−import qualified Generate.JavaScript as JS
24−
25−
26−
27−-- COMBINE GRAPHS
28−
29−
30−{-# NOINLINE empty #-}
31−empty :: Opt.Graph
32−empty =
33− Opt.Graph Map.empty Map.empty Map.empty
34−
35−
36−union :: Opt.Graph -> Opt.Graph -> Opt.Graph
37−union (Opt.Graph mains1 graph1 fields1) (Opt.Graph mains2 graph2 fields2) =
38− Opt.Graph
39− (Map.union mains1 mains2)
40− (Map.union graph1 graph2)
41− (Map.union fields1 fields2)
42−
43−
44−unions :: [Opt.Graph] -> Opt.Graph
45−unions graphs =
46− case graphs of
47− [] ->
48− empty
49−
50− g:gs ->
51− List.foldl' union g gs
52−
53−
54−
55−-- KERNEL GRAPHS
56−
57−
58−data Kernel =
59− Kernel
60− { _client :: Opt.KContent
61− , _server :: Maybe Opt.KContent
62− }
63−
64−
65−fromKernels :: Map.Map Name.Name Kernel -> Opt.Graph
66−fromKernels kernels =
67− Opt.Graph
68− Map.empty
69− (Map.mapKeys toGlobal (Map.map toNode kernels))
70− (Map.foldl' addKernel Map.empty kernels)
71−
72−
73−
74−-- KERNEL TO NODES
75−
76−
77−toGlobal :: Name.Name -> Opt.Global
78−toGlobal home =
79− Opt.Global (ModuleName.Canonical Pkg.kernel (Name.getKernel home)) Name.dollar
80−
81−
82−toNode :: Kernel -> Opt.Node
83−toNode (Kernel client server) =
84− Opt.Kernel client server
85−
86−
87−
88−-- KERNEL TO ELM FIELDS
89−
90−
91−addKernel :: Map.Map Name.Name Int -> Kernel -> Map.Map Name.Name Int
92−addKernel fields (Kernel client maybeServer) =
93− addContent (maybe fields (addContent fields) maybeServer) client
94−
95−
96−addContent :: Map.Map Name.Name Int -> Opt.KContent -> Map.Map Name.Name Int
97−addContent fields (Opt.KContent chunks _) =
98− List.foldl' addChunk fields chunks
99−
100−
101−addChunk :: Map.Map Name.Name Int -> Opt.KChunk -> Map.Map Name.Name Int
102−addChunk fields chunk =
103− case chunk of
104− Opt.ElmField name ->
105− Map.insertWith (+) name 1 fields
106−
107− _ ->
108− fields