-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
58 lines (53 loc) · 2.01 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
53
54
55
56
57
58
module Main where
import GHC.IO.Encoding
import qualified Data.ByteString as BS
import ClassFile.Parser
import VirtualMachine.Environment
import Control.Monad
import Data.IORef
import System.IO
-- Status flags for input
dummyFlag = "DUMMY"
selectedJava = "Examples/Java/"
selectedScala = "Examples/Scala/"
selectedCustom = "CUSTOM"
processInput :: Int -> IO String
processInput n = case n of
-- Toggle Debug
0 -> return dummyFlag
-- Java
1 -> return selectedJava
-- Scala
2 -> return selectedScala
-- Custom Input
3 -> return selectedCustom
-- Bad Input
_ -> error $ "Bad Input: " ++ show n
processFileSelection :: String -> Int -> IO String
processFileSelection prefix n = case n of
-- Arithmetic Test
1 -> return $ prefix ++ "ArithmeticTest.class"
-- Conditional Test
2 -> return $ prefix ++ "ConditionalTest.class"
-- Bad Input
_ -> error $ "Bad Input: " ++ show n
askInput :: IORef Bool -> IO String
askInput debug = read <$> (putStrLn "0) Toggle Debug. 1) Java. 2) Scala. 3) Custom" >> getLine)
>>= processInput >>= \opt -> if opt == dummyFlag then modifyIORef debug not
>> askInput debug else if opt == selectedCustom then getLine
else read <$> (putStrLn "1) Arithmetic Test. 2) Conditional Test." >> getLine)
>>= processFileSelection opt
main :: IO ()
main = do
debug <- newIORef False
setLocaleEncoding utf8
hSetBuffering stdout LineBuffering
file <- askInput debug >>= BS.readFile
let classFile = parseClassFile file
flip when (print classFile) <$> readIORef debug
flip when (putStrLn "Initializing Runtime Environment...") <$> readIORef debug
env <- readIORef debug >>= VirtualMachine.Environment.init
flip when (putStrLn "Loading bootstrap class...") <$> readIORef debug
loadClass env classFile
flip when (putStrLn "Starting Virtual Machine...") <$> readIORef debug
VirtualMachine.Environment.start env