git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Support single-line mode for REPL server
1bf7686Phil Freeman
psci/Main.hs+18−11
@@ -276,7 +276,7 @@ handleDeclaration :: P.Expr -> PSCI ()
276handleDeclaration value = do
276handleDeclaration value = do
277 st <- PSCI $ lift get
277 st <- PSCI $ lift get
278 let m = createTemporaryModule True st value
278 let m = createTemporaryModule True st value
279− e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [("$PSCI.purs", m)]) []
279+ e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [("$PSCI.purs", m)]) []
280 case e of
280 case e of
281 Left err -> PSCI $ outputStrLn err
281 Left err -> PSCI $ outputStrLn err
282 Right _ -> do
282 Right _ -> do
@@ -295,7 +295,7 @@ handleTypeOf :: P.Expr -> PSCI ()
295handleTypeOf value = do
295handleTypeOf value = do
296 st <- PSCI $ lift get
296 st <- PSCI $ lift get
297 let m = createTemporaryModule False st value
297 let m = createTemporaryModule False st value
298− e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [("$PSCI.purs", m)]) []
298+ e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [("$PSCI.purs", m)]) []
299 case e of
299 case e of
300 Left err -> PSCI $ outputStrLn err
300 Left err -> PSCI $ outputStrLn err
301 Right env' ->
301 Right env' ->
@@ -311,7 +311,7 @@ handleKindOf typ = do
311 st <- PSCI $ lift get
311 st <- PSCI $ lift get
312 let m = createTemporaryModuleForKind st typ
312 let m = createTemporaryModuleForKind st typ
313 mName = P.ModuleName [P.ProperName "$PSCI"]
313 mName = P.ModuleName [P.ProperName "$PSCI"]
314− e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [("$PSCI.purs", m)]) []
314+ e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st ++ [("$PSCI.purs", m)]) []
315 case e of
315 case e of
316 Left err -> PSCI $ outputStrLn err
316 Left err -> PSCI $ outputStrLn err
317 Right env' ->
317 Right env' ->
@@ -329,13 +329,13 @@ handleKindOf typ = do
329-- |
329-- |
330-- Parses the input and returns either a Metacommand or an expression.
330-- Parses the input and returns either a Metacommand or an expression.
331--
331--
332−getCommand :: InputT (StateT PSCiState IO) (Either ParseError (Maybe Command))
332+getCommand :: Bool -> InputT (StateT PSCiState IO) (Either ParseError (Maybe Command))
333−getCommand = do
333+getCommand singleLineMode = do
334 firstLine <- getInputLine "> "
334 firstLine <- getInputLine "> "
335 case firstLine of
335 case firstLine of
336 Nothing -> return (Right Nothing)
336 Nothing -> return (Right Nothing)
337 Just "" -> return (Right Nothing)
337 Just "" -> return (Right Nothing)
338− Just s@ (':' : _) -> return . either Left (Right . Just) $ parseCommand s -- The start of a command
338+ Just s | singleLineMode || head s == ':' -> return . either Left (Right . Just) $ parseCommand s
339 Just s -> either Left (Right . Just) . parseCommand <$> go [s]
339 Just s -> either Left (Right . Just) . parseCommand <$> go [s]
340 where
340 where
341 go :: [String] -> InputT (StateT PSCiState IO) String
341 go :: [String] -> InputT (StateT PSCiState IO) String
@@ -371,9 +371,16 @@ handleCommand (TypeOf val) = handleTypeOf val
371handleCommand (KindOf typ) = handleKindOf typ
371handleCommand (KindOf typ) = handleKindOf typ
372handleCommand _ = PSCI $ outputStrLn "Unknown command"
372handleCommand _ = PSCI $ outputStrLn "Unknown command"
373
373
374+singleLineFlag :: Cmd.Term Bool
375+singleLineFlag = Cmd.value $ Cmd.flag $ (Cmd.optInfo ["single-line-mode"])
376+ { Cmd.optName = "Single-line mode"
377+ , Cmd.optDoc = "Run in single-line mode"
378+ }
379+
374inputFiles :: Cmd.Term [FilePath]
380inputFiles :: Cmd.Term [FilePath]
375inputFiles = Cmd.value $ Cmd.posAny [] $ Cmd.posInfo { Cmd.posName = "file(s)"
381inputFiles = Cmd.value $ Cmd.posAny [] $ Cmd.posInfo { Cmd.posName = "file(s)"
376− , Cmd.posDoc = "Optional .purs files to load on start" }
382+ , Cmd.posDoc = "Optional .purs files to load on start"
383+ }
377
384
378loadUserConfig :: IO (Maybe [Command])
385loadUserConfig :: IO (Maybe [Command])
379loadUserConfig = do
386loadUserConfig = do
@@ -391,8 +398,8 @@ loadUserConfig = do
391-- |
398-- |
392-- The PSCI main loop.
399-- The PSCI main loop.
393--
400--
394−loop :: [FilePath] -> IO ()
401+loop :: Bool -> [FilePath] -> IO ()
395−loop files = do
402+loop singleLineMode files = do
396 config <- loadUserConfig
403 config <- loadUserConfig
397 preludeFilename <- P.preludeFilename
404 preludeFilename <- P.preludeFilename
398 filesAndModules <- mapM (\file -> fmap (fmap (map ((,) file))) . loadModule $ file) (preludeFilename : files)
405 filesAndModules <- mapM (\file -> fmap (fmap (map ((,) file))) . loadModule $ file) (preludeFilename : files)
@@ -409,7 +416,7 @@ loop files = do
409 where
416 where
410 go :: InputT (StateT PSCiState IO) ()
417 go :: InputT (StateT PSCiState IO) ()
411 go = do
418 go = do
412− c <- getCommand
419+ c <- getCommand singleLineMode
413 case c of
420 case c of
414 Left err -> outputStrLn (show err) >> go
421 Left err -> outputStrLn (show err) >> go
415 Right Nothing -> go
422 Right Nothing -> go
@@ -417,7 +424,7 @@ loop files = do
417 Right (Just c') -> runPSCI (handleCommand c') >> go
424 Right (Just c') -> runPSCI (handleCommand c') >> go
418
425
419term :: Cmd.Term (IO ())
426term :: Cmd.Term (IO ())
420−term = loop <$> inputFiles
427+term = loop <$> singleLineFlag <*> inputFiles
421
428
422termInfo :: Cmd.TermInfo
429termInfo :: Cmd.TermInfo
423termInfo = Cmd.defTI
430termInfo = Cmd.defTI