import System.Environment
import qualified MacroExpander
import qualified Parser
import qualified Beautifier
import qualified Generator
import qualified Serializer
import qualified Optimizer
import qualified Text.Show.Pretty as Pr

import Control.Monad.Writer

pPutStrLn :: Bool -> String -> IO ()
pPutStrLn predicate line
  | predicate == True = do putStrLn line
  | otherwise = do return ()


main :: IO ()
main = do
  program <- getContents
  args <- getArgs
  let verbose = "--verbose" `elem` args

  expandedProgram <- MacroExpander.expand program
  let syntax_tree = Parser.parse expandedProgram
      ast = Beautifier.beautify syntax_tree
      ir = Generator.generate ast
      (optimized, stats) = runWriter $ Optimizer.optimize ir
      assembly = Serializer.serialize optimized

  -- Generate verbose output
  pPutStrLn verbose $ "Expanded with macros\n" ++ expandedProgram
  pPutStrLn verbose $ "syntax tree:\n" ++ Pr.ppShow syntax_tree
  pPutStrLn verbose $ "Ast:\n" ++ Pr.ppShow ast
  pPutStrLn verbose $ "IR:\n" ++ Pr.ppShow ir
  pPutStrLn verbose $ "Optimized:\n" ++ Pr.ppShow optimized

  -- Generate actual output
  putStrLn "; Generated by HDC"
  putStrLn $ unlines $ map ("; "++) stats
  putStrLn $ unlines assembly

import System.Environment
import qualified MacroExpander
import qualified Parser
import qualified Beautifier
import qualified Generator
import qualified Serializer
import qualified Optimizer
import qualified Assembler
import qualified Text.Show.Pretty as Pr

import Control.Monad.Writer

pPutStrLn :: Bool -> String -> IO ()
pPutStrLn predicate line
  | predicate == True = do putStrLn line
  | otherwise = do return ()


main :: IO ()
main = do
  program <- getContents
  args <- getArgs
  let verbose = "--verbose" `elem` args
      printHex = "--hex" `elem` args

  expandedProgram <- MacroExpander.expand program
  let syntax_tree = Parser.parse expandedProgram
      ast = Beautifier.beautify syntax_tree
      ir = Generator.generate ast
      (optimized, stats) = runWriter $ Optimizer.optimize ir
      assembly = Serializer.serialize optimized
      hex = Assembler.assemble optimized

  -- Generate verbose output
  pPutStrLn verbose $ "Expanded with macros\n" ++ expandedProgram
  pPutStrLn verbose $ "syntax tree:\n" ++ Pr.ppShow syntax_tree
  pPutStrLn verbose $ "Ast:\n" ++ Pr.ppShow ast
  pPutStrLn verbose $ "IR:\n" ++ Pr.ppShow ir
  pPutStrLn verbose $ "Optimized:\n" ++ Pr.ppShow optimized

  -- Generate actual output
  putStrLn "; Generated by HDC"
  putStrLn $ unlines $ map ("; "++) stats
  putStrLn $ unlines $ if printHex then hex else assembly

Add --hex flag to show hex output from HDC
Add --hex flag to show hex output from HDC
