-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
52 lines (49 loc) · 1.56 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 Colourista
import Control.Monad (unless, when)
import qualified Data.Text as Text
import Module.OPG_Parser (parseCheck, parseOPGfile, unwrap)
import Module.OPG_Printer (output, readLines)
import Module.OPG_core
( Grammar,
Matrix,
Relation (..),
output_matrix,
output_t,
reify,
type_checker,
)
import System.Directory (doesFileExist)
import System.IO (hFlush, stdout)
import System.IO.Unsafe (unsafePerformIO)
import Text.Printf (printf)
compute :: String -> IO ()
compute path =
if unsafePerformIO (doesFileExist path)
then do
let grammar_raw = unsafePerformIO (readLines path)
let grammar = parseOPGfile grammar_raw
if parseCheck grammar
then do
successMessage $ Text.pack "Grammar loaded."
output (path ++ "_output.md") (unwrap grammar)
else errorMessage $ Text.pack "Grammar parse failed!"
else errorMessage $ Text.pack "File does not exist!"
main :: IO ()
main = do
printf (formatWith [yellowBg, magenta, bold] "Input the grammar file: ")
hFlush stdout
path <- getLine
when (path == "test") $ do
infoMessage $ Text.pack "Test 1: accepted grammar"
compute "./Test/in1.txt"
infoMessage $ Text.pack "Test 2: Unaccepted grammar (Error 2)"
compute "./Test/in2.txt"
infoMessage $ Text.pack "Test 3: Unaccepted grammar (Error 1)"
compute "./Test/in3.txt"
successMessage $ Text.pack "All tests passed."
main
when (path == ":q") (printf (formatWith [bold] "Bye."))
unless (path == ":q" || path == "test") $ do
compute path
main