summaryrefslogtreecommitdiff
path: root/main.hs
blob: e1b975a1791d6876dc9137d9a77e3c74a87f6e58 (plain)
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 Main where

import Control.Monad
import Data.Either
import System.Environment
import System.Exit

import Codegen
import Parser
import PShow


fromLeft :: Either a b -> a
fromLeft (Left a) = a
fromLeft (Right _) = error "Either is not a Left"

fromRight :: Either a b -> b
fromRight (Right b) = b
fromRight (Left _) = error "Either is not a Right"

dieShow :: (Show a) => a -> IO ()
dieShow = die . show


main :: IO ()
main = do
    args <- getArgs
    when (length args /= 1) $ die "Pass NL file name as a command-line parameter"

    let fname = args !! 0
    parseResult <- (\file -> parseProgram file fname) <$> readFile fname

    when (isLeft parseResult) $ dieShow $ fromLeft parseResult
    
    let ast = fromRight parseResult
    pprint ast

    either die print $ codegen ast "Module" fname