main :: IO ()
main = do
    hSetBuffering stdout NoBuffering
    args <- getArgs
    if (length args > 1) then do
        let opt = parseOpt $ drop 2 args
        let includeDir = _includeOpt opt
        txt <- readFile (includeDir </> head args)
        let target = includeDir </> (args !! 1)
        case parseProgram txt of
            Left err -> exitError $ "Error in parsing: " ++ show err
            Right program -> do
                program' <- resolveDeps includeDir program
                when (_debugOpt opt) $ print program'
                case checkProgram program' of
                    Left err -> exitError $ "Error in checking: " ++ err
                    Right _  -> do
                        let bytecode = if (_optimizeOpt opt)
                                        then optimize $ runCodegen program'
                                        else runCodegen program'
                        when (_debugOpt opt) $ printCode bytecode
                        writeFile target (concat $ map (encode . serialize) bytecode)
        else putStrLn "usage: rusc <source> <target>"

    where
        exitError s = hPutStrLn stderr s >> exitFailure
main :: IO ()
main = do
    hSetBuffering stdout NoBuffering
    args <- getArgs
    if (length args > 1) then do
        let opt = parseOpt $ drop 2 args
        let includeDir = _includeOpt opt
        txt <- readFile (includeDir </> head args)
        let target = includeDir </> (args !! 1)
        case parseProgram txt of
            Left err -> exitError $ "Error in parsing: " ++ show err
            Right program -> do
                program' <- resolveDeps includeDir program
                when (_debugOpt opt) $ print program'
                case checkProgram program' of
                    Left err -> exitError $ "Error in checking: " ++ err
                    Right _  -> do
                        let bytecode = if (_optimizeOpt opt)
                                        then optimize $ runCodegen program'
                                        else runCodegen program'
                        when (_debugOpt opt) $ printCode bytecode
                        writeFile target (concat $ map (encode . serialize) bytecode)
        else putStrLn "usage: rusc <source> <target>"

    where
        exitError s = hPutStrLn stderr s >> exitFailure
main = do
    hSetBuffering stdout NoBuffering
    args <- getArgs
    if (length args > 1) then do
        let opt = parseOpt $ drop 2 args
        let includeDir = _includeOpt opt
        txt <- readFile (includeDir </> head args)
        let target = includeDir </> (args !! 1)
        case parseProgram txt of
            Left err -> exitError $ "Error in parsing: " ++ show err
            Right program -> do
                program' <- resolveDeps includeDir program
                when (_debugOpt opt) $ print program'
                case checkProgram program' of
                    Left err -> exitError $ "Error in checking: " ++ err
                    Right _  -> do
                        let bytecode = if (_optimizeOpt opt)
                                        then optimize $ runCodegen program'
                                        else runCodegen program'
                        when (_debugOpt opt) $ printCode bytecode
                        writeFile target (concat $ map (encode . serialize) bytecode)
        else putStrLn "usage: rusc <source> <target>"

    where
        exitError s = hPutStrLn stderr s >> exitFailure