blob: 51e5815e722389212a3f5607f175ef32050b3bf2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
module Main where
import Data.Char
import Data.List
import System.Console.Readline
import System.Environment
import System.Exit
import System.IO.Error
import Compiler
import Interpreter
import Optimiser
import Parser
import Stdlib
import VM
usage :: IO ()
usage = do
progname <- getProgName
putStrLn $ "Usage: " ++ progname ++ " [filename.lisp]"
repl :: Context -> IO ()
repl ctx = do
mline <- fmap (fmap strip) (readline "> ")
case mline of
Nothing -> putStrLn ""
Just "" -> repl ctx
Just (';' : _) -> repl ctx
Just line -> do
addHistory line
case parseExpression line of
Right val -> do
ires <- interpret ctx val
case ires of
Right (retval, ctx') -> do
putStrLn $ "\x1B[36m" ++ show retval ++ "\x1B[0m"
repl ctx'
Left err -> do
putStrLn $ "\x1B[31;1mError: " ++ err ++ "\x1B[0m"
repl ctx
Left err -> do
putStrLn $ "\x1B[31;1mParse error:\n" ++ show err ++ "\x1B[0m"
repl ctx
runFile :: String -> Context -> IO ()
runFile fname ctx = do
source <- readFile fname
case parseProgram source of
Right ast -> do
res <- interpretProgram ctx ast
case res of
Right _ -> return ()
Left err -> die $ "Error: " ++ err
Left err -> die $ "Parse error:\n" ++ show err
strip :: String -> String
strip = dropWhileEnd isSpace . dropWhile isSpace
handleEOFError :: IO () -> IO ()
handleEOFError op = catchIOError op (\e -> if isEOFError e then putStrLn "" else ioError e)
-- main :: IO ()
-- main = do
-- clargs <- getArgs
-- Right ctx <- interpretProgram newContext stdlib
-- case clargs of
-- [] -> handleEOFError (repl ctx)
-- [fname] -> runFile fname ctx
-- _ -> usage >> exitFailure
main :: IO ()
main = do
clargs <- getArgs
source <- case clargs of
[] -> getContents
[fname] -> readFile fname
_ -> usage >> exitFailure
prog <- either (die . show) return (parseProgram source)
irprog <- either die return (compileProgram prog)
let opt = optimise irprog
-- print opt
vmRun opt
|