diff --git a/app/Main.hs b/app/Main.hs index c538d6d994cbe33b655372e0332414e77da718ea..3ff761e952afc79ef89145ca08ffbee7ff89877d 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,8 +1,12 @@ module Main where -import Lib +import Lib +import Options +import MainOptions +import System.Environment main :: IO () -main = do - x <- getLine - parseLine x \ No newline at end of file +main = runCommand $ \opts args -> case (input opts, output opts) of + ("" , "" ) -> startRepl + (inFile, "" ) -> runFile inFile + (inFile, outFile) -> compileFile inFile outFile diff --git a/app/MainOptions.hs b/app/MainOptions.hs new file mode 100644 index 0000000000000000000000000000000000000000..af1bfe291f20565aaf6a48257dac9467214e5b66 --- /dev/null +++ b/app/MainOptions.hs @@ -0,0 +1,33 @@ +module MainOptions where + +import Options +import Control.Applicative + + +data MainOptions = MainOptions + { + input :: String, + output :: String, + verbose :: Bool + } + + +instance Options MainOptions where + defineOptions = + MainOptions + <$> defineOption + optionType_string + (\o -> o + { optionLongFlags = ["input"] + , optionShortFlags = ['i'] + , optionDescription = "Input file with brainBreak code" + } + ) + <*> defineOption + optionType_string + (\o -> o { optionLongFlags = ["output"] + , optionShortFlags = ['o'] + , optionDescription = "Compiled file output" + } + ) + <*> simpleOption "v" False "Be more verbose" diff --git a/package.yaml b/package.yaml index 06640dd05f296d50d4b405cbe7836670285bcece..90e28e273938411bfd471c4317f71d7110fe27e3 100644 --- a/package.yaml +++ b/package.yaml @@ -16,6 +16,7 @@ dependencies: - trifecta >= 2 && < 3 - base >= 4.7 && < 5 - parsers >= 0.12 && < 1 +- options >= 1.2 && < 1.3 name: brainBreak version: 0.1.0.0 extra-source-files: diff --git a/src/CodeGenerator.hs b/src/CodeGenerator.hs index 77b33fab8281838204d47c0fa016589f00775364..8fe4b10672769b3e646dad04746a7c4b919733c7 100644 --- a/src/CodeGenerator.hs +++ b/src/CodeGenerator.hs @@ -1,6 +1,6 @@ module CodeGenerator where -import Definitions +import Definitions data State = State { parsed :: BrainBreakBlock, diff --git a/src/Definitions.hs b/src/Definitions.hs index 34c367c3c0e98dca040611f1c7086bcf187623fd..0e5bec49e16e343c9d0906cd54ac1ad4c71012f6 100644 --- a/src/Definitions.hs +++ b/src/Definitions.hs @@ -1,6 +1,6 @@ module Definitions where -data BrainBreakOperation = Increment +data BrainBreakOperation = Increment | Decrement | MoveRight | MoveLeft @@ -10,4 +10,4 @@ data BrainBreakOperation = Increment | Loop BrainBreakBlock deriving (Eq, Show) -type BrainBreakBlock = [BrainBreakOperation] \ No newline at end of file +type BrainBreakBlock = [BrainBreakOperation] diff --git a/src/LangParser.hs b/src/LangParser.hs index 16ecd248caae8e9b5529e867735da4064dcc5f82..5327a87bc564325e2e702408923c8676d4982dc3 100644 --- a/src/LangParser.hs +++ b/src/LangParser.hs @@ -1,17 +1,18 @@ module LangParser where -import Control.Applicative -import Text.Trifecta -import Data.Maybe (catMaybes) -import Definitions -import Text.Parser.Combinators -import Text.Parser.Token -import Data.Functor +import Control.Applicative +import Text.Trifecta +import Data.Maybe ( catMaybes ) +import Definitions +import Text.Parser.Combinators +import Text.Parser.Token +import Data.Functor filterComments :: BrainBreakBlock -> BrainBreakBlock -filterComments (Comment:ops) = [] ++ filterComments ops -filterComments (Loop ops:otherCode) = Loop (filterComments ops) : filterComments otherCode -filterComments (op:ops) = op : filterComments ops -filterComments [] = [] +filterComments (Comment : ops) = [] ++ filterComments ops +filterComments (Loop ops : otherCode) = + Loop (filterComments ops) : filterComments otherCode +filterComments (op : ops) = op : filterComments ops +filterComments [] = [] parseComments :: Parser BrainBreakOperation parseComments = satisfy (not . (`elem` "<+>-[],.")) $> Comment @@ -22,18 +23,20 @@ parseLoop = do pure $ Loop expr parseBrainBreak :: Parser BrainBreakBlock -parseBrainBreak = many $ parseLeft - <|> parseRight - <|> parseIncrement - <|> parseDecrement - <|> parseRead - <|> parseWrite - <|> parseLoop - <|> parseComments - where - parseRead = comma $> Read - parseWrite = dot $> Write - parseLeft = symbolic '<' $> MoveLeft - parseRight = symbolic '>' $> MoveRight - parseIncrement = symbolic '+' $> Increment - parseDecrement = symbolic '-' $> Decrement \ No newline at end of file +parseBrainBreak = + many + $ parseLeft + <|> parseRight + <|> parseIncrement + <|> parseDecrement + <|> parseRead + <|> parseWrite + <|> parseLoop + <|> parseComments + where + parseRead = comma $> Read + parseWrite = dot $> Write + parseLeft = symbolic '<' $> MoveLeft + parseRight = symbolic '>' $> MoveRight + parseIncrement = symbolic '+' $> Increment + parseDecrement = symbolic '-' $> Decrement diff --git a/src/Lib.hs b/src/Lib.hs index ae6d5d4ce501a5e91ce2da5ba42040c81fad9ceb..f78bb063cb8e145c68ec9fdd5b253ed9efb57b08 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -1,10 +1,32 @@ module Lib where -import LangParser -import Definitions -import Text.Trifecta - -parseLine :: String -> IO () -parseLine line = case parseString parseBrainBreak mempty line of - Success code -> print $ show $ filterComments code - Failure info -> print $ _errDoc info +import LangParser +import Definitions +import Text.Trifecta +import Data.List + +startRepl :: IO () +startRepl = do + input <- getLine + let parsed_string = parseString parseBrainBreak mempty input + case parsed_string of + Success code -> print (filterComments code) + Failure info -> print (_errDoc info) + startRepl + +runFile :: String -> IO () +runFile filename = do + parse_result <- parseFromFileEx parseBrainBreak filename + case parse_result of + Success code -> print (filterComments code) + Failure info -> print (_errDoc info) + + +compileFile :: String -> String -> IO () +compileFile input output = putStrLn "Compiling code in unavailable." + +run :: [String] -> IO () +run args = case args of + [] -> startRepl + [x] -> runFile x + (inFile : _ : "-o" : outFile : _) -> compileFile inFile outFile