Skip to content

Commit

Permalink
now can have tape cli arg without the brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleberetta98 committed Apr 15, 2021
1 parent ed4e252 commit f9d48fd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
18 changes: 11 additions & 7 deletions app/Opts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import System.Console.GetOpt (ArgDescr (NoArg, ReqArg),
ArgOrder (RequireOrder), OptDescr (..),
getOpt, usageInfo)

import Data.List (foldl')
import Instruction (Instruction (TapeValue), parseInstruction)
import Data.List (foldl', isPrefixOf)
import Instruction (Instruction (TapeValue),
parseInstruction)
import System.Environment (getArgs)
import System.Exit (exitFailure, exitSuccess)
import System.IO (hPutStrLn, stderr)
Expand Down Expand Up @@ -53,7 +54,7 @@ options =
"The output file. If not specified, it will use the standard output."
, Option "t" ["tape"]
(ReqArg withTape "TAPE")
"The initial tape in the format {Symbol, Symbol, ...}.\nIt will overwrite any tape in the input file.\nIf not specified, it will be searched in the input file."
"The initial tape in the format {Symbol, Symbol, ...}, or even without the brackets.\nIt will overwrite any tape in the input file.\nIf not specified, it will be searched in the input file."
, Option "v" ["version"]
(NoArg printVersion)
"Print the program version"
Expand All @@ -73,21 +74,24 @@ withOutput arg opt = return opt{ output = writeFile arg }
-- | Sets the initial tape option
withTape :: String -> Options -> IO Options
withTape arg opt =
case parseInstruction arg of
case parseInstruction (format arg) of
Right (TapeValue t) -> return opt{ tape = Just $ fromList t }
_ -> do
hPutStrLn stderr "Invalid tape provided"
exitFailure

where
format t
| "{" `isPrefixOf` t = t
| otherwise = "{" ++ t ++ "}"

-- | Prints the version
printVersion :: a -> IO b
printVersion = const $ do
hPutStrLn stderr "1.2.3"
hPutStrLn stderr "1.3.0"
exitSuccess

-- | Prints the usage
help :: a -> IO b
help = const $ do
hPutStrLn stderr $ usageInfo "Hasking - A Turing Machine Interpreter written in Haskell" options
hPutStrLn stderr $ usageInfo "🖥 Hasking - A Turing Machine Interpreter written in Haskell" options
exitSuccess
2 changes: 1 addition & 1 deletion example.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; A turing machine that transforms a tape of 0 into a tape of 1

{0 0 0 0} ; The input tape
; {0 0 0 0} ; The input tape

[BEGIN s] ; The initial state
[FINAL q] ; The final state (here just 1, could be more)
Expand Down
4 changes: 2 additions & 2 deletions hasking.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: e08f0da40f7131d192786c8821e1aa3975bda907732ef945101e8d7ac21fa581
-- hash: 73657d9a0931135dd32d5261180c296a30df7716a28092363eecb3be8e00dfb5

name: hasking
version: 1.2.3
version: 1.3.0
description: Please see the README on GitHub at <https://github.com/micheleberetta98/hasking#readme>
homepage: https://github.com/micheleberetta98/hasking#readme
bug-reports: https://github.com/micheleberetta98/hasking/issues
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: hasking
version: 1.2.3
version: 1.3.0
github: "micheleberetta98/hasking"
license: MIT
author: "Michele Beretta"
Expand Down

0 comments on commit f9d48fd

Please sign in to comment.