whops
This commit is contained in:
parent
ac4f235592
commit
d362b517f3
3 changed files with 2 additions and 2 deletions
|
|
@ -32,14 +32,14 @@ library backend
|
|||
|
||||
executable bfhs
|
||||
import: warnings
|
||||
main-is: bhfs.hs
|
||||
main-is: bfhs.hs
|
||||
build-depends: base, array, brainfuckhs:backend
|
||||
hs-source-dirs: src
|
||||
default-language: Haskell2010
|
||||
|
||||
executable bfhsc
|
||||
import: warnings
|
||||
main-is: bhfsc.hs
|
||||
main-is: bfhsc.hs
|
||||
build-depends: base, array, brainfuckhs:backend
|
||||
hs-source-dirs: src
|
||||
default-language: Haskell2010
|
||||
|
|
|
|||
58
src/bfhs.hs
Normal file
58
src/bfhs.hs
Normal 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 ()
|
||||
103
src/bfhsc.hs
Normal file
103
src/bfhsc.hs
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
module Main where
|
||||
|
||||
import Backend
|
||||
import Backend.Compiler
|
||||
import Data.List (isInfixOf)
|
||||
import qualified Data.List.NonEmpty as NonEmpty
|
||||
import GHC.Environment (getFullArgs)
|
||||
import System.IO.Unsafe (unsafePerformIO)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
args <- NonEmpty.fromList <$> getFullArgs
|
||||
|
||||
if length args == 1
|
||||
then error $ "usage: " ++ NonEmpty.head args ++ " file [OPTIONS]"
|
||||
else do
|
||||
if "-h" `elem` args
|
||||
then do
|
||||
putStrLn $ "usage: " ++ (NonEmpty.head args) ++ " file [OPTIONS]"
|
||||
putStrLn "OPTIONS:"
|
||||
putStrLn " -h: this text"
|
||||
putStrLn " -On: optimization level [0, 1, 2, 3, s, z]"
|
||||
putStrLn " -o: output file"
|
||||
return ()
|
||||
else do
|
||||
let parsedArgs = foldl handle_Args (Args None_ None Nothing True) (NonEmpty.drop 1 args)
|
||||
case (getInput parsedArgs) of
|
||||
Just file ->
|
||||
readFile file
|
||||
>>= execStr
|
||||
( Compiler
|
||||
( case (getOutput parsedArgs) of
|
||||
None -> file ++ ".out"
|
||||
Next -> error "you need to pass an output file if you specify the -o flag"
|
||||
Value out -> out
|
||||
)
|
||||
(getBuild parsedArgs)
|
||||
( case (getOptLevel parsedArgs) of
|
||||
None_ -> Two
|
||||
Next_ -> error "you need to pass a level if you use the -O flag"
|
||||
Value_ lvl -> lvl
|
||||
)
|
||||
)
|
||||
>> return ()
|
||||
Nothing -> error "needed input file"
|
||||
|
||||
data Arg = None | Next | Value String deriving (Eq, Show)
|
||||
data OptLevel = None_ | Next_ | Value_ Optimization deriving (Eq, Show)
|
||||
type Output = Arg
|
||||
type Input = Maybe String
|
||||
type Build = Bool
|
||||
|
||||
data Args = Args OptLevel Output Input Build deriving (Show)
|
||||
|
||||
getOptLevel :: Args -> OptLevel
|
||||
getOptLevel (Args optLevel _ _ _) = optLevel
|
||||
|
||||
getOutput :: Args -> Output
|
||||
getOutput (Args _ file _ _) = file
|
||||
|
||||
getInput :: Args -> Input
|
||||
getInput (Args _ _ file _) = file
|
||||
|
||||
getBuild :: Args -> Bool
|
||||
getBuild (Args _ _ _ b) = b
|
||||
|
||||
handle_Args :: Args -> String -> Args
|
||||
handle_Args (Args optLevel out input build) str =
|
||||
unsafePerformIO (putStr (str ++ "|") >> print (Args optLevel out input build)) `seq`
|
||||
if (optLevel == Next_)
|
||||
then Args (Value_ $ parseOpt (str !! 0)) out input build
|
||||
else
|
||||
if (out == Next)
|
||||
then Args optLevel (Value str) input build
|
||||
else
|
||||
if ("-O" `isInfixOf` str)
|
||||
then
|
||||
Args (Value_ $ parseOpt (str !! 2)) out input build
|
||||
else
|
||||
if ("-o" `isInfixOf` str)
|
||||
then
|
||||
if ((length str) > 2)
|
||||
then Args optLevel (Value $ drop 2 str) input build
|
||||
else Args optLevel Next input build
|
||||
else
|
||||
if (length "-emit-llvm" == length str)
|
||||
then
|
||||
if (any (uncurry (/=)) (zip "-emit-llvm" str))
|
||||
then
|
||||
Args optLevel out (Just str) build
|
||||
else
|
||||
Args optLevel out input False
|
||||
else
|
||||
Args optLevel out (Just str) build
|
||||
where
|
||||
parseOpt char = case (char) of
|
||||
'0' -> Zero
|
||||
'1' -> One
|
||||
'2' -> Two
|
||||
'3' -> Three
|
||||
's' -> Size
|
||||
'z' -> SizeSize
|
||||
c -> error $ "Error: unrecognised optimization level " ++ [c]
|
||||
Loading…
Reference in a new issue