-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.hs
88 lines (72 loc) · 4.23 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
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
-- automatically generated by BNF Converter with little changes
module Main where
import System.IO ( stdin, hGetContents, hPutStrLn, stderr )
import System.Environment ( getArgs, getProgName )
import System.Exit ( exitFailure, exitSuccess )
import LexXYZgrammar
import ParXYZgrammar
import SkelXYZgrammar
import PrintXYZgrammar
import AbsXYZgrammar
import Types
import Interpreter
import StaticCheck
import StaticCheckTypes
import ErrM
type ParseFun a = [Token] -> Err a
myLLexer = myLexer
runFile :: Mode -> FilePath -> IO ()
runFile mode f = readFile f >>= run mode
run :: Mode -> String -> IO ()
run mode s = let ts = myLLexer s in case pProgram ts of
Bad s -> do
hPutStrLn stderr "\nParse failed...\n"
exitFailure
Ok (Program tree) -> do
checkResult <- runStaticCheck tree
case checkResult of
Left exception -> do
hPutStrLn stderr "There was a static check exception\n"
case exception of
WrongTypeException s -> hPutStrLn stderr $ "Wrong types - " ++ s
UndefinedException s -> hPutStrLn stderr $ "Ident '" ++ s ++ "' not defined."
FunctionHasNotValueException -> hPutStrLn stderr $ "Function hasn't got any value.\nMaybe you meant 'function_name()' ?"
GeneratorHasNotValueException -> hPutStrLn stderr $ "Generator hasn't got any value.\nYou should instantiate generator first."
GeneratorVarHasNotValueException -> hPutStrLn stderr $ "Generator object hasn't got any value.\n Maybe you meant 'generator_object_name.next()' ?"
CanNotMakeVariableApplicationException -> hPutStrLn stderr $ "Can't make variable application.\nMaybe try without '()'"
WrongArgsCountException s -> hPutStrLn stderr $ "Function '" ++ s ++ "' needs different number of args."
ReturnNotInFunctionException -> hPutStrLn stderr $ "Return can be only inside function or main program."
YieldNotInGeneratorException -> hPutStrLn stderr $ "Yield can be only inside generators."
NextNotOnGeneratorException -> hPutStrLn stderr $ "'.next() can be used only on generator objects.'"
ForGenOnlyOverGeneratorException -> hPutStrLn stderr $ "In 'for (var : gen) {...}' 'gen' should be generator object variable"
VoidVariableException -> hPutStrLn stderr $ "Variables of type void are forbidden."
exitFailure
Right _ -> do
result <- runInterpret tree mode
case result of
Left exception -> do
hPutStrLn stderr "There was a runtime exception\n"
case exception of
ZeroDivException -> hPutStrLn stderr "Dividing by 0 is forbidden."
ZeroModException -> hPutStrLn stderr "Modulo by 0 is forbidden."
NoReturnStmtException -> hPutStrLn stderr "Function should end with return statement."
NoGenResultException -> hPutStrLn stderr "Generator next() should return value. Generator ended or do not have yield statement."
WrongRefArgException -> hPutStrLn stderr "Function argument by reference shoulde be variable."
exitFailure
Right _ -> do exitSuccess
usage :: IO ()
usage = do
putStrLn $ unlines
[ "usage: Call with one of the following argument combinations:"
, " --help Display this help message."
, " (no arguments) Parse stdin."
, " (files) Parse content of files."
]
exitFailure
main :: IO ()
main = do
args <- getArgs
case args of
["--help"] -> usage
[] -> hGetContents stdin >>= run StdinMode
fs -> mapM_ (runFile FileMode) fs