-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmojito.hs
86 lines (72 loc) · 2.78 KB
/
mojito.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
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
85
86
module Main where
-- This program can act as a FastCGI executable when the
-- program name is simple.fcgi.
import System.Environment
import System.Exit (exitFailure)
import Text.PrettyPrint.HughesPJClass
import Test.HUnit
import Language.Mojito.Syntax.SExpr
import Language.Mojito.Syntax.ExprBuilder
import qualified Language.Mojito.Inference.Cardelli.Prelude as C
(someEnvironment)
import qualified Language.Mojito.Inference.Cardelli.Cardelli as C
(infer)
import Language.Mojito.Inference.SystemCT1999.Prelude
import Language.Mojito.Inference.SystemCT1999.SystemCT1999
main :: IO ()
main = do
who <- getProgName
if who == "simple.fcgi"
then putStrLn "TODO: simple.fcgi"
else normal
try :: String -> Either String a -> IO a
try msg e = case e of
Left err -> putStrLn (msg ++ err) >> exitFailure
Right a -> return a
normal :: IO ()
normal = do
as <- getArgs
case as of
["--help"] -> usage
["--run-tests"] -> tests
["--sexpr", s] -> do
sexpr <- try "Parse error: " $ parseSExprs' s
print sexpr
["--expr", s] -> do
sexpr <- try "Parse error: " $ parseSExprs' s
expr <- try "Wrong s-expression: " $ sexprsToExpr sexpr
print expr
["--milner", s] -> do
sexpr <- try "Parse error: " $ parseSExprs' s
expr <- try "Wrong s-expression: " $ sexprsToExpr sexpr
case C.infer expr C.someEnvironment of
((Left err,_),_) -> putStrLn $ "Type-checking failed: " ++ err
((Right typedExpr,_),_) -> print typedExpr
["--system-ct", s] -> do
sexpr <- try "Parse error: " $ parseSExprs' s
expr <- try "Wrong s-expression: " $ sexprsToExpr sexpr
case infer someTypes expr someContext of
((Left err,_),_) -> putStrLn $ "Type-checking failed: " ++ err
((Right (c,g),_),inf) -> do
typedExpr <- try "wrong final type: " $ duplicate' inf expr c g
print $ pPrint typedExpr
_ -> putStrLn "Unrecognized arguments." >> usage
usage :: IO ()
usage = do
me <- getProgName
putStr . unlines $
concat [ "Usage: ", me, " [OPTION]"] :
"Options:" :
" --help Print this message" :
" --run-tests Run the test suite" :
" --sexpr <string> Parse the string as an s-expression" :
" and print it" :
" --expr <string> Parse the string as an abstract syntax" :
" tree and print it." :
" --milner <string> Parse the string as an abstract syntax" :
" tree, infer the types, and p-print it." :
" --system-ct <string> Parse the string as an abstract syntax" :
" tree, infer the types, and p-print it." :
[]
tests :: IO ()
tests = putStrLn "TODO --run-tests"