packages feed

calc-0.1: Compile.hs

{-# OPTIONS_GHC -XTemplateHaskell -XPatternGuards #-}
module Compile (compile) where
import Control.Monad.Trans
import Harpy.X86Disassembler
import Harpy.CodeGenMonad
import Harpy.X86Assembler
import Harpy.X86CodeGen
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
        allocate = ensureBufferSize . (* 16)
        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"