initial; no loops

This commit is contained in:
hellerve
2015-06-23 14:42:21 +02:00
parent 81469e7210
commit 32be7ac2ff
3 changed files with 112 additions and 0 deletions

4
Setup.hs Normal file
View File

@@ -0,0 +1,4 @@
#!/usr/bin/env runhaskell
import Distribution.Simple
main = defaultMain

28
brainf*ck.cabal Normal file
View File

@@ -0,0 +1,28 @@
Name: brainfuck
Version: 0.1.0
Synopsis: Brainfuck interpreter and REPL.
Description:
brainfuck is a Haskell implementation of the programming
language Brainfuck(http://www.muppetlabs.com/~breadbox/bf/).
License: GPL
License-file: LICENSE
Author: Veit Heller <veitheller.de>
Maintainer: Veit Heller <github.com/hellerve>
Bug-Reports: http://github.com/hellerve/brainfuck/issues
Build-Type: Simple
Category: Compilers/Interpreters, Language
Tested-with: GHC == 7.8.4, GHC == 7.6.3
Cabal-Version: >= 1.2
Extra-Source-Files: README.md
LICENSE
Source-Repository head
Type: git
Location: git://github.com/hellerve/brainfuck.git
Executable brainfuck
Build-Depends: base, containers, haskeline
Extensions: ExistentialQuantification CPP
ghc-options: -Wall -Werror -O2
Main-is: brainf*ck.hs

80
brainf*ck.hs Normal file
View File

@@ -0,0 +1,80 @@
module Main where
import Data.Char
import Data.Sequence hiding (null)
import System.Console.Haskeline
import System.Environment
name :: String
name = "brainfuck"
version :: String
version = "0.1.0"
prompt :: String
prompt = name ++ "> "
printUsage :: IO ()
printUsage = do printVersion
putStrLn("\nUsage: " ++
"\n\twithout arguments - runs REPL" ++
"\n\t-h/--help - display this help message" ++
"\n\nMore information can be found on " ++
"https://github.com/hellerve/unlambda")
printVersion :: IO ()
printVersion = putStrLn (name ++ " Version " ++ version)
printCommands :: IO ()
printCommands = putStrLn "Press Ctrl-C to exit interpreter"
program :: String -> Seq Int -> Int -> IO ()
program "" _ _ = return ()
program ('>' : l) tape ptr = program l tape (ptr+1)
program ('<' : l) tape ptr = program l tape (ptr-1)
program ('+' : l) tape ptr = let newtape = update ptr ((index tape ptr) + 1) tape
in program l newtape ptr
program ('-' : l) tape ptr = let newtape = update ptr ((index tape ptr) - 1) tape
in program l newtape ptr
program ('.' : l) tape ptr = do putChar $ chr $ index tape ptr
program l tape ptr
program (',' : l) tape ptr = do x <- getChar
let newtape = update ptr (ord x) tape
program l newtape ptr
program (x : _) _ _ = putStrLn ("Unknown instruction: " ++ [x])
main :: IO ()
main = do
args <- getArgs
if null args
then do printVersion
printCommands
putStrLn ""
repl
else
if(head args == "-h") || (head args == "--help")
then printUsage
else exec args
exec :: [String] -> IO ()
exec _ = undefined
getInput :: IO String
getInput = runInputT defaultSettings repl_
where repl_ = do
x <- getInputLine "> "
case x of
Nothing -> return ""
Just i -> return i
repl :: IO ()
repl = do input <- getInput
case input of
"" -> repl
"quit" -> putStrLn "Bye"
x -> do program x (zeroes 30000 empty) 0
putStrLn ""
repl
where zeroes :: Int -> Seq Int -> Seq Int
zeroes 0 l = l
zeroes n l = zeroes (n-1) (l |> 0)