-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.hs
More file actions
101 lines (95 loc) · 3.83 KB
/
Main.hs
File metadata and controls
101 lines (95 loc) · 3.83 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
-- Jan Miksa
import System.Environment
import System.IO
import SRC.Structure.Term
import SRC.Processor.Lexer
import SRC.Processor.Parser
import SRC.Processor.Evaluator
-- | Arguments
-- i - repl - if not set expects file to read
-- g - greedy eval mode
-- s - single Beta reduction step mode
main :: IO ()
main = do
args <- getArgs
if (length args) /= 0
then do
let e = if elem "-g" args
then (eval)
else if elem "-s" args
then (reduce)
else (reduce2Norm)
if elem "-i" args
then do
repl e
return ()
else do (fileE e (head (filterFileName args)))
else do
putStrLn(" ___ ___ ___ ___")
putStrLn(" /\\__\\ /\\__\\ /\\ \\ /\\ \\")
putStrLn(" /:/ / /::| | /::\\ \\ /::\\ \\")
putStrLn(" /:/ / /:|:| | /:/\\:\\ \\ /:/\\:\\ \\")
putStrLn(" /:/ / /:/|:|__|__ /::\\~\\:\\__\\ /:/ \\:\\__\\")
putStrLn(" /:/__/ /:/ |::::\\__\\ /:/\\:\\ \\:|__| /:/__/ \\:|__|")
putStrLn(" \\:\\ \\ \\/__/~~/:/ / \\:\\~\\:\\/:/ / \\:\\ \\ /:/ /")
putStrLn(" \\:\\ \\ /:/ / \\:\\ \\::\\ \\ \\:\\ /:/ /")
putStrLn(" \\:\\ \\ /:/ / \\:\\/:/ / \\:\\/:/ /")
putStrLn(" \\:\\__\\ /:/ / \\::/ / \\::/ /")
putStrLn(" \\/__/ \\/__/ \\/__/ \\/__/")
putStrLn("---------------------------------- v.0.0.1 Jan Miksa-")
putStrLn("| LMBD - Lambda expression evaluator/reducer. |")
putStrLn("| Usage: |")
putStrLn("| - lmbd \"test.lmbd\" - run file |")
putStrLn("| - [-i] - REPL |")
putStrLn("| - [-g] - greedy evaluation mode |")
putStrLn("| - [-s] - perform only one beta reduction step |")
putStrLn("| - ! - quit interactive mode |")
putStrLn("-----------------------------------------------------")
-- | Filter filename from arguments
filterFileName :: [String] -> [String]
filterFileName s = filter (\x -> not (elem x ["-i", "-s", "-g"])) s
-- | REPL prompt
prompt :: String -> IO String
prompt t = do
putStr t
hFlush stdout
getLine
-- | REPL loop mode
-- Takes in evaluator as argument
repl :: (Term -> Term) -> IO ()
repl f = do
l <- (prompt "\\>")
if l == [] then do repl f
else case (head l) of
'>' -> do repl f -- comment
'!' -> return () -- exit
otherwise -> do
case (tokenize l) of
Nothing -> do
putStrLn "Syntax error!"
repl f
(Just t) -> do
if not e
then do
putStrLn "Syntax error!"
repl f
else do
print (f x)
repl f
where (x, e) = parse t
-- | File input mode
fileE :: (Term -> Term) -> String -> IO ()
fileE f s = do
c <- readFile s
let l = lines c
let lc = filter (\(x:xs) -> (x /= '>')) l -- Filters out comment lines
case (tokenize (concat lc)) of
Nothing -> do
putStrLn "Syntax error!"
(Just t) -> do
if not e
then do
putStrLn "Syntax error!"
else do
print (f x)
where (x, e) = parse t