-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpreter.hs
38 lines (31 loc) · 1008 Bytes
/
Interpreter.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
module Interpreter where
import Evaluator.Evaluator (evalProgram)
import Lagger.Abs
import Lagger.Par
import System.Exit
import System.IO
import Typechecker.Typechecker (checkTypeProgram)
interpretFile :: FilePath -> IO ()
interpretFile file = readFile file >>= interpret
interpret :: String -> IO ()
interpret input =
case parseProgram input of
Left err -> printErrorAndExit err
Right program -> do
typecheckProgram program
runProgram program
parseProgram :: String -> Either String Program
parseProgram input = pProgram $ myLexer input
typecheckProgram :: Program -> IO ()
typecheckProgram tree =
case checkTypeProgram tree of
Left err -> printErrorAndExit err
Right _ -> pure ()
runProgram :: Program -> IO ()
runProgram program = do
executionResult <- evalProgram program
case executionResult of
Left err -> printErrorAndExit err
Right _ -> exitSuccess
printErrorAndExit :: (Show a) => a -> IO b
printErrorAndExit err = hPrint stderr err >> exitFailure