git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #197 from joneshf/psci-fixes
0694c05Phil Freeman
psci/Main.hs+24−12
@@ -16,15 +16,19 @@
16
16
17module Main where
17module Main where
18
18
19−import Control.Monad.IO.Class
20import Control.Applicative
19import Control.Applicative
20+import Control.Monad
21import Control.Monad.Trans.Class
21import Control.Monad.Trans.Class
22+import Control.Monad.Trans.Maybe (MaybeT(..), runMaybeT)
22
23
23import Data.List (nub, isPrefixOf)
24import Data.List (nub, isPrefixOf)
24import Data.Maybe (mapMaybe)
25import Data.Maybe (mapMaybe)
26+import Data.Traversable (traverse)
25
27
26−import System.Process
27import System.Console.Haskeline
28import System.Console.Haskeline
29+import System.Directory (findExecutable)
30+import System.Exit
31+import System.Process
28
32
29import qualified Language.PureScript as P
33import qualified Language.PureScript as P
30import qualified Paths_purescript as Paths
34import qualified Paths_purescript as Paths
@@ -35,7 +39,7 @@ getPreludeFilename :: IO FilePath
35getPreludeFilename = Paths.getDataFileName "prelude/prelude.purs"
39getPreludeFilename = Paths.getDataFileName "prelude/prelude.purs"
36
40
37options :: P.Options
41options :: P.Options
38−options = P.Options True False True Nothing True "PS" []
42+options = P.Options True False True (Just "Main") True "PS" []
39
43
40completion :: [P.Module] -> CompletionFunc IO
44completion :: [P.Module] -> CompletionFunc IO
41completion ms = completeWord Nothing " \t\n\r" findCompletions
45completion ms = completeWord Nothing " \t\n\r" findCompletions
@@ -43,12 +47,12 @@ completion ms = completeWord Nothing " \t\n\r" findCompletions
43 findCompletions :: String -> IO [Completion]
47 findCompletions :: String -> IO [Completion]
44 findCompletions str = do
48 findCompletions str = do
45 files <- listFiles str
49 files <- listFiles str
46− let names = nub $ [ show qual
50+ let names = nub [ show qual
47− | P.Module moduleName ds <- ms
51+ | P.Module moduleName ds <- ms
48− , ident <- mapMaybe getDeclName ds
52+ , ident <- mapMaybe getDeclName ds
49− , qual <- [ P.Qualified Nothing ident
53+ , qual <- [ P.Qualified Nothing ident
50− , P.Qualified (Just (P.ModuleName moduleName)) ident]
54+ , P.Qualified (Just (P.ModuleName moduleName)) ident]
51− ]
55+ ]
52 let matches = filter (isPrefixOf str) names
56 let matches = filter (isPrefixOf str) names
53 return $ map simpleCompletion matches ++ files
57 return $ map simpleCompletion matches ++ files
54 getDeclName :: P.Declaration -> Maybe P.Ident
58 getDeclName :: P.Declaration -> Maybe P.Ident
@@ -75,8 +79,12 @@ handleDeclaration loadedModules imports value = do
75 case P.compile options (loadedModules ++ [m]) of
79 case P.compile options (loadedModules ++ [m]) of
76 Left err -> outputStrLn err
80 Left err -> outputStrLn err
77 Right (js, _, _) -> do
81 Right (js, _, _) -> do
78− output <- liftIO $ readProcess "nodejs" [] js
82+ process <- lift findNodeProcess
83+ result <- lift $ traverse (\node -> readProcessWithExitCode node [] js) process
84+ case result of
85+ Just (ExitSuccess, out, _) -> outputStrLn out
79− outputStrLn output
86+ Just (ExitFailure _, _, err) -> outputStrLn err
87+ Nothing -> outputStrLn "Couldn't find node.js"
80
88
81data Command
89data Command
82 = Empty
90 = Empty
@@ -108,6 +116,10 @@ loadModule moduleFile = do
108 moduleText <- U.readFile moduleFile
116 moduleText <- U.readFile moduleFile
109 return . either (Left . show) Right $ P.runIndentParser "" P.parseModules moduleText
117 return . either (Left . show) Right $ P.runIndentParser "" P.parseModules moduleText
110
118
119+findNodeProcess :: IO (Maybe String)
120+findNodeProcess = runMaybeT . msum $ map (MaybeT . findExecutable) names
121+ where names = ["nodejs", "node"]
122+
111main :: IO ()
123main :: IO ()
112main = do
124main = do
113 preludeFilename <- getPreludeFilename
125 preludeFilename <- getPreludeFilename
@@ -121,7 +133,7 @@ main = do
121 outputStrLn " |_| "
133 outputStrLn " |_| "
122 outputStrLn ""
134 outputStrLn ""
123 outputStrLn "Expressions are terminated using Ctrl+D"
135 outputStrLn "Expressions are terminated using Ctrl+D"
124− go [P.ProperName "Prelude"] prelude
136+ go [P.ProperName "Prelude", P.ProperName "Eff"] prelude
125 where
137 where
126 go imports loadedModules = do
138 go imports loadedModules = do
127 cmd <- getCommand
139 cmd <- getCommand
purescript.cabal+3−3
@@ -77,9 +77,9 @@ executable psc
77 ghc-options: -Wall -O2 -fno-warn-unused-do-bind
77 ghc-options: -Wall -O2 -fno-warn-unused-do-bind
78
78
79executable psci
79executable psci
80− build-depends: base >=4 && <5, containers -any, mtl -any,
80+ build-depends: base >=4 && <5, containers -any, directory -any,
81− parsec -any, haskeline <=0.7.1.1, purescript -any, syb -any,
81+ mtl -any, parsec -any, haskeline <=0.7.1.1, purescript -any,
82− transformers -any, utf8-string -any, process -any
82+ syb -any, transformers -any, utf8-string -any, process -any
83 main-is: Main.hs
83 main-is: Main.hs
84 buildable: True
84 buildable: True
85 hs-source-dirs: psci
85 hs-source-dirs: psci