@@ -29,6 +29,7 @@ import Data.List (intercalate, isPrefixOf, nub, sort)
2929import Data.Maybe (mapMaybe)
3030import Data.Traversable (traverse)
3131
32+
3233import System.Console.Haskeline
3334import System.Directory (findExecutable)
3435import System.Exit
@@ -38,7 +39,8 @@ import System.Process
3839import qualified Language.PureScript as P
3940import qualified Paths_purescript as Paths
4041import qualified System.IO.UTF8 as U (readFile)
41−import qualified Text.Parsec as Parsec (Parsec, eof)
42+import qualified Text.Parsec as Parsec (Parsec, eof, try)
43+import qualified Data.Map as M
4244
4345-- |
4446-- The PSCI state.
@@ -182,24 +184,25 @@ options = P.Options True False True (Just "Main") True "PS" []
182184-- |
183185-- Makes a volatile module to execute the current expression.
184186--
185−createTemporaryModule :: [P.ProperName] -> [P.Value -> P.Value] -> P.Value -> P.Module
186−createTemporaryModule imports lets value =
187+createTemporaryModule :: Bool -> [P.ProperName] -> [P.Value -> P.Value] -> P.Value -> P.Module
188+createTemporaryModule exec imports lets value =
187189 let
188190 moduleName = P.ModuleName [P.ProperName "Main"]
189191 importDecl m = P.ImportDeclaration m Nothing
190192 traceModule = P.ModuleName [P.ProperName "Trace"]
191193 trace = P.Var (P.Qualified (Just traceModule) (P.Ident "print"))
192194 value' = foldr ($) value lets
193− mainDecl = P.ValueDeclaration (P.Ident "main") [] Nothing (P.App trace value')
195+ itDecl = P.ValueDeclaration (P.Ident "it") [] Nothing value'
196+ mainDecl = P.ValueDeclaration (P.Ident "main") [] Nothing (P.App trace (P.Var (P.Qualified Nothing (P.Ident "it"))))
194197 in
195− P.Module moduleName $ map (importDecl . P.ModuleName . return) imports ++ [mainDecl]
198+ P.Module moduleName $ map (importDecl . P.ModuleName . return) imports ++ if exec then [itDecl, mainDecl] else [itDecl]
196199
197200-- |
198201-- Takes a value declaration and evaluates it with the current state.
199202--
200203handleDeclaration :: P.Value -> PSCI -> InputT IO ()
201204handleDeclaration value (PSCI imports loadedModules lets) = do
202− let m = createTemporaryModule imports lets value
205+ let m = createTemporaryModule True imports lets value
203206 case P.compile options (loadedModules ++ [m]) of
204207 Left err -> outputStrLn err
205208 Right (js, _, _) -> do
@@ -210,6 +213,19 @@ handleDeclaration value (PSCI imports loadedModules lets) = do
210213 Just (ExitFailure _, _, err) -> outputStrLn err
211214 Nothing -> outputStrLn "Couldn't find node.js"
212215
216+-- |
217+-- Takes a value and prints its type
218+--
219+handleTypeOf :: P.Value -> PSCI -> InputT IO ()
220+handleTypeOf value (PSCI imports loadedModules lets) = do
221+ let m = createTemporaryModule False imports lets value
222+ case P.compile options { P.optionsMain = Nothing } (loadedModules ++ [m]) of
223+ Left err -> outputStrLn err
224+ Right (_, _, env') -> do
225+ case M.lookup (P.ModuleName [P.ProperName "Main"], P.Ident "it") (P.names env') of
226+ Just (ty, _) -> outputStrLn . P.prettyPrintType $ ty
227+ Nothing -> outputStrLn "Could not find type"
228+
213229-- Parser helpers
214230
215231-- |
@@ -235,12 +251,14 @@ parseExpression = P.whiteSpace *> P.parseValue <* Parsec.eof
235251handleCommand :: Command -> StateT PSCI (InputT IO) ()
236252handleCommand Empty = return ()
237253handleCommand (Expression ls) =
238− case P.runIndentParser "" parseLet (unlines ls) of
239− Left _ ->
240− case P.runIndentParser "" parseExpression (unlines ls) of
241− Left err -> inputTToState $ outputStrLn (show err)
242− Right decl -> get >>= inputTToState . handleDeclaration decl
243− Right l -> modify (updateLets l)
254+ case P.runIndentParser "" (Left <$> Parsec.try parseLet <|> Right <$> parseExpression) (unlines ls) of
255+ Left err -> inputTToState $ outputStrLn (show err)
256+ Right (Left l) -> modify (updateLets l)
257+ Right (Right decl) -> get >>= inputTToState . handleDeclaration decl
258+handleCommand (TypeOf expr) =
259+ case P.runIndentParser "" parseExpression expr of
260+ Left err -> inputTToState $ outputStrLn (show err)
261+ Right expr' -> get >>= inputTToState . handleTypeOf expr'
244262handleCommand Help = inputTToState $ outputStrLn helpMessage
245263handleCommand (Import moduleName) = modify (updateImports moduleName)
246264handleCommand (LoadFile filePath) = do