Skip to content

Commit 3b05d62

Browse files
committed
Moved REPL code into it's own file.
Core.hs now only contains core eval code.
1 parent 6829df8 commit 3b05d62

File tree

2 files changed

+11
-79
lines changed

2 files changed

+11
-79
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
husk: core.hs macro.hs numerical.hs parser.hs types.hs variables.hs
2-
ghc --make -package parsec -fglasgow-exts -o huski core.hs macro.hs numerical.hs parser.hs types.hs variables.hs
2+
ghc --make -package parsec -fglasgow-exts -o huski shell.hs core.hs macro.hs numerical.hs parser.hs types.hs variables.hs
33

44
# Run all unit tests
55
test: husk

core.hs

Lines changed: 10 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
-
1717
- -}
1818

19-
module Main where
19+
module Scheme.Core where
2020
import Scheme.Macro
2121
import Scheme.Numerical
2222
import Scheme.Parser
@@ -35,75 +35,7 @@ import IO hiding (try)
3535
import Numeric
3636
import Ratio
3737
import System.Environment
38-
import System.Console.Haskeline
39-
40-
main :: IO ()
41-
main = do args <- getArgs
42-
if null args then do showBanner
43-
runRepl
44-
else runOne $ args
45-
46-
-- REPL Section
47-
flushStr :: String -> IO ()
48-
flushStr str = putStr str >> hFlush stdout
49-
50-
evalString :: Env -> String -> IO String
51-
evalString env expr = runIOThrows $ liftM show $ (liftThrows $ readExpr expr) >>= macroEval env >>= eval env
52-
53-
evalAndPrint :: Env -> String -> IO ()
54-
evalAndPrint env expr = evalString env expr >>= putStrLn
55-
56-
runOne :: [String] -> IO ()
57-
runOne args = do
58-
env <-primitiveBindings >>= flip bindVars [((varNamespace, "args"), List $ map String $ drop 1 args)]
59-
(runIOThrows $ liftM show $ eval env (List [Atom "load", String (args !! 0)]))
60-
>>= hPutStrLn stderr -- echo this or not??
61-
62-
-- Call into (main) if it exists...
63-
alreadyDefined <- liftIO $ isBound env "main"
64-
let argv = List $ map String $ args
65-
if alreadyDefined
66-
then (runIOThrows $ liftM show $ eval env (List [Atom "main", List [Atom "quote", argv]])) >>= hPutStrLn stderr
67-
else (runIOThrows $ liftM show $ eval env $ Nil "") >>= hPutStrLn stderr
68-
69-
showBanner :: IO ()
70-
showBanner = do
71-
putStrLn " __ __ __ __ ______ __ __ "
72-
putStrLn "/\\ \\_\\ \\ /\\ \\/\\ \\ /\\ ___\\ /\\ \\/ / Scheme Interpreter "
73-
putStrLn "\\ \\ __ \\ \\ \\ \\_\\ \\ \\ \\___ \\ \\ \\ _\\\"-. Version 1.0"
74-
putStrLn " \\ \\_\\ \\_\\ \\ \\_____\\ \\/\\_____\\ \\ \\_\\ \\_\\ (c) 2010 Justin Ethier "
75-
putStrLn " \\/_/\\/_/ \\/_____/ \\/_____/ \\/_/\\/_/ github.com/justinethier/husk-scheme "
76-
putStrLn ""
77-
78-
runRepl :: IO ()
79-
runRepl = do
80-
env <- primitiveBindings
81-
evalString env "(load \"stdlib.scm\")" -- Load standard library into the REPL
82-
runInputT defaultSettings (loop env)
83-
where
84-
loop :: Env -> InputT IO ()
85-
loop env = do
86-
minput <- getInputLine "huski> "
87-
case minput of
88-
Nothing -> return ()
89-
Just "quit" -> return ()
90-
Just "" -> loop env -- FUTURE: integrate with strip to ignore inputs of just whitespace
91-
Just input -> do result <- liftIO (evalString env input)
92-
if (length result) > 0
93-
then do outputStrLn result
94-
loop env
95-
else loop env
96-
-- End REPL Section
9738

98-
{- Should not need this function, since we are using Haskell
99-
trampoline :: Env -> LispVal -> IOThrowsError LispVal
100-
trampoline env val = do
101-
result <- eval env val
102-
case result of
103-
-- If a form is not fully-evaluated to a value, bounce it back onto the trampoline...
104-
func@(Func params vararg body closure True) -> trampoline env func -- next iteration, via tail call (?)
105-
val -> return val
106-
-}
10739

10840
-- Eval section
10941
eval :: Env -> LispVal -> IOThrowsError LispVal
@@ -810,12 +742,12 @@ isBoolean ([Bool n]) = return $ Bool True
810742
isBoolean _ = return $ Bool False
811743
-- end Eval section
812744

813-
-- Begin Util section, of generic functions
814-
815-
-- Remove leading/trailing white space from a string; based on corresponding Python function
816-
-- Code taken from: http://gimbo.org.uk/blog/2007/04/20/splitting-a-string-in-haskell/
817-
strip :: String -> String
818-
strip s = dropWhile ws $ reverse $ dropWhile ws $ reverse s
819-
where ws = (`elem` [' ', '\n', '\t', '\r'])
820-
821-
-- End Util
745+
{- Should not need this function, since we are using Haskell
746+
trampoline :: Env -> LispVal -> IOThrowsError LispVal
747+
trampoline env val = do
748+
result <- eval env val
749+
case result of
750+
-- If a form is not fully-evaluated to a value, bounce it back onto the trampoline...
751+
func@(Func params vararg body closure True) -> trampoline env func -- next iteration, via tail call (?)
752+
val -> return val
753+
-}

0 commit comments

Comments
 (0)