Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f08905
commit fd45e30
Showing
3 changed files
with
38 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,42 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE StandaloneDeriving #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
|
||
module Main where | ||
|
||
import qualified Data.Text as T (pack, strip, Text) | ||
import Options.Generic | ||
import System.Environment (getArgs) | ||
import Text.Megaparsec (runParser) | ||
import Text.Megaparsec.Error (showErrorComponent) | ||
|
||
import System.Environment (getArgs) | ||
import Text.Megaparsec (runParser) | ||
import Text.Megaparsec.Error (showErrorComponent) | ||
import qualified Data.Text as T (Text, pack, strip) | ||
import qualified Data.Text.IO as TIO | ||
|
||
import Lib | ||
import Parsing | ||
import Lib | ||
import Parsing | ||
|
||
data Options w = | ||
Options | ||
{ file :: w ::: Maybe FilePath <?> "A .b or .bf file to execute" | ||
, source :: w ::: Maybe String <?> "Source code to execute" | ||
} deriving (Generic) | ||
|
||
instance ParseRecord (Options Wrapped) | ||
deriving instance Show (Options Wrapped) | ||
|
||
main :: IO () | ||
main = do | ||
args <- getArgs | ||
case prepareArgs args of | ||
Just code -> case runParser program [] code of | ||
Left err -> putStrLn "Whatever error happened" | ||
Right program -> runProgram initialState program | ||
Nothing -> putStrLn "You must enter the code you want to execute" | ||
|
||
prepareArgs :: [String] -> Maybe T.Text | ||
prepareArgs (x:_) = Just (T.strip . T.pack $ x) | ||
prepareArgs [] = Nothing | ||
opts <- unwrapRecord "Execute brainfuck" | ||
case opts of | ||
(Options (Just _) (Just _)) -> putStrLn "You must provide either a program or a filepath, not both" | ||
(Options (Just f) _) -> runIt =<< TIO.readFile f | ||
(Options _ (Just s)) -> runIt (T.strip . T.pack $ s) | ||
|
||
runIt :: Text -> IO () | ||
runIt code = | ||
case runParser program [] code of | ||
Left err -> print err | ||
Right program -> runProgram initialState program |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters