16
16
-
17
17
- -}
18
18
19
- module Main where
19
+ module Scheme.Core where
20
20
import Scheme.Macro
21
21
import Scheme.Numerical
22
22
import Scheme.Parser
@@ -35,75 +35,7 @@ import IO hiding (try)
35
35
import Numeric
36
36
import Ratio
37
37
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
97
38
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
- -}
107
39
108
40
-- Eval section
109
41
eval :: Env -> LispVal -> IOThrowsError LispVal
@@ -810,12 +742,12 @@ isBoolean ([Bool n]) = return $ Bool True
810
742
isBoolean _ = return $ Bool False
811
743
-- end Eval section
812
744
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