This commit is contained in:
Dario48 2026-03-02 16:00:53 +01:00
commit d362b517f3

58
src/bfhs.hs Normal file
View file

@ -0,0 +1,58 @@
module Main where
import Data.Array.Base ((!))
import qualified Data.List.NonEmpty as NonEmpty (fromList, head, (!!))
import GHC.Environment (getFullArgs)
import System.IO (hFlush, stdout)
import Text.Printf (printf)
import Backend
import Backend.Interpreter
repl :: Interpreter -> IO ()
repl interpreter = do
putStr "bfhs> "
hFlush stdout
line <- getLine
case line of
":h" -> do
putStrLn "Commands:"
putStrLn " :h = this text"
putStrLn " :q = quit"
putStrLn " :d = print the state of the array around the index"
repl interpreter
":q" -> return ()
":d" ->
let (arr, size) = (getArray interpreter, getSize interpreter - 1)
in case getIndex interpreter of
0 -> printf "[ |%u| %u %u ...\n" (arr ! 0) (arr ! 1) (arr ! 2)
1 -> printf "[ %u |%u| %u %u ...\n" (arr ! 0) (arr ! 1) (arr ! 2) (arr ! 3)
2 -> printf "[ %u %u |%u| %u %u ...\n" (arr ! 0) (arr ! 1) (arr ! 2) (arr ! 3) (arr ! 4)
other -> case () of
_
| other == size - 2 -> printf "... %u %u |%u| %u %u ]\n" (arr ! (size - 4)) (arr ! (size - 3)) (arr ! (size - 2)) (arr ! (size - 1)) (arr ! size)
| other == size - 1 -> printf "... %u %u |%u| %u ]\n" (arr ! (size - 3)) (arr ! (size - 2)) (arr ! (size - 1)) (arr ! size)
| other == size - 0 -> printf "... %u %u |%u| ]\n" (arr ! (size - 2)) (arr ! (size - 1)) (arr ! size)
| otherwise -> printf "... %u %u |%u| %u %u ...\n" (arr ! (other - 2)) (arr ! (other - 1)) (arr ! other) (arr ! (other + 1)) (arr ! (other + 2))
>> repl interpreter
brainfuck -> interpreter `execStr` brainfuck >>= repl
main :: IO ()
main = do
args <- NonEmpty.fromList <$> getFullArgs
if length args == 1
then putStrLn "repl mode initialized, this mode adds a bunch of utilities using the character :, to list them instert just :h" >> repl defaultInterpreter
else do
if "-h" `elem` args
then do
putStrLn $ "usage: " ++ (NonEmpty.head args) ++ " [OPTIONS] [file]"
putStrLn "if file is not provided the program will enter repl mode"
putStrLn "OPTIONS:"
putStrLn " -h: this text"
return ()
else do
code <- readFile $ args NonEmpty.!! 1
_ <- defaultInterpreter `execStr` code
return ()