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" putStrLn " :s = print a table of all symbols and their functionality" 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 ":s" -> return () >> putStrLn "|--------+-------------------------------------------------------------------------------------------------------|" >> putStrLn "| symbol | description |" >> putStrLn "|========+=======================================================================================================|" >> putStrLn "| > | Increment the data pointer by one (to point to the next cell to the right). |" >> putStrLn "|--------+-------------------------------------------------------------------------------------------------------|" >> putStrLn "| < | Decrement the data pointer by one (to point to the next cell to the left). Undefined if at 0. |" >> putStrLn "|--------+-------------------------------------------------------------------------------------------------------|" >> putStrLn "| + | Increment the byte at the data pointer by one modulo 256. |" >> putStrLn "|--------+-------------------------------------------------------------------------------------------------------|" >> putStrLn "| - | Decrement the byte at the data pointer by one modulo 256. |" >> putStrLn "|--------+-------------------------------------------------------------------------------------------------------|" >> putStrLn "| . | Output the byte at the data pointer. |" >> putStrLn "|--------+-------------------------------------------------------------------------------------------------------|" >> putStrLn "| , | Accept one byte of input, storing its value in the byte at the data pointer. |" >> putStrLn "|--------+-------------------------------------------------------------------------------------------------------|" >> putStrLn "| [ | If the byte at the data pointer is zero, jump forward to the command after the matching ] command. |" >> putStrLn "|--------+-------------------------------------------------------------------------------------------------------|" >> putStrLn "| ] | If the byte at the data pointer is nonzero, jump back to the command after the matching [ command. |" >> putStrLn "|--------+-------------------------------------------------------------------------------------------------------|" >> 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 ()