on master help sign in
Make public API for generating JS nicer
3f20280Evan Czaplicki
src/Elm/Compiler/Module.hs+1−13
@@ -4,7 +4,6 @@ module Elm.Compiler.Module
4 -- interfaces
4 -- interfaces
5 ( I.Interface
5 ( I.Interface
6 , I.Interfaces
6 , I.Interfaces
7− , interfaceAliasedTypes
8
7
9 -- module names
8 -- module names
10 , Raw
9 , Raw
@@ -21,15 +20,13 @@ module Elm.Compiler.Module
21 )
20 )
22 where
21 where
23
22
23+
24import qualified Data.Char as Char
24import qualified Data.Char as Char
25import qualified Data.List as List
25import qualified Data.List as List
26−import qualified Data.Map as Map
27import qualified Data.Text as Text
26import qualified Data.Text as Text
28import System.FilePath ((</>))
27import System.FilePath ((</>))
29
28
30import qualified AST.Module.Name as ModuleName
29import qualified AST.Module.Name as ModuleName
31−import qualified Elm.Compiler.Type as Type
32−import qualified Elm.Compiler.Type.Extract as Extract
33import qualified Elm.Interface as I
30import qualified Elm.Interface as I
34import qualified Elm.Name as N
31import qualified Elm.Name as N
35import qualified Json.Decode as Decode
32import qualified Json.Decode as Decode
@@ -37,15 +34,6 @@ import qualified Json.Encode as Encode
37
34
38
35
39
36
40−-- INTERFACES
41−
42−
43−interfaceAliasedTypes :: I.Interface -> Map.Map N.Name Type.Type
44−interfaceAliasedTypes interface =
45− Map.map Extract.fromAnnotation (I._types interface)
46−
47−
48−
49-- NAMES
37-- NAMES
50
38
51
39
src/Elm/Compiler/Objects.hs+13−2
@@ -1,19 +1,20 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2module Elm.Compiler.Objects
2module Elm.Compiler.Objects
3 ( Name.Target(..)
3 ( Name.Target(..)
4− , JS.ReplBuilder(..)
5− , JS.generateForRepl
6 , JS.Mode(..)
4 , JS.Mode(..)
7 , JS.generate
5 , JS.generate
6+ , JS.generateForRepl
8 , Opt.Graph
7 , Opt.Graph
9 , empty
8 , empty
10 , union
9 , union
10+ , unions
11 , Kernel(..)
11 , Kernel(..)
12 , fromKernels
12 , fromKernels
13 )
13 )
14 where
14 where
15
15
16
16
17+import qualified Data.List as List
17import qualified Data.Map as Map
18import qualified Data.Map as Map
18
19
19import qualified AST.Optimized as Opt
20import qualified AST.Optimized as Opt
@@ -42,6 +43,16 @@ union (Opt.Graph mains1 graph1 fields1) (Opt.Graph mains2 graph2 fields2) =
42 (Map.union fields1 fields2)
43 (Map.union fields1 fields2)
43
44
44
45
46+unions :: [Opt.Graph] -> Opt.Graph
47+unions graphs =
48+ case graphs of
49+ [] ->
50+ empty
51+
52+ g:gs ->
53+ List.foldl' union g gs
54+
55+
45
56
46-- KERNEL GRAPHS
57-- KERNEL GRAPHS
47
58
src/Generate/JavaScript.hs+37−28
@@ -1,15 +1,14 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Generate.JavaScript
3module Generate.JavaScript
4− ( ReplBuilder(..)
5− , generateForRepl
6− , Mode(..)
4+ ( Mode(..)
7 , generate
5 , generate
6+ , generateForRepl
8 )
7 )
9 where
8 where
10
9
11
10
12−import Prelude hiding (cycle)
11+import Prelude hiding (cycle, print)
13import qualified Data.ByteString.Builder as B
12import qualified Data.ByteString.Builder as B
14import Data.Monoid ((<>))
13import Data.Monoid ((<>))
15import qualified Data.List as List
14import qualified Data.List as List
@@ -20,6 +19,8 @@ import qualified Data.Text as Text
20
19
21import qualified AST.Optimized as Opt
20import qualified AST.Optimized as Opt
22import qualified AST.Module.Name as ModuleName
21import qualified AST.Module.Name as ModuleName
22+import qualified Elm.Compiler.Type as Type
23+import qualified Elm.Compiler.Type.Extract as Extract
23import qualified Elm.Interface as I
24import qualified Elm.Interface as I
24import qualified Elm.Name as N
25import qualified Elm.Name as N
25import qualified Generate.JavaScript.Builder as JS
26import qualified Generate.JavaScript.Builder as JS
@@ -28,36 +29,14 @@ import qualified Generate.JavaScript.Name as Name
28
29
29
30
30
31
31−-- GENERATE FOR REPL
32−
33−
34−data ReplBuilder =
35− ReplBuilder
36− { _repl_code :: B.Builder
37− , _repl_name :: B.Builder
38− }
39−
40−
41−generateForRepl :: Opt.Graph -> ModuleName.Canonical -> N.Name -> ReplBuilder
42−generateForRepl (Opt.Graph _ graph _) home name =
43− ReplBuilder
44− { _repl_code =
45− stateToBuilder $
46− addGlobal (Name.Debug Name.Client) graph emptyState (Opt.Global home name)
47− , _repl_name =
48− Name.toBuilder (Name.fromGlobal home name)
49− }
50−
51−
52−
53-- GENERATE MAINS
32-- GENERATE MAINS
54
33
55
34
56data Mode = Debug | Prod
35data Mode = Debug | Prod
57
36
58
37
59−generate :: Mode -> Name.Target -> Opt.Graph -> I.Interfaces -> [ModuleName.Canonical] -> Either [ModuleName.Canonical] B.Builder
38+generate :: Mode -> Name.Target -> I.Interfaces -> Opt.Graph -> [ModuleName.Canonical] -> Either [ModuleName.Canonical] B.Builder
60−generate mode target (Opt.Graph mains graph fields) interfaces roots =
39+generate mode target interfaces (Opt.Graph mains graph fields) roots =
61 let
40 let
62 rootSet = Set.fromList roots
41 rootSet = Set.fromList roots
63 rootMap = Map.restrictKeys mains rootSet
42 rootMap = Map.restrictKeys mains rootSet
@@ -90,6 +69,36 @@ toRealMode mode target fields =
90
69
91
70
92
71
72+-- GENERATE FOR REPL
73+
74+
75+generateForRepl :: I.Interfaces -> Opt.Graph -> ModuleName.Canonical -> N.Name -> B.Builder
76+generateForRepl interfaces (Opt.Graph _ graph _) home name =
77+ let
78+ mode = Name.Debug Name.Server
79+ eval = addGlobal mode graph emptyState (Opt.Global home name)
80+ in
81+ stateToBuilder eval <> print interfaces home name
82+
83+
84+print :: I.Interfaces -> ModuleName.Canonical -> N.Name -> B.Builder
85+print interfaces home name =
86+ let
87+ value = Name.toBuilder (Name.fromGlobal home name)
88+ toString = Name.toBuilder (Name.fromKernel N.debug "toString")
89+ annotation = I._types (interfaces ! home) ! name
90+ tipe = Type.toString Type.MultiLine $ Extract.fromAnnotation annotation
91+ in
92+ "var _value = " <> toString <> "(" <> value <> ");\n" <>
93+ "var _type = '" <> B.stringUtf8 (show tipe) <> "';\n\
94+ \if (_value.length + 3 + _type.length >= 80 || _type.indexOf('\\n') >= 0) {\n\
95+ \ console.log(_value + '\\n : ' + _type.split('\\n').join('\\n '));\n\
96+ \} else {\n\
97+ \ console.log(_value + ' : ' + _type);\n\
98+ \}\n"
99+
100+
101+
93-- GRAPH TRAVERSAL STATE
102-- GRAPH TRAVERSAL STATE
94
103
95
104