git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
First pass at let bindings.
commit2410dad1807b041814fca29eb4f29194e95ef742 authorjoneshf <jones3.hardy@gmail.com> dateTue 18 Feb 2014, 01:33 (13 years ago) parent6144a2a
2 files changed+65−40
psci/Commands.hs+30 psci/Main.hs+35−40
psci/Commands.hsnew file+30
@@ -0,0 +1,30 @@
1+module Commands where
2+
3+import Control.Applicative
4+import System.Console.Haskeline
5+
6+data Command
7+ = Empty
8+ | Expression [String]
9+ | Import String
10+ | Let String
11+ | LoadModule FilePath
12+ | Reload deriving (Show, Eq)
13+
14+getCommand :: InputT IO Command
15+getCommand = do
16+ firstLine <- getInputLine "> "
17+ case firstLine of
18+ Nothing -> return Empty
19+ Just (':':'i':' ':moduleName) -> return $ Import moduleName
20+ Just (':':'m':' ':modulePath) -> return $ LoadModule modulePath
21+ Just ":r" -> return Reload
22+ Just (':':_) -> outputStrLn "Unknown command" >> getCommand
23+ Just l@('l':'e':'t':_) -> return $ Let l
24+ Just other -> Expression <$> go [other]
25+ where
26+ go ls = do
27+ l <- getInputLine " "
28+ case l of
29+ Nothing -> return $ reverse ls
30+ Just l' -> go (l' : ls)
psci/Main.hs+35−40
@@ -16,6 +16,8 @@
1616
1717module Main where
1818
19+import Commands
20+
1921import Control.Applicative
2022import Control.Monad
2123import Control.Monad.Trans.Class
@@ -34,7 +36,7 @@ import System.Process
3436import qualified Language.PureScript as P
3537import qualified Paths_purescript as Paths
3638import qualified System.IO.UTF8 as U (readFile)
37−import qualified Text.Parsec as Parsec (eof)
39+import qualified Text.Parsec as Parsec (Parsec, eof)
3840
3941getHistoryFilename :: IO FilePath
4042getHistoryFilename = getUserConfigFile "purescript" "psci_history"
@@ -46,6 +48,9 @@ findNodeProcess :: IO (Maybe String)
4648findNodeProcess = runMaybeT . msum $ map (MaybeT . findExecutable) names
4749 where names = ["nodejs", "node"]
4850
51+defaultImports :: [P.ProperName]
52+defaultImports = [P.ProperName "Prelude", P.ProperName "Eff"]
53+
4954options :: P.Options
5055options = P.Options True False True (Just "Main") True "PS" []
5156
@@ -67,23 +72,24 @@ completion ms = completeWord Nothing " \t\n\r" findCompletions
6772 getDeclName (P.ValueDeclaration ident _ _ _) = Just ident
6873 getDeclName _ = Nothing
6974
70−createTemporaryModule :: [P.ProperName] -> P.Value -> P.Module
71−createTemporaryModule imports value =
75+createTemporaryModule :: [P.ProperName] -> [P.DoNotationElement] -> P.Value -> P.Module
76+createTemporaryModule imports lets value =
7277 let
7378 moduleName = P.ModuleName [P.ProperName "Main"]
7479 importDecl m = P.ImportDeclaration m Nothing
7580 traceModule = P.ModuleName [P.ProperName "Trace"]
7681 trace = P.Var (P.Qualified (Just traceModule) (P.Ident "print"))
7782 mainDecl = P.ValueDeclaration (P.Ident "main") [] Nothing
78− (P.Do [ P.DoNotationBind (P.VarBinder (P.Ident "it")) value
83+ (P.Do (lets ++
84+ [ P.DoNotationBind (P.VarBinder (P.Ident "it")) value
7985 , P.DoNotationValue (P.App trace (P.Var (P.Qualified Nothing (P.Ident "it"))) )
80− ])
86+ ]))
8187 in
8288 P.Module moduleName $ map (importDecl . P.ModuleName . return) imports ++ [mainDecl]
8389
84−handleDeclaration :: [P.Module] -> [P.ProperName] -> P.Value -> InputT IO ()
85−handleDeclaration loadedModules imports value = do
86− let m = createTemporaryModule imports value
90+handleDeclaration :: [P.Module] -> [P.ProperName] -> [P.DoNotationElement] -> P.Value -> InputT IO ()
91+handleDeclaration loadedModules imports lets value = do
92+ let m = createTemporaryModule imports lets value
8793 case P.compile options (loadedModules ++ [m]) of
8894 Left err -> outputStrLn err
8995 Right (js, _, _) -> do
@@ -94,36 +100,19 @@ handleDeclaration loadedModules imports value = do
94100 Just (ExitFailure _, _, err) -> outputStrLn err
95101 Nothing -> outputStrLn "Couldn't find node.js"
96102
97−data Command
98− = Empty
99− | Expression [String]
100− | Import String
101− | LoadModule FilePath
102− | Reload deriving (Show, Eq)
103−
104−getCommand :: InputT IO Command
105−getCommand = do
106− firstLine <- getInputLine "> "
107− case firstLine of
108− Nothing -> return Empty
109− Just (':':'i':' ':moduleName) -> return $ Import moduleName
110− Just (':':'m':' ':modulePath) -> return $ LoadModule modulePath
111− Just ":r" -> return Reload
112− Just (':':_) -> outputStrLn "Unknown command" >> getCommand
113− Just other -> Expression <$> go [other]
114− where
115− go ls = do
116− l <- getInputLine " "
117− case l of
118− Nothing -> return $ reverse ls
119− Just l' -> go (l' : ls)
120−
121103loadModule :: FilePath -> IO (Either String [P.Module])
122104loadModule moduleFile = do
123105 print moduleFile
124106 moduleText <- U.readFile moduleFile
125107 return . either (Left . show) Right $ P.runIndentParser "" P.parseModules moduleText
126108
109+parseDoNotationLet :: Parsec.Parsec String P.ParseState P.DoNotationElement
110+parseDoNotationLet = P.DoNotationLet <$> (P.reserved "let" *> P.indented *> P.parseBinder)
111+ <*> (P.indented *> P.reservedOp "=" *> P.parseValue)
112+
113+parseDoNotationBind :: Parsec.Parsec String P.ParseState P.DoNotationElement
114+parseDoNotationBind = P.DoNotationBind <$> P.parseBinder <*> (P.indented *> P.reservedOp "<-" *> P.parseValue)
115+
127116main :: IO ()
128117main = do
129118 preludeFilename <- getPreludeFilename
@@ -139,26 +128,32 @@ main = do
139128 outputStrLn " |_| "
140129 outputStrLn ""
141130 outputStrLn "Expressions are terminated using Ctrl+D"
142− go [P.ProperName "Prelude", P.ProperName "Eff"] prelude
131+ go defaultImports prelude []
143132 where
144− go imports loadedModules = do
133+ go :: [P.ProperName] -> [P.Module] -> [P.DoNotationElement] -> InputT IO ()
134+ go imports loadedModules lets = do
145135 cmd <- getCommand
146136 case cmd of
147− Empty -> go imports loadedModules
137+ Empty -> go imports loadedModules lets
148138 Expression ls -> do
149139 case P.runIndentParser "" (P.whiteSpace *> P.parseValue <* Parsec.eof) (unlines ls) of
150140 Left err -> outputStrLn (show err)
151− Right decl -> handleDeclaration loadedModules imports decl
152− go imports loadedModules
153− Import moduleName -> go (imports ++ [P.ProperName moduleName]) loadedModules
141+ Right decl -> handleDeclaration loadedModules imports lets decl
142+ go imports loadedModules lets
143+ Import moduleName -> go (imports ++ [P.ProperName moduleName]) loadedModules lets
144+ Let line -> do
145+ moreLets <- case P.runIndentParser "" parseDoNotationLet line of
146+ Left err -> outputStrLn (show err) >> return lets
147+ Right l -> return $ lets ++ [l]
148+ go imports loadedModules moreLets
154149 LoadModule moduleFile -> do
155150 ms <- lift $ loadModule moduleFile
156151 case ms of
157152 Left err -> outputStrLn err
158− Right ms' -> go imports (loadedModules ++ ms')
153+ Right ms' -> go imports (loadedModules ++ ms') lets
159154 Reload -> do
160155 preludeFilename <- lift getPreludeFilename
161156 (Right prelude) <- lift $ loadModule preludeFilename
162− go [P.ProperName "Prelude"] prelude
157+ go defaultImports prelude lets
163158
164159