on bc3983f help sign in
164 lines 3.3 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Generate.JavaScript.BuiltIn
4 ( char
5 , list
6 , cons
7 , recordUpdate
8 , eq
9 , cmp
10 , effectManagers
11 , effect
12 , outgoingPort
13 , incomingPort
14 , staticProgram
15 , crash
16 )
17 where
20import Data.Text (Text)
22import qualified AST.Module.Name as ModuleName
23import qualified AST.Variable as Var
24import qualified Generate.JavaScript.Builder as JS
25import Generate.JavaScript.Helpers ((<|), (==>))
26import qualified Generate.JavaScript.Variable as Var
27import Generate.JavaScript.Variable (Generator)
28import qualified Reporting.Region as R
32-- LITERALS
35char :: Text -> Generator JS.Expr
36char c =
37 utils "chr" [ JS.String c ]
40utils :: Text -> [JS.Expr] -> Generator JS.Expr
41utils name args =
42 do func <- core "Elm.Kernel.Utils" name
43 return $ JS.Call func args
46core :: ModuleName.Raw -> Text -> Generator JS.Expr
47core home name =
48 Var.global (Var.Global (ModuleName.inCore home) name)
52-- LISTS
55list :: [JS.Expr] -> Generator JS.Expr
56list elements =
57 do fromArray <- core "Elm.Kernel.List" "fromArray"
58 return $ fromArray <| JS.Array elements
61cons :: JS.Expr -> JS.Expr -> Generator JS.Expr
62cons first rest =
63 do func <- core "Elm.Kernel.List" "Cons"
64 return $ JS.Call func [first, rest]
68-- RECORDS
71recordUpdate :: JS.Expr -> [(Text, JS.Expr)] -> Generator JS.Expr
72recordUpdate record fields =
73 utils "update"
74 [ record
75 , JS.Object (map (uncurry (==>)) fields)
76 ]
80-- COMPARISIONS
83eq :: JS.Expr -> JS.Expr -> Generator JS.Expr
84eq left right =
85 utils "eq" [ left, right ]
88cmp :: JS.Expr -> JS.Expr -> Generator JS.Expr
89cmp left right =
90 utils "cmp" [ left, right ]
94-- EFFECTS
97effectManagers :: Generator JS.Expr
98effectManagers =
99 core "Elm.Kernel.Platform" "effectManagers"
102effect :: ModuleName.Canonical -> Generator JS.Expr
103effect effectName =
104 do leaf <- core "Elm.Kernel.Platform" "leaf"
105 return $ leaf <| JS.String (ModuleName.canonicalToText effectName)
108outgoingPort :: Text -> JS.Expr -> Generator JS.Expr
109outgoingPort name converter =
110 do outPort <- core "Elm.Kernel.Platform" "outgoingPort"
111 return $ JS.Call outPort [ JS.String name, converter ]
114incomingPort :: Text -> JS.Expr -> Generator JS.Expr
115incomingPort name converter =
116 do inPort <- core "Elm.Kernel.Platform" "incomingPort"
117 return $ JS.Call inPort [ JS.String name, converter ]
121-- VIRTUAL DOM
124staticProgram :: Generator JS.Expr
125staticProgram =
126 let
127 home =
128 ModuleName.inVirtualDom "Elm.Kernel.VirtualDom"
129 in
130 Var.global (Var.Global home "staticProgram")
134-- CRASH
137crash :: ModuleName.Canonical -> R.Region -> Maybe (JS.Expr) -> Generator JS.Expr
138crash home region maybeCaseCrashValue =
139 let
140 homeString =
141 JS.String (ModuleName.canonicalToText home)
142 in
143 case maybeCaseCrashValue of
144 Nothing ->
145 utils "crash" [ homeString, regionToJs region ]
147 Just crashValue ->
148 utils "crashCase" [ homeString, regionToJs region, crashValue ]
151regionToJs :: R.Region -> JS.Expr
152regionToJs (R.Region start end) =
153 JS.Object
154 [ "start" ==> positionToJs start
155 , "end" ==> positionToJs end
156 ]
159positionToJs :: R.Position -> JS.Expr
160positionToJs (R.Position line column) =
161 JS.Object
162 [ "line" ==> JS.Int line
163 , "column" ==> JS.Int column
164 ]