mirror of
https://github.com/gren-lang/compiler.git
synced 2024-09-11 08:45:42 +03:00
Make Generate.JavaScript.Mode its own module
This change also moves towards getting --debug working again. In particular, if you are in debug mode, you must have the full transitive set of module interfaces. By putting that information in the Mode, it means it only needs to be loaded in that case. In dev and prod mode, no extra work is needed.
This commit is contained in:
parent
4efbdd9710
commit
6d686c5b57
94
compiler/src/Generate/JavaScript/Mode.hs
Normal file
94
compiler/src/Generate/JavaScript/Mode.hs
Normal file
@ -0,0 +1,94 @@
|
||||
module Generate.JavaScript.Mode
|
||||
( Mode(..)
|
||||
, Target(..)
|
||||
, debug
|
||||
, dev
|
||||
, prod
|
||||
, isServer
|
||||
)
|
||||
where
|
||||
|
||||
|
||||
import qualified Data.List as List
|
||||
import qualified Data.Map as Map
|
||||
|
||||
import qualified AST.Optimized as Opt
|
||||
import qualified Elm.Interface as I
|
||||
import qualified Elm.Name as N
|
||||
import qualified Generate.JavaScript.Name as Name
|
||||
|
||||
|
||||
|
||||
-- MODE
|
||||
|
||||
|
||||
data Mode
|
||||
= Dev Target (Maybe I.Interfaces)
|
||||
| Prod Target ShortFieldNames
|
||||
|
||||
|
||||
data Target = Client | Server
|
||||
|
||||
|
||||
debug :: Target -> I.Interfaces -> Mode
|
||||
debug target interfaces =
|
||||
Dev target (Just interfaces)
|
||||
|
||||
|
||||
dev :: Target -> Mode
|
||||
dev target =
|
||||
Dev target Nothing
|
||||
|
||||
|
||||
prod :: Target -> Opt.Graph -> Mode
|
||||
prod target (Opt.Graph _ _ fieldCounts) =
|
||||
Prod target (shortenFieldNames fieldCounts)
|
||||
|
||||
|
||||
|
||||
-- IS SERVER?
|
||||
|
||||
|
||||
isServer :: Mode -> Bool
|
||||
isServer mode =
|
||||
case mode of
|
||||
Dev target _ -> isServerHelp target
|
||||
Prod target _ -> isServerHelp target
|
||||
|
||||
|
||||
isServerHelp :: Target -> Bool
|
||||
isServerHelp target =
|
||||
case target of
|
||||
Client -> False
|
||||
Server -> True
|
||||
|
||||
|
||||
|
||||
-- SHORTEN FIELD NAMES
|
||||
|
||||
|
||||
type ShortFieldNames =
|
||||
Map.Map N.Name Name.Name
|
||||
|
||||
|
||||
shortenFieldNames :: Map.Map N.Name Int -> ShortFieldNames
|
||||
shortenFieldNames frequencies =
|
||||
Map.foldr addToShortNames Map.empty $
|
||||
Map.foldrWithKey addToBuckets Map.empty frequencies
|
||||
|
||||
|
||||
addToBuckets :: N.Name -> Int -> Map.Map Int [N.Name] -> Map.Map Int [N.Name]
|
||||
addToBuckets field frequency buckets =
|
||||
-- TODO try using an IntMap for buckets
|
||||
Map.insertWith (++) frequency [field] buckets
|
||||
|
||||
|
||||
addToShortNames :: [N.Name] -> ShortFieldNames -> ShortFieldNames
|
||||
addToShortNames fields shortNames =
|
||||
List.foldl' addField shortNames fields
|
||||
|
||||
|
||||
addField :: ShortFieldNames -> N.Name -> ShortFieldNames
|
||||
addField shortNames field =
|
||||
let rename = Name.fromInt (Map.size shortNames) in
|
||||
Map.insert field rename shortNames
|
@ -2,10 +2,6 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
module Generate.JavaScript.Name
|
||||
( Name
|
||||
, Mode(..)
|
||||
, Target(..)
|
||||
, Debugger(..)
|
||||
, isServer
|
||||
, toBuilder
|
||||
, fromIndex
|
||||
, fromInt
|
||||
@ -13,22 +9,18 @@ module Generate.JavaScript.Name
|
||||
, fromGlobal
|
||||
, fromCycle
|
||||
, fromKernel
|
||||
, fromField
|
||||
, makeF
|
||||
, makeA
|
||||
, makeLabel
|
||||
, makeTemp
|
||||
, dollar
|
||||
, shortenFieldNames
|
||||
)
|
||||
where
|
||||
|
||||
|
||||
import qualified Data.ByteString.Builder as B
|
||||
import qualified Data.ByteString.Short as S
|
||||
import qualified Data.List as List
|
||||
import Data.Monoid ((<>))
|
||||
import Data.Map ((!))
|
||||
import qualified Data.Map as Map
|
||||
import qualified Data.Set as Set
|
||||
import qualified Data.Text as Text
|
||||
@ -50,39 +42,6 @@ newtype Name =
|
||||
|
||||
|
||||
|
||||
-- MODE
|
||||
|
||||
|
||||
data Mode
|
||||
= Dev Target Debugger
|
||||
| Prod Target ShortFieldNames
|
||||
|
||||
|
||||
data Target = Client | Server
|
||||
|
||||
|
||||
data Debugger
|
||||
= DebuggerOn
|
||||
| DebuggerOff
|
||||
|
||||
|
||||
type ShortFieldNames =
|
||||
Map.Map N.Name Name
|
||||
|
||||
|
||||
isServer :: Mode -> Bool
|
||||
isServer mode =
|
||||
case mode of
|
||||
Dev target _ -> isServerHelp target
|
||||
Prod target _ -> isServerHelp target
|
||||
|
||||
|
||||
isServerHelp :: Target -> Bool
|
||||
isServerHelp target =
|
||||
case target of { Client -> False ; Server -> True }
|
||||
|
||||
|
||||
|
||||
-- CONSTRUCTORS
|
||||
|
||||
|
||||
@ -127,16 +86,6 @@ fromKernel home name =
|
||||
Name ("_" <> N.toBuilder home <> "_" <> N.toBuilder name)
|
||||
|
||||
|
||||
fromField :: Mode -> N.Name -> Name
|
||||
fromField mode name =
|
||||
case mode of
|
||||
Dev _ _ ->
|
||||
Name (N.toBuilder name)
|
||||
|
||||
Prod _ fields ->
|
||||
fields ! name
|
||||
|
||||
|
||||
|
||||
-- TEMPORARY NAMES
|
||||
|
||||
@ -167,33 +116,6 @@ dollar =
|
||||
|
||||
|
||||
|
||||
-- SHORTEN FIELD NAMES
|
||||
|
||||
|
||||
shortenFieldNames :: Map.Map N.Name Int -> ShortFieldNames
|
||||
shortenFieldNames frequencies =
|
||||
Map.foldr addToShortNames Map.empty $
|
||||
Map.foldrWithKey addToBuckets Map.empty frequencies
|
||||
|
||||
|
||||
addToBuckets :: N.Name -> Int -> Map.Map Int [N.Name] -> Map.Map Int [N.Name]
|
||||
addToBuckets field frequency buckets =
|
||||
-- TODO try using an IntMap for buckets
|
||||
Map.insertWith (++) frequency [field] buckets
|
||||
|
||||
|
||||
addToShortNames :: [N.Name] -> ShortFieldNames -> ShortFieldNames
|
||||
addToShortNames fields shortNames =
|
||||
List.foldl' addField shortNames fields
|
||||
|
||||
|
||||
addField :: ShortFieldNames -> N.Name -> ShortFieldNames
|
||||
addField shortNames field =
|
||||
let rename = fromInt (Map.size shortNames) in
|
||||
Map.insert field rename shortNames
|
||||
|
||||
|
||||
|
||||
-- RESERVED NAMES
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user