git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Flip order of let bindings and use foldl.
0af5229joneshf
psci/Main.hs+11−9
@@ -84,7 +84,7 @@ updateModules modules st = st { psciLoadedModules = psciLoadedModules st ++ modu
84-- Updates the state to have more let bindings.
84-- Updates the state to have more let bindings.
85--
85--
86updateLets :: (P.Value -> P.Value) -> PSCiState -> PSCiState
86updateLets :: (P.Value -> P.Value) -> PSCiState -> PSCiState
87−updateLets name st = st { psciLetBindings = psciLetBindings st ++ [name] }
87+updateLets name st = st { psciLetBindings = name : psciLetBindings st }
88
88
89-- File helpers
89-- File helpers
90-- |
90-- |
@@ -194,25 +194,27 @@ options = P.Options True False True (Just "Main") True "PS" []
194-- |
194-- |
195-- Makes a volatile module to execute the current expression.
195-- Makes a volatile module to execute the current expression.
196--
196--
197−createTemporaryModule :: Bool -> [P.ModuleName] -> [P.Value -> P.Value] -> P.Value -> P.Module
197+createTemporaryModule :: Bool -> PSCiState -> P.Value -> P.Module
198−createTemporaryModule exec imports lets value =
198+createTemporaryModule exec PSCiState{psciImportedModuleNames = imports, psciLetBindings = lets} value =
199 let
199 let
200 moduleName = P.ModuleName [P.ProperName "Main"]
200 moduleName = P.ModuleName [P.ProperName "Main"]
201 importDecl m = P.ImportDeclaration m Nothing
201 importDecl m = P.ImportDeclaration m Nothing
202 traceModule = P.ModuleName [P.ProperName "Debug", P.ProperName "Trace"]
202 traceModule = P.ModuleName [P.ProperName "Debug", P.ProperName "Trace"]
203 trace = P.Var (P.Qualified (Just traceModule) (P.Ident "print"))
203 trace = P.Var (P.Qualified (Just traceModule) (P.Ident "print"))
204− value' = foldr ($) value lets
204+ itValue = foldl (\x f -> f x) value lets
205− itDecl = P.ValueDeclaration (P.Ident "it") [] Nothing value'
206− mainDecl = P.ValueDeclaration (P.Ident "main") [] Nothing (P.App trace (P.Var (P.Qualified Nothing (P.Ident "it"))))
205+ mainValue = P.App trace (P.Var (P.Qualified Nothing (P.Ident "it")))
206+ itDecl = P.ValueDeclaration (P.Ident "it") [] Nothing itValue
207+ mainDecl = P.ValueDeclaration (P.Ident "main") [] Nothing mainValue
208+ decls = if exec then [itDecl, mainDecl] else [itDecl]
207 in
209 in
208− P.Module moduleName $ map importDecl imports ++ if exec then [itDecl, mainDecl] else [itDecl]
210+ P.Module moduleName $ map importDecl imports ++ decls
209
211
210-- |
212-- |
211-- Takes a value declaration and evaluates it with the current state.
213-- Takes a value declaration and evaluates it with the current state.
212--
214--
213handleDeclaration :: P.Value -> PSCiState -> InputT (StateT PSCiState IO) ()
215handleDeclaration :: P.Value -> PSCiState -> InputT (StateT PSCiState IO) ()
214handleDeclaration value st = do
216handleDeclaration value st = do
215− let m = createTemporaryModule True (psciImportedModuleNames st) (psciLetBindings st) value
217+ let m = createTemporaryModule True st value
216 case P.compile options (psciLoadedModules st ++ [m]) of
218 case P.compile options (psciLoadedModules st ++ [m]) of
217 Left err -> outputStrLn err
219 Left err -> outputStrLn err
218 Right (js, _, _) -> do
220 Right (js, _, _) -> do
@@ -228,7 +230,7 @@ handleDeclaration value st = do
228--
230--
229handleTypeOf :: P.Value -> PSCiState -> InputT (StateT PSCiState IO) ()
231handleTypeOf :: P.Value -> PSCiState -> InputT (StateT PSCiState IO) ()
230handleTypeOf value st = do
232handleTypeOf value st = do
231− let m = createTemporaryModule False (psciImportedModuleNames st) (psciLetBindings st) value
233+ let m = createTemporaryModule False st value
232 case P.compile options { P.optionsMain = Nothing } (psciLoadedModules st ++ [m]) of
234 case P.compile options { P.optionsMain = Nothing } (psciLoadedModules st ++ [m]) of
233 Left err -> outputStrLn err
235 Left err -> outputStrLn err
234 Right (_, _, env') ->
236 Right (_, _, env') ->