From 32be7ac2ffa2d6c4c48d29f15d86b234a81371cf Mon Sep 17 00:00:00 2001 From: hellerve Date: Tue, 23 Jun 2015 14:42:21 +0200 Subject: [PATCH] initial; no loops --- Setup.hs | 4 +++ brainf*ck.cabal | 28 +++++++++++++++++ brainf*ck.hs | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 Setup.hs create mode 100644 brainf*ck.cabal create mode 100644 brainf*ck.hs diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 0000000..f7af188 --- /dev/null +++ b/Setup.hs @@ -0,0 +1,4 @@ +#!/usr/bin/env runhaskell + +import Distribution.Simple +main = defaultMain diff --git a/brainf*ck.cabal b/brainf*ck.cabal new file mode 100644 index 0000000..412ab8c --- /dev/null +++ b/brainf*ck.cabal @@ -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 +Maintainer: Veit Heller +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 diff --git a/brainf*ck.hs b/brainf*ck.hs new file mode 100644 index 0000000..bdb5a34 --- /dev/null +++ b/brainf*ck.hs @@ -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)