Skip to content

Commit

Permalink
Allow executing entire files
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciofierrom committed Jul 10, 2021
1 parent 8f08905 commit fd45e30
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
51 changes: 34 additions & 17 deletions app/Main.hs
@@ -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
5 changes: 3 additions & 2 deletions brain-f.cabal
Expand Up @@ -32,7 +32,7 @@ library
-- other-modules:
other-extensions: GADTs RecordWildCards OverloadedStrings
build-depends:
base ^>=4.12.0.0,
base ^>=4.14.1.0,
containers ^>=0.6.0.1,
text ^>=1.2.3.1,
megaparsec,
Expand All @@ -47,10 +47,11 @@ executable brainfuck
-- other-modules:
other-extensions: GADTs RecordWildCards OverloadedStrings
build-depends:
base ^>=4.12.0.0,
base ^>=4.14.1.0,
containers ^>=0.6.0.1,
text ^>=1.2.3.1,
megaparsec,
optparse-generic,
brainfuck

hs-source-dirs: app
Expand Down
2 changes: 1 addition & 1 deletion src/Lib.hs
Expand Up @@ -75,7 +75,7 @@ getCurrentByte State{..} = fromMaybe (error "Out of bounds. ")
(S.lookup pointer bytes)

printByte :: State -> IO ()
printByte st = print . chr . fromIntegral $ getCurrentByte st
printByte st = putChar . chr . fromIntegral $ getCurrentByte st

getByte :: State -> IO State
getByte s@State{..} = do
Expand Down

0 comments on commit fd45e30

Please sign in to comment.