git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Haddock comments
2620fe0Gary Burgess
src/Language/PureScript/CodeGen/JS.hs+45−1
@@ -45,7 +45,8 @@ import Language.PureScript.CodeGen.Common
45import Language.PureScript.TypeChecker.Monad (canonicalizeDataConstructor)
45import Language.PureScript.TypeChecker.Monad (canonicalizeDataConstructor)
46
46
47-- |
47-- |
48−-- Generate code in the simplified Javascript intermediate representation for all declarations in a module
48+-- Generate code in the simplified Javascript intermediate representation for all declarations in a
49+-- module.
49--
50--
50moduleToJs :: Options -> Module -> Environment -> [JS]
51moduleToJs :: Options -> Module -> Environment -> [JS]
51moduleToJs opts (Module pname@(ProperName name) decls) env =
52moduleToJs opts (Module pname@(ProperName name) decls) env =
@@ -85,6 +86,10 @@ declToJs _ mp (ExternDeclaration importTy ident (Just js) _) _ =
85 Just $ js : setProperty ident (var ident) mp
86 Just $ js : setProperty ident (var ident) mp
86declToJs _ _ _ _ = Nothing
87declToJs _ _ _ _ = Nothing
87
88
89+-- |
90+-- Generate code in the simplified Javascript intermediate representation for setting the property
91+-- of an object.
92+--
88setProperty :: Ident -> JS -> ModuleName -> [JS]
93setProperty :: Ident -> JS -> ModuleName -> [JS]
89setProperty ident@(Op op) val (ModuleName (ProperName moduleName)) =
94setProperty ident@(Op op) val (ModuleName (ProperName moduleName)) =
90 [ JSAssignment (accessor ident (JSVar moduleName)) val
95 [ JSAssignment (accessor ident (JSVar moduleName)) val
@@ -92,13 +97,25 @@ setProperty ident@(Op op) val (ModuleName (ProperName moduleName)) =
92setProperty ident val (ModuleName (ProperName moduleName)) =
97setProperty ident val (ModuleName (ProperName moduleName)) =
93 [ JSAssignment (accessor ident (JSVar moduleName)) val ]
98 [ JSAssignment (accessor ident (JSVar moduleName)) val ]
94
99
100+-- |
101+-- Generate code in the simplified Javascript intermediate representation for a variable based on a
102+-- PureScript identifier.
103+--
95var :: Ident -> JS
104var :: Ident -> JS
96var = JSVar . identToJs
105var = JSVar . identToJs
97
106
107+-- |
108+-- Generate code in the simplified Javascript intermediate representation for an accessor based on
109+-- a PureScript identifier. If the name is not valid in Javascript (symbol based, reserved name) an
110+-- indexer is returned.
111+--
98accessor :: Ident -> JS -> JS
112accessor :: Ident -> JS -> JS
99accessor (Ident name) | nameIsJsReserved name = JSIndexer (JSStringLiteral name)
113accessor (Ident name) | nameIsJsReserved name = JSIndexer (JSStringLiteral name)
100accessor ident = JSAccessor (identToJs ident)
114accessor ident = JSAccessor (identToJs ident)
101
115
116+-- |
117+-- Generate code in the simplified Javascript intermediate representation for a value or expression.
118+--
102valueToJs :: Options -> ModuleName -> Environment -> Value -> JS
119valueToJs :: Options -> ModuleName -> Environment -> Value -> JS
103valueToJs _ _ _ (NumericLiteral n) = JSNumericLiteral n
120valueToJs _ _ _ (NumericLiteral n) = JSNumericLiteral n
104valueToJs _ _ _ (StringLiteral s) = JSStringLiteral s
121valueToJs _ _ _ (StringLiteral s) = JSStringLiteral s
@@ -130,6 +147,9 @@ valueToJs _ _ _ _ = error "Invalid argument to valueToJs"
130bindName :: ModuleName -> Ident -> Environment -> Environment
147bindName :: ModuleName -> Ident -> Environment -> Environment
131bindName m ident env = env { names = M.insert (m, ident) (unit, LocalVariable) $ names env }
148bindName m ident env = env { names = M.insert (m, ident) (unit, LocalVariable) $ names env }
132
149
150+-- |
151+-- Generate code in the simplified Javascript intermediate representation for runtime type checks.
152+--
133runtimeTypeChecks :: String -> Type -> [JS]
153runtimeTypeChecks :: String -> Type -> [JS]
134runtimeTypeChecks arg ty =
154runtimeTypeChecks arg ty =
135 let
155 let
@@ -159,6 +179,10 @@ runtimeTypeChecks arg ty =
159 arrayCheck :: JS -> JS
179 arrayCheck :: JS -> JS
160 arrayCheck js = JSIfElse (JSUnary Not (JSApp (JSAccessor "isArray" (JSVar "Array")) [js])) (JSBlock [JSThrow (JSStringLiteral "Array expected")]) Nothing
180 arrayCheck js = JSIfElse (JSUnary Not (JSApp (JSAccessor "isArray" (JSVar "Array")) [js])) (JSBlock [JSThrow (JSStringLiteral "Array expected")]) Nothing
161
181
182+-- |
183+-- Generate code in the simplified Javascript intermediate representation for a reference to a
184+-- variable.
185+--
162varToJs :: ModuleName -> Environment -> Qualified Ident -> JS
186varToJs :: ModuleName -> Environment -> Qualified Ident -> JS
163varToJs m e qual@(Qualified _ ident) = go qual
187varToJs m e qual@(Qualified _ ident) = go qual
164 where
188 where
@@ -174,10 +198,18 @@ varToJs m e qual@(Qualified _ ident) = go qual
174 Nothing -> error "Undefined alias in varToJs"
198 Nothing -> error "Undefined alias in varToJs"
175 isExtern _ = False
199 isExtern _ = False
176
200
201+-- |
202+-- Generate code in the simplified Javascript intermediate representation for a reference to a
203+-- variable that may have a qualified name.
204+--
177qualifiedToJS :: (a -> Ident) -> Qualified a -> JS
205qualifiedToJS :: (a -> Ident) -> Qualified a -> JS
178qualifiedToJS f (Qualified (Just (ModuleName (ProperName m))) a) = accessor (f a) (JSVar m)
206qualifiedToJS f (Qualified (Just (ModuleName (ProperName m))) a) = accessor (f a) (JSVar m)
179qualifiedToJS f (Qualified Nothing a) = JSVar $ identToJs (f a)
207qualifiedToJS f (Qualified Nothing a) = JSVar $ identToJs (f a)
180
208
209+-- |
210+-- Generate code in the simplified Javascript intermediate representation for pattern match binders
211+-- and guards.
212+--
181bindersToJs :: Options -> ModuleName -> Environment -> [([Binder], Maybe Guard, Value)] -> [JS] -> JS
213bindersToJs :: Options -> ModuleName -> Environment -> [([Binder], Maybe Guard, Value)] -> [JS] -> JS
182bindersToJs opts m e binders vals = runGen (map identToJs (unusedNames (binders, vals))) $ do
214bindersToJs opts m e binders vals = runGen (map identToJs (unusedNames (binders, vals))) $ do
183 valNames <- replicateM (length vals) fresh
215 valNames <- replicateM (length vals) fresh
@@ -193,6 +225,10 @@ bindersToJs opts m e binders vals = runGen (map identToJs (unusedNames (binders,
193 binderToJs m e v done'' b
225 binderToJs m e v done'' b
194 go _ _ _ _ = error "Invalid arguments to bindersToJs"
226 go _ _ _ _ = error "Invalid arguments to bindersToJs"
195
227
228+-- |
229+-- Generate code in the simplified Javascript intermediate representation for a pattern match
230+-- binder.
231+--
196binderToJs :: ModuleName -> Environment -> String -> [JS] -> Binder -> Gen [JS]
232binderToJs :: ModuleName -> Environment -> String -> [JS] -> Binder -> Gen [JS]
197binderToJs _ _ _ done NullBinder = return done
233binderToJs _ _ _ done NullBinder = return done
198binderToJs _ _ varName done (StringBinder str) =
234binderToJs _ _ varName done (StringBinder str) =
@@ -256,6 +292,10 @@ binderToJs m e varName done (NamedBinder ident binder) = do
256 js <- binderToJs m e varName done binder
292 js <- binderToJs m e varName done binder
257 return (JSVariableIntroduction (identToJs ident) (Just (JSVar varName)) : js)
293 return (JSVariableIntroduction (identToJs ident) (Just (JSVar varName)) : js)
258
294
295+-- |
296+-- Checks whether a data constructor is the only constructor for that type, used to simplify the
297+-- check when generating code for binders.
298+--
259isOnlyConstructor :: ModuleName -> Environment -> Qualified ProperName -> Bool
299isOnlyConstructor :: ModuleName -> Environment -> Qualified ProperName -> Bool
260isOnlyConstructor m e ctor =
300isOnlyConstructor m e ctor =
261 let (ty, _) = fromMaybe (error "Data constructor not found") $ qualify m ctor `M.lookup` dataConstructors e
301 let (ty, _) = fromMaybe (error "Data constructor not found") $ qualify m ctor `M.lookup` dataConstructors e
@@ -268,6 +308,10 @@ isOnlyConstructor m e ctor =
268 typeConstructor (TypeApp ty _) = typeConstructor ty
308 typeConstructor (TypeApp ty _) = typeConstructor ty
269 typeConstructor fn = error $ "Invalid arguments to typeConstructor: " ++ show fn
309 typeConstructor fn = error $ "Invalid arguments to typeConstructor: " ++ show fn
270
310
311+-- |
312+-- Generate code in the simplified Javascript intermediate representation for a statement in a
313+-- PureScript block.
314+--
271statementToJs :: Options -> ModuleName -> Environment -> Statement -> JS
315statementToJs :: Options -> ModuleName -> Environment -> Statement -> JS
272statementToJs opts m e (VariableIntroduction ident value) = JSVariableIntroduction (identToJs ident) (Just (valueToJs opts m e value))
316statementToJs opts m e (VariableIntroduction ident value) = JSVariableIntroduction (identToJs ident) (Just (valueToJs opts m e value))
273statementToJs opts m e (Assignment target value) = JSAssignment (JSVar (identToJs target)) (valueToJs opts m e value)
317statementToJs opts m e (Assignment target value) = JSAssignment (JSVar (identToJs target)) (valueToJs opts m e value)