git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #183 from garyb/master
3ca411cPhil Freeman
prelude/prelude.purs+46−2
@@ -54,6 +54,13 @@ module Prelude where
54 instance Read Boolean where
54 instance Read Boolean where
55 read "true" = true
55 read "true" = true
56 read _ = false
56 read _ = false
57+
58+ foreign import readNumber "function readNumber(n) {\
59+ \ return parseInt(n, 10);\
60+ \}" :: String -> Number
61+
62+ instance Read Number where
63+ read = readNumber
57
64
58 infixl 4 <$>
65 infixl 4 <$>
59
66
@@ -362,6 +369,10 @@ module Monoid where
362 instance Monoid String where
369 instance Monoid String where
363 mempty = ""
370 mempty = ""
364 (<>) = (++)
371 (<>) = (++)
372+
373+ instance Monoid [a] where
374+ mempty = []
375+ (<>) = Arrays.concat
365
376
366 mconcat :: forall m. (Monoid m) => [m] -> m
377 mconcat :: forall m. (Monoid m) => [m] -> m
367 mconcat [] = mempty
378 mconcat [] = mempty
@@ -429,22 +440,52 @@ module Maybe where
429
440
430 fromMaybe :: forall a. a -> Maybe a -> a
441 fromMaybe :: forall a. a -> Maybe a -> a
431 fromMaybe a = maybe a (Prelude.id :: forall a. a -> a)
442 fromMaybe a = maybe a (Prelude.id :: forall a. a -> a)
443+
444+ instance Prelude.Functor Maybe where
445+ (<$>) fn (Just x) = Just (fn x)
446+ (<$>) _ _ = Nothing
447+
448+ instance Prelude.Applicative Maybe where
449+ pure = Just
450+ (<*>) (Just fn) x = fn <$> x
451+ (<*>) Nothing _ = Nothing
432
452
433 instance Prelude.Monad Maybe where
453 instance Prelude.Monad Maybe where
434 return = Just
454 return = Just
435 (>>=) m f = maybe Nothing f m
455 (>>=) m f = maybe Nothing f m
456+
457+ instance (Show a) => Prelude.Show (Maybe a) where
458+ show (Just x) = "Just " ++ (show x)
459+ show Nothing = "Nothing"
436
460
437module Either where
461module Either where
438
462
463+ import Prelude
464+
439 data Either a b = Left a | Right b
465 data Either a b = Left a | Right b
440
466
441 either :: forall a b c. (a -> c) -> (b -> c) -> Either a b -> c
467 either :: forall a b c. (a -> c) -> (b -> c) -> Either a b -> c
442 either f _ (Left a) = f a
468 either f _ (Left a) = f a
443 either _ g (Right b) = g b
469 either _ g (Right b) = g b
470+
471+ {-
472+ instance Prelude.Functor (Either a) where
473+ (<$>) _ (Left x) = Left x
474+ (<$>) f (Right y) = Right (f y)
475+ -}
476+
477+ instance Prelude.Applicative (Either e) where
478+ pure = Right
479+ (<*>) (Left e) _ = Left e
480+ (<*>) (Right f) r = f <$> r
444
481
445 instance Prelude.Monad (Either e) where
482 instance Prelude.Monad (Either e) where
446 return = Right
483 return = Right
447 (>>=) = either (\e _ -> Left e) (\a f -> f a)
484 (>>=) = either (\e _ -> Left e) (\a f -> f a)
485+
486+ instance (Show a, Show b) => Prelude.Show (Either a b) where
487+ show (Left x) = "Left " ++ (show x)
488+ show (Right y) = "Right " ++ (show y)
448
489
449module Arrays where
490module Arrays where
450
491
@@ -578,7 +619,7 @@ module Arrays where
578 range lo hi = {
619 range lo hi = {
579 var ns = [];
620 var ns = [];
580 for (n <- lo until hi) {
621 for (n <- lo until hi) {
581− ns = push ns n;
622+ ns = push ns n;
582 }
623 }
583 return ns;
624 return ns;
584 }
625 }
@@ -598,7 +639,10 @@ module Arrays where
598 instance (Prelude.Show a) => Prelude.Show [a] where
639 instance (Prelude.Show a) => Prelude.Show [a] where
599 show [] = "[]"
640 show [] = "[]"
600 show (x:xs) = show x ++ " : " ++ show xs
641 show (x:xs) = show x ++ " : " ++ show xs
601−
642+
643+ instance Prelude.Functor [] where
644+ (<$>) = map
645+
602 instance Prelude.Monad [] where
646 instance Prelude.Monad [] where
603 return = singleton
647 return = singleton
604 (>>=) = concatMap
648 (>>=) = concatMap
src/Language/PureScript.hs+1−1
@@ -67,4 +67,4 @@ compile opts ms = do
67 Left "Main.main is undefined"
67 Left "Main.main is undefined"
68 return $ js ++ [JSApp (JSAccessor "main" (JSVar "Main")) []]
68 return $ js ++ [JSApp (JSAccessor "main" (JSVar "Main")) []]
69 | otherwise -> return js
69 | otherwise -> return js
70− return (prettyPrintJS js', exts, env)
70+ return (prettyPrintJS [(wrapExportsContainer js')], exts, env)
src/Language/PureScript/CodeGen/JS.hs+11−1
@@ -18,7 +18,8 @@
18module Language.PureScript.CodeGen.JS (
18module Language.PureScript.CodeGen.JS (
19 module AST,
19 module AST,
20 declToJs,
20 declToJs,
21− moduleToJs
21+ moduleToJs,
22+ wrapExportsContainer
22) where
23) where
23
24
24import Data.Maybe (fromMaybe, mapMaybe)
25import Data.Maybe (fromMaybe, mapMaybe)
@@ -55,6 +56,7 @@ moduleToJs opts (Module pname@(ProperName name) decls) env =
55 (JSBlock (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts (ModuleName pname) decl env) (decls))))
56 (JSBlock (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts (ModuleName pname) decl env) (decls))))
56 [JSAssignment (JSVar name)
57 [JSAssignment (JSVar name)
57 (JSBinary Or (JSVar name) (JSObjectLiteral []))]
58 (JSBinary Or (JSVar name) (JSObjectLiteral []))]
59+ , JSAssignment (JSAccessor name (JSVar "exports")) (JSVar name)
58 ]
60 ]
59
61
60-- |
62-- |
@@ -325,3 +327,11 @@ statementToJs opts m e (If ifst) = ifToJs ifst
325 elseToJs (Else sts) = JSBlock (map (statementToJs opts m e) sts)
327 elseToJs (Else sts) = JSBlock (map (statementToJs opts m e) sts)
326 elseToJs (ElseIf elif) = ifToJs elif
328 elseToJs (ElseIf elif) = ifToJs elif
327statementToJs opts m e (Return value) = JSReturn (valueToJs opts m e value)
329statementToJs opts m e (Return value) = JSReturn (valueToJs opts m e value)
330+
331+wrapExportsContainer :: [JS] -> JS
332+wrapExportsContainer modules = JSApp (JSFunction Nothing ["exports"] $ JSBlock $ (JSStringLiteral "use strict") : modules) [exportSelector]
333+ where exportSelector = JSConditional (JSBinary And (JSBinary NotEqualTo (JSTypeOf $ JSVar "module") (JSStringLiteral "undefined")) (JSAccessor "exports" (JSVar "module")))
334+ (JSAccessor "exports" (JSVar "module"))
335+ (JSConditional (JSBinary NotEqualTo (JSTypeOf $ JSVar "window") (JSStringLiteral "undefined"))
336+ (JSAssignment (JSAccessor "PS" (JSVar "window")) (JSObjectLiteral []))
337+ (JSApp (JSFunction Nothing [] $ JSBlock [JSThrow $ JSStringLiteral "PureScript doesn't know how to export modules in the current environment"]) []))
src/Language/PureScript/Pretty/JS.hs+2−1
@@ -69,6 +69,7 @@ literals = mkPattern' match
69 , fmap (intercalate ", ") $ forM xs prettyPrintJS'
69 , fmap (intercalate ", ") $ forM xs prettyPrintJS'
70 , return " ]"
70 , return " ]"
71 ]
71 ]
72+ match (JSObjectLiteral []) = return "{}"
72 match (JSObjectLiteral ps) = fmap concat $ sequence
73 match (JSObjectLiteral ps) = fmap concat $ sequence
73 [ return "{\n"
74 [ return "{\n"
74 , withIndent $ do
75 , withIndent $ do
@@ -107,7 +108,7 @@ literals = mkPattern' match
107 , prettyPrintJS' sts
108 , prettyPrintJS' sts
108 ]
109 ]
109 match (JSFor ident start end sts) = fmap concat $ sequence
110 match (JSFor ident start end sts) = fmap concat $ sequence
110− [ return $ "for (" ++ ident ++ " = "
111+ [ return $ "for (var " ++ ident ++ " = "
111 , prettyPrintJS' start
112 , prettyPrintJS' start
112 , return $ "; " ++ ident ++ " < "
113 , return $ "; " ++ ident ++ " < "
113 , prettyPrintJS' end
114 , prettyPrintJS' end