feat: tests for the rendering/parsing

This commit is contained in:
vegowotenks 2025-06-20 09:26:11 +02:00
parent 9484d097d4
commit d012307d19
8 changed files with 104 additions and 45 deletions

View file

@ -1,7 +1,8 @@
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE DerivingStrategies #-}
module Language.Brainfuck (parse, ParseFailure) where
{-# LANGUAGE LambdaCase #-}
module Language.Brainfuck (parse, ParseFailure, render) where
import Control.Monad ((<$!>))
import Control.Monad.ST (runST, ST)
@ -14,11 +15,12 @@ import Data.Vector qualified as Vector
import Data.Vector.Mutable qualified as MutableVector
import Language.Brainfuck.Instruction ( Instruction(..) )
import qualified Data.List as List
data ParseFailure
= UnexpectedClosingBracket Int -- closing bracket position
| UnmatchedOpenBracket Int -- opening bracket position
deriving stock (Show)
deriving stock (Show, Eq)
-- | Convert a Text to a list of instructions, discard all comments.
--
@ -83,3 +85,21 @@ parse text = runST $ do
-- >>> parse $ Text.pack "[]]"
-- Left (UnexpectedClosingBracket 2)
render :: Vector Instruction -> Text
render = Text.concat . List.map renderSingle . Vector.toList
where
renderSingle = \case
Increment -> Text.singleton '+'
Decrement -> Text.singleton '-'
MoveLeft -> Text.singleton '<'
MoveRight -> Text.singleton '>'
ReadByte -> Text.singleton ','
PutByte -> Text.singleton '.'
Loop body -> Text.concat [Text.singleton '[', render body, Text.singleton ']']
-- >>> import Test.QuickCheck.Instances.Vector ()
-- >>> import Test.QuickCheck.Arbitrary (arbitrary)
-- >>> render <$> Gen.generate arbitrary
-- "<,<>,<,."