From 2d9cc0ddf345bd291fdfce7fc38c96fd19261d94 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin <win10@list.ru> Date: Sat, 13 Jun 2020 22:43:28 +0400 Subject: [PATCH] Fixed language parser. Signed-off-by: Pavel Kirilin <win10@list.ru> --- src/CodeGenerator.hs | 10 ++++++++++ src/Definitions.hs | 1 + src/LangParser.hs | 39 +++++++++++++++++++++++++++++++++++++++ src/Lexer.hs | 26 -------------------------- src/Lib.hs | 5 +++-- 5 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 src/LangParser.hs delete mode 100644 src/Lexer.hs diff --git a/src/CodeGenerator.hs b/src/CodeGenerator.hs index e69de29..77b33fa 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 11d5385..34c367c 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 0000000..16ecd24 --- /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 cabfd70..0000000 --- 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 e169f9d..ae6d5d4 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 -- GitLab