on e28d01e help sign in
160 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 Var.global (Var.Global (ModuleName.inBrowser "Browser") "staticPage")
130-- CRASH
133crash :: ModuleName.Canonical -> R.Region -> Maybe (JS.Expr) -> Generator JS.Expr
134crash home region maybeCaseCrashValue =
135 let
136 homeString =
137 JS.String (ModuleName.canonicalToText home)
138 in
139 case maybeCaseCrashValue of
140 Nothing ->
141 utils "crash" [ homeString, regionToJs region ]
143 Just crashValue ->
144 utils "crashCase" [ homeString, regionToJs region, crashValue ]
147regionToJs :: R.Region -> JS.Expr
148regionToJs (R.Region start end) =
149 JS.Object
150 [ "start" ==> positionToJs start
151 , "end" ==> positionToJs end
152 ]
155positionToJs :: R.Position -> JS.Expr
156positionToJs (R.Position line column) =
157 JS.Object
158 [ "line" ==> JS.Int line
159 , "column" ==> JS.Int column
160 ]