-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.hs
52 lines (43 loc) · 1.32 KB
/
Main.hs
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
module Main where
import Control.Monad.Trans
import System.Console.Haskeline
import System.Environment
import Syntax (prettyTy)
import Parser (parseExpr)
import Typecheck (typecheckExpr)
ansiGreen :: String
ansiGreen = "\x1b[32m"
ansiRed :: String
ansiRed = "\x1b[31m"
ansiReset :: String
ansiReset = "\x1b[0m"
process :: String -> IO ()
process line = case parseExpr line of
Left err -> print err
Right ex -> do
putStrLn $ "[Expr]: " ++ line
putStrLn $ "[Typecheck] " ++ res
-- putStrLn $ "[AST]: " ++ show ex
putStrLn ""
where res = case typecheckExpr ex of
Left err -> "[" ++ ansiRed ++ "FAIL" ++ ansiReset ++ "]: " ++ err
Right ty -> "[" ++ ansiGreen ++ "OK" ++ ansiReset ++ "]: " ++ prettyTy ty
-- https://hackage.haskell.org/package/haskeline-0.7.3.1/docs/System-Console-Haskeline.html
runRepl :: IO ()
runRepl = runInputT defaultSettings loop
where
loop = do
minput <- getInputLine "> "
case minput of
Nothing -> outputStrLn "Goodbye."
Just input -> liftIO (process input) >> loop
loadFile :: String -> IO ()
loadFile filePath = do
src <- readFile filePath
mapM_ process [line | line <- lines src, line /= ""]
main :: IO ()
main = do
args <- getArgs
case args of
[] -> runRepl
(x:_) -> loadFile x