diff --git a/src/CodeGenerator.hs b/src/CodeGenerator.hs index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..77b33fab8281838204d47c0fa016589f00775364 100644 --- a/src/CodeGenerator.hs +++ b/src/CodeGenerator.hs @@ -0,0 +1,10 @@ +module CodeGenerator where + +import Definitions + +data State = State { + parsed :: BrainBreakBlock, + unparsed :: BrainBreakBlock, + mainFunction :: String +} + diff --git a/src/Definitions.hs b/src/Definitions.hs index 11d5385a19404822842c6f788e9db03146345727..34c367c3c0e98dca040611f1c7086bcf187623fd 100644 --- a/src/Definitions.hs +++ b/src/Definitions.hs @@ -6,6 +6,7 @@ data BrainBreakOperation = Increment | MoveLeft | Read | Write + | Comment | Loop BrainBreakBlock deriving (Eq, Show) diff --git a/src/LangParser.hs b/src/LangParser.hs new file mode 100644 index 0000000000000000000000000000000000000000..16ecd248caae8e9b5529e867735da4064dcc5f82 --- /dev/null +++ b/src/LangParser.hs @@ -0,0 +1,39 @@ +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 + +filterComments :: BrainBreakBlock -> BrainBreakBlock +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 + +parseLoop :: Parser BrainBreakOperation +parseLoop = do + expr <- brackets parseBrainBreak + 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 diff --git a/src/Lexer.hs b/src/Lexer.hs deleted file mode 100644 index cabfd705cc1764f1f732f07dcdc6b961a320a37e..0000000000000000000000000000000000000000 --- a/src/Lexer.hs +++ /dev/null @@ -1,26 +0,0 @@ -module Lexer where -import Control.Applicative -import Text.Trifecta -import Definitions -import Text.Parser.Combinators -import Text.Parser.Token -import Data.Functor - -parseBrainBreak :: Parser BrainBreakBlock -parseBrainBreak = many $ parseLeft - <|> parseRight - <|> parseIncrement - <|> parseDecrement - <|> parseRead - <|> parseWrite - <|> parseLoop - where - parseRead = comma $> Read - parseWrite = dot $> Write - parseLeft = symbolic '<' $> MoveLeft - parseRight = symbolic '>' $> MoveRight - parseIncrement = symbolic '+' $> Increment - parseDecrement = symbolic '-' $> Decrement - parseLoop = do - expr <- brackets parseBrainBreak - pure $ Loop expr diff --git a/src/Lib.hs b/src/Lib.hs index e169f9da6ffa7c0aebfc765021e2611c3274d629..ae6d5d4ce501a5e91ce2da5ba42040c81fad9ceb 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -1,9 +1,10 @@ module Lib where -import Lexer +import LangParser +import Definitions import Text.Trifecta parseLine :: String -> IO () parseLine line = case parseString parseBrainBreak mempty line of - Success code -> print $ show code + Success code -> print $ show $ filterComments code Failure info -> print $ _errDoc info