Code cleanup

Added type signatures to all top-level bindings and made minor
modifications to remove ghc warnings generated by -Wall, excluding the
unused-do-bind warning.

Signed-off-by: Collin J. Doering <collin.doering@rekahsoft.ca>
This commit is contained in:
Collin J. Doering 2015-06-15 22:37:33 -04:00
parent 2ea33e3749
commit 28b19bdcc2
1 changed files with 24 additions and 18 deletions

View File

@ -26,7 +26,7 @@ import Text.Parsec.Char (endOfLine)
import System.IO import System.IO
import System.FilePath (dropExtension) import System.FilePath (dropExtension)
import System.Console.GetOpt import System.Console.GetOpt
import System.Environment (getArgs, getProgName) import System.Environment (getArgs)
import Control.Monad (liftM) import Control.Monad (liftM)
import Numeric (showIntAtBase) import Numeric (showIntAtBase)
@ -35,10 +35,10 @@ import qualified Data.Map as Map
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
type SymbolTable = Map.Map String Int
type Label = String type Label = String
type SymbolTable = Map.Map Label Int
data Instruction = AInstr Int data Instruction = AInstr Int
| CInstr String String String | CInstr String String String
deriving (Show, Read, Eq) deriving (Show, Read, Eq)
@ -64,7 +64,7 @@ aInstr = do
char '@' char '@'
aInstrAddr <|> aInstrSym aInstrAddr <|> aInstrSym
cInstrDest :: GenParser Char st (String, String) cInstrDest :: GenParser Char st (Label, String)
cInstrDest = choice [ try (string "AMD") >> return ("AMD", "111") cInstrDest = choice [ try (string "AMD") >> return ("AMD", "111")
, try (string "AD") >> return ("AD", "110") , try (string "AD") >> return ("AD", "110")
, try (string "AM") >> return ("AM", "101") , try (string "AM") >> return ("AM", "101")
@ -73,7 +73,7 @@ cInstrDest = choice [ try (string "AMD") >> return ("AMD", "111")
, char 'D' >> return ("D", "010") , char 'D' >> return ("D", "010")
, char 'M' >> return ("M", "001") ] , char 'M' >> return ("M", "001") ]
cInstrJump :: GenParser Char st (String, String) cInstrJump :: GenParser Char st (Label, String)
cInstrJump = char 'J' >> cInstrJump = char 'J' >>
choice [ string "MP" >> return ("JMP", "111") choice [ string "MP" >> return ("JMP", "111")
, try (string "LE") >> return ("JLE", "110") , try (string "LE") >> return ("JLE", "110")
@ -83,7 +83,7 @@ cInstrJump = char 'J' >>
, try (string "GE") >> return ("JGE", "011") , try (string "GE") >> return ("JGE", "011")
, string "GT" >> return ("JGT", "001") ] , string "GT" >> return ("JGT", "001") ]
cInstrAluOps :: GenParser Char st (String, String) cInstrAluOps :: GenParser Char st (Label, String)
cInstrAluOps = choice [ char '0' >> return ("0", "0101010") cInstrAluOps = choice [ char '0' >> return ("0", "0101010")
, char '1' >> return ("1", "0111111") , char '1' >> return ("1", "0111111")
@ -158,7 +158,7 @@ comment = do
string "//" >> manyTill anyChar (lookAhead endOfLine) string "//" >> manyTill anyChar (lookAhead endOfLine)
return "" return ""
labelLine :: GenParser Char (a, Map.Map String a) String labelLine :: GenParser Char (a, Map.Map Label a) String
labelLine= do labelLine= do
lbl <- between (char '(') (char ')') symbol lbl <- between (char '(') (char ')') symbol
optional lineSpaces optional lineSpaces
@ -249,10 +249,6 @@ genHackML xs = unlines $ map instrToML xs
instrToML (CInstr op dest jump) = "111" ++ op ++ dest ++ jump instrToML (CInstr op dest jump) = "111" ++ op ++ dest ++ jump
leftPad n a ys = replicate (n - (length ys)) a ++ ys leftPad n a ys = replicate (n - (length ys)) a ++ ys
parseHackAsmFile :: FilePath -> IO String
parseHackAsmFile f = withFile f ReadMode $ \h -> do
hGetContents h >>= parseHackAsm
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
data Flag = Verbose data Flag = Verbose
@ -268,16 +264,26 @@ options = [ Option ['v'] ["verbose"] (NoArg Verbose) "chatty output on stderr"
, Option ['o'] ["output"] (OptArg Output "FILE") "output file or '-' for stdout" ] , Option ['o'] ["output"] (OptArg Output "FILE") "output file or '-' for stdout" ]
progVersion :: String progVersion :: String
progVersion = "1.0" progVersion = unlines [ "Assmblr 1.0"
, "Copyright (C) 2014 RekahSoft, Ltd."
, "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>."
, "This is free software: you are free to change and redistribute it."
, "There is NO WARRANTY, to the extent permitted by law."
, ""
, "Written by Collin J. Doering." ]
-- TODO:
-- * handle if input file dne or is unreadable
-- * handle if output file is unwritable
-- * display parser errors on stdout and do not generate output file
main :: IO () main :: IO ()
main = do main = do
argv <- getArgs argv <- getArgs
progName <- getProgName
case getOpt Permute options argv of case getOpt Permute options argv of
(o, n, []) | any isVersion o -> putStrLn progVersion (o, _, []) | any isVersion o -> putStr progVersion
(o, n, []) | any isHelp o -> putStrLn $ usageInfo (header progName) options (o, _, []) | any isHelp o -> putStr $ usageInfo header options
(o, [i], []) -> do (o, [i], []) -> do
--curDir <- getWorkingDirectory --curDir <- getWorkingDirectory
inFile <- if i == "-" inFile <- if i == "-"
@ -293,12 +299,12 @@ main = do
hGetContents inFile >>= parseHackAsm >>= hPutStr outFile hGetContents inFile >>= parseHackAsm >>= hPutStr outFile
hClose inFile hClose inFile
hClose outFile hClose outFile
(_, _, errs) -> ioError (userError (concat errs ++ usageInfo (header progName) options)) (_, _, errs) -> ioError (userError (concat errs ++ usageInfo header options))
where header pn = "Usage: " ++ pn ++ " [OPTION...] file" where header = "Usage: Asmblr [OPTION...] file"
isVersion Version = True isVersion Version = True
isVersion _ = False isVersion _ = False
isHelp Help = True isHelp Help = True
isHelp _ = False isHelp _ = False
hasOutput [] = Nothing hasOutput [] = Nothing
hasOutput ((Output (Just x)):xs) = Just x hasOutput ((Output (Just x)):_) = Just x
hasOutput (_:xs) = hasOutput xs hasOutput (_:xs) = hasOutput xs