From 551c77e34abc02c2f3d623ea5a2b2a65d5aed8cc Mon Sep 17 00:00:00 2001
From: Pavel Kirilin <win10@list.ru>
Date: Sun, 14 Jun 2020 13:04:05 +0400
Subject: [PATCH] Arguments and different modes. Descritpion: - Added
 arguments; - Added compilation mode. - Added file parsing mode.

Signed-off-by: Pavel Kirilin <win10@list.ru>
---
 app/Main.hs          | 12 ++++++----
 app/MainOptions.hs   | 33 ++++++++++++++++++++++++++
 package.yaml         |  1 +
 src/CodeGenerator.hs |  2 +-
 src/Definitions.hs   |  4 ++--
 src/LangParser.hs    | 55 +++++++++++++++++++++++---------------------
 src/Lib.hs           | 38 +++++++++++++++++++++++-------
 7 files changed, 104 insertions(+), 41 deletions(-)
 create mode 100644 app/MainOptions.hs

diff --git a/app/Main.hs b/app/Main.hs
index c538d6d..3ff761e 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 0000000..af1bfe2
--- /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 06640dd..90e28e2 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 77b33fa..8fe4b10 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 34c367c..0e5bec4 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 16ecd24..5327a87 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 ae6d5d4..f78bb06 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
-- 
GitLab