-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mppc.hs
50 lines (43 loc) · 1.82 KB
/
Mppc.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
-- Daniel Dastoor
module Main where
import System.Environment
import System.IO
import Lexer
import Parser
import SymbolTable
import IRGenerator
import CodeGenerator
main :: IO ()
main = do
args <- getArgs
-- Check if proper number of args were supplied
case (length args) of
3 -> do
-- Check if second arg is properly formatted
case (args !! 1) of
"-o" -> do
-- Perform lexing and get tokens
ts <- lexer (head args)
-- | Check if there are any errors in the list of tokens
case (length (filter isError ts)) of
0 -> do
-- Recognize grammar, and build abstract syntax tree
let ast = parse ts
-- Print syntax tree (converted back to readable code)
putStr "\nParsed Syntax Tree:\n\n"
putStrLn $ printAST ast
putStrLn ""
let c = newCounter
let ir = generateIR ast c
putStr "\nIntermediate Representation:\n\n"
putStrLn $ printIR ir
-- Write generated stack machine code to output file
outFile <- openFile (args !! 2) WriteMode
hSetNewlineMode outFile noNewlineTranslation
let c2 = newCounter
hPutStr outFile $ generateCode ir c2
hClose outFile
_ -> printErrors (filter isError ts)
_ -> putStrLn "Usage: inputFilename -o outputFilename"
_ -> do
putStrLn "Usage: inputFilename -o outputFilename"