{-# OPTIONS_GHC -XTemplateHaskell -XPatternGuards #-}
module Compile (compile) where
import Control.Monad.Trans
import Harpy
import Harpy.X86Disassembler
import System.IO
import Foreign
-- our modules
import Parser
import Asm

$(callDecl "callgen" [t|Int|])

compile :: [Exp] -> IO ()
compile i = do
  runCodeGen (compile' i) () ()
  return ()

compile' e = do
  gfunc (mapM_ generate e)
  x   <- callgen
  dis <- disassemble
  io $ putStrLn ("Disassembly:") >> mapM_ (putStrLn . showIntel) dis
  io $ putStr ("Result: ") >> print x
      where 
        io  = liftIO
        generate x | EInt y   <- x = gconst y
                   | Add      <- x = gadd
                   | Multiply <- x = gmul
                   | Divide   <- x = gdiv
                   | Minus    <- x = gsub
                   | Open     <- x = error "Parsing error, invalid (mismatched) parenthesis"
                   | Close    <- x = error "Parsing error, invalid (mismatched) parenthesis"
