git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #183 from garyb/master

Potential fix for #182

commit3ca411c6c7fa1f73fd1559ed8d49af3c4cde56fc authorPhil Freeman <paf31@cantab.net> dateSun 9 Feb 2014, 09:26 (13 years ago) parent1238f29f0e4444
4 files changed+60−5
prelude/prelude.purs+46−2 src/Language/PureScript.hs+1−1 src/Language/PureScript/CodeGen/JS.hs+11−1 src/Language/PureScript/Pretty/JS.hs+2−1
prelude/prelude.purs+46−2
@@ -54,6 +54,13 @@ module Prelude where
5454 instance Read Boolean where
5555 read "true" = true
5656 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
5764
5865 infixl 4 <$>
5966
@@ -362,6 +369,10 @@ module Monoid where
362369 instance Monoid String where
363370 mempty = ""
364371 (<>) = (++)
372+
373+ instance Monoid [a] where
374+ mempty = []
375+ (<>) = Arrays.concat
365376
366377 mconcat :: forall m. (Monoid m) => [m] -> m
367378 mconcat [] = mempty
@@ -429,22 +440,52 @@ module Maybe where
429440
430441 fromMaybe :: forall a. a -> Maybe a -> a
431442 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
432452
433453 instance Prelude.Monad Maybe where
434454 return = Just
435455 (>>=) 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"
436460
437461module Either where
438462
463+ import Prelude
464+
439465 data Either a b = Left a | Right b
440466
441467 either :: forall a b c. (a -> c) -> (b -> c) -> Either a b -> c
442468 either f _ (Left a) = f a
443469 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
444481
445482 instance Prelude.Monad (Either e) where
446483 return = Right
447484 (>>=) = 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)
448489
449490module Arrays where
450491
@@ -578,7 +619,7 @@ module Arrays where
578619 range lo hi = {
579620 var ns = [];
580621 for (n <- lo until hi) {
581− ns = push ns n;
622+ ns = push ns n;
582623 }
583624 return ns;
584625 }
@@ -598,7 +639,10 @@ module Arrays where
598639 instance (Prelude.Show a) => Prelude.Show [a] where
599640 show [] = "[]"
600641 show (x:xs) = show x ++ " : " ++ show xs
601−
642+
643+ instance Prelude.Functor [] where
644+ (<$>) = map
645+
602646 instance Prelude.Monad [] where
603647 return = singleton
604648 (>>=) = concatMap
src/Language/PureScript.hs+1−1
@@ -67,4 +67,4 @@ compile opts ms = do
6767 Left "Main.main is undefined"
6868 return $ js ++ [JSApp (JSAccessor "main" (JSVar "Main")) []]
6969 | 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 @@
1818module Language.PureScript.CodeGen.JS (
1919 module AST,
2020 declToJs,
21− moduleToJs
21+ moduleToJs,
22+ wrapExportsContainer
2223) where
2324
2425import Data.Maybe (fromMaybe, mapMaybe)
@@ -55,6 +56,7 @@ moduleToJs opts (Module pname@(ProperName name) decls) env =
5556 (JSBlock (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts (ModuleName pname) decl env) (decls))))
5657 [JSAssignment (JSVar name)
5758 (JSBinary Or (JSVar name) (JSObjectLiteral []))]
59+ , JSAssignment (JSAccessor name (JSVar "exports")) (JSVar name)
5860 ]
5961
6062-- |
@@ -325,3 +327,11 @@ statementToJs opts m e (If ifst) = ifToJs ifst
325327 elseToJs (Else sts) = JSBlock (map (statementToJs opts m e) sts)
326328 elseToJs (ElseIf elif) = ifToJs elif
327329statementToJs 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
6969 , fmap (intercalate ", ") $ forM xs prettyPrintJS'
7070 , return " ]"
7171 ]
72+ match (JSObjectLiteral []) = return "{}"
7273 match (JSObjectLiteral ps) = fmap concat $ sequence
7374 [ return "{\n"
7475 , withIndent $ do
@@ -107,7 +108,7 @@ literals = mkPattern' match
107108 , prettyPrintJS' sts
108109 ]
109110 match (JSFor ident start end sts) = fmap concat $ sequence
110− [ return $ "for (" ++ ident ++ " = "
111+ [ return $ "for (var " ++ ident ++ " = "
111112 , prettyPrintJS' start
112113 , return $ "; " ++ ident ++ " < "
113114 , prettyPrintJS' end