yabi-muno (empty) → 0.1.0.0
raw patch · 8 files changed
+222/−0 lines, 8 filesdep +basedep +bytestringdep +containerssetup-changed
Dependencies added: base, bytestring, containers, lens, mtl, parsec, yabi
Files
- LICENSE +20/−0
- Setup.hs +2/−0
- lib/Yabi.hs +9/−0
- lib/Yabi/Engine.hs +39/−0
- lib/Yabi/Parser.hs +53/−0
- lib/Yabi/Types.hs +40/−0
- src/Main.hs +19/−0
- yabi-muno.cabal +40/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2015 Tenor Biel++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ lib/Yabi.hs view
@@ -0,0 +1,9 @@+module MiniFuck+ ( module MiniFuck.Types+ , module MiniFuck.Engine+ , module MiniFuck.Parser+ ) where++import MiniFuck.Types+import MiniFuck.Engine+import MiniFuck.Parser
+ lib/Yabi/Engine.hs view
@@ -0,0 +1,39 @@+module Yabi.Engine+ ( run+ , initWorld+ ) where++import Control.Monad.Trans+import Data.ByteString+import Control.Lens+import System.IO (stdin, stdout)+import Prelude hiding (null, head)++import qualified Data.IntMap as IM++import Yabi.Types++initWorld :: World+initWorld = World IM.empty 0++run :: [Inst] -> VM ()+run [] = return ()+run (x:xs) = inst x >> run xs++inst :: Inst -> VM ()+inst Next = pos += 1+inst Prev = pos -= 1+inst Incr = use pos >>= \p -> array . at p %= Just . maybe 1 (+1)+inst Decr = use pos >>= \p -> array . at p %= Just . maybe (-1) (+ (-1))+inst GetC = do+ bs <- liftIO $ hGet stdin 1+ p <- use pos+ array . at p ?= if null bs then -1 else head bs+inst PutC = do+ p <- use pos+ c <- uses (array . at p) (maybe 0 id)+ liftIO $ hPut stdout (pack [c])+inst (Loop blk) = do+ p <- use pos+ c <- uses (array . at p) (maybe 0 id)+ if c == 0 then return () else run blk >> inst (Loop blk)
+ lib/Yabi/Parser.hs view
@@ -0,0 +1,53 @@+module Yabi.Parser+ ( programParser+ ) where++import Control.Monad+import Data.Functor+import Text.Parsec++import Yabi.Types++nothings :: Parsec String () Char+nothings = noneOf "><+-.,[]"++programParser :: Parsec String () [Inst]+programParser = do+ is <- instsParser+ eof+ return is++instsParser :: Parsec String () [Inst]+instsParser = skipMany nothings >> instParser `sepEndBy` many nothings++instParser :: Parsec String () Inst+instParser = msum+ [ nextParser+ , prevParser+ , incrParser+ , decrParser+ , putCParser+ , getCParser+ , loopParser+ ]++nextParser :: Parsec String () Inst+nextParser = char '>' >> return Next++prevParser :: Parsec String () Inst+prevParser = char '<' >> return Prev++incrParser :: Parsec String () Inst+incrParser = char '+' >> return Incr++decrParser :: Parsec String () Inst+decrParser = char '-' >> return Decr++putCParser :: Parsec String () Inst+putCParser = char '.' >> return PutC++getCParser :: Parsec String () Inst+getCParser = char ',' >> return GetC++loopParser :: Parsec String () Inst+loopParser = Loop <$> between (char '[') (char ']') instsParser
+ lib/Yabi/Types.hs view
@@ -0,0 +1,40 @@+{-# LANGUAGE TemplateHaskell, GeneralizedNewtypeDeriving #-}++module Yabi.Types+ ( VM(..)+ , Inst(..)+ , World(..)+ , array+ , pos+ ) where++import Control.Monad.State+import Control.Applicative+import Control.Lens+import Data.Word++import qualified Data.IntMap as IM++data Inst+ = Next+ | Prev+ | Incr+ | Decr+ | PutC+ | GetC+ | Loop [Inst]++newtype VM a = VM { runVM :: StateT World IO a }+ deriving ( Functor+ , Applicative+ , Monad+ , MonadState World+ , MonadIO+ )++data World = World+ { _array :: IM.IntMap Word8+ , _pos :: Int+ }++makeLenses ''World
+ src/Main.hs view
@@ -0,0 +1,19 @@+module Main+ ( main+ ) where++import Control.Monad.State+import System.Environment+import Text.Parsec++import MiniFuck++runProgram :: String -> IO ()+runProgram s = case parse programParser "" s of+ Left e -> fail (show e)+ Right is -> runStateT (runVM $ run is) initWorld >> return ()++main :: IO ()+main = getArgs >>= go where+ go [] = getContents >>= runProgram+ go (x:_) = readFile x >>= runProgram
+ yabi-muno.cabal view
@@ -0,0 +1,40 @@+name: yabi-muno+version: 0.1.0.0+synopsis: Yet Another Brainfuck Interpreter+description: Yet Another Brainfuck Interpreter for muno+license: MIT+license-file: LICENSE+author: Tenor Biel+maintainer: tenorbiel@gmail.com+category: Language+build-type: Simple+cabal-version: >=1.10+source-repository head+ type: git+ location: https://github.com/L8D/yabi.git++library+ ghc-options: -Wall+ exposed-modules: Yabi.Types,+ Yabi.Engine,+ Yabi.Parser,+ Yabi+ build-depends: base >=4.7 && <4.8,+ mtl >=2.2 && <2.3,+ lens >=4.7 && <4.8,+ containers >=0.5 && <0.6,+ parsec >=3.1 && <3.2,+ bytestring >=0.10.4 && <0.10.5+ hs-source-dirs: lib+ default-language: Haskell2010++executable yabi+ ghc-options: -Wall+ main-is: Main.hs+ build-depends: base,+ mtl,+ containers,+ parsec,+ yabi+ hs-source-dirs: src+ default-language: Haskell2010