diff --git a/bf-optimize.cabal b/bf-optimize.cabal index ca802e7..89d9135 100644 --- a/bf-optimize.cabal +++ b/bf-optimize.cabal @@ -29,6 +29,8 @@ library Language.Brainfuck.Instruction Language.Brainfuck.Instruction.Compressed Language.Brainfuck.Instruction.Extended + Language.Brainfuck.Instruction.Extended.Interaction + Language.Brainfuck.Instruction.Extended.Operation Language.Brainfuck.Interpreter other-modules: Paths_bf_optimize diff --git a/src/Language/Brainfuck/Instruction/Extended.hs b/src/Language/Brainfuck/Instruction/Extended.hs index 5fb434c..3b685b6 100644 --- a/src/Language/Brainfuck/Instruction/Extended.hs +++ b/src/Language/Brainfuck/Instruction/Extended.hs @@ -4,23 +4,20 @@ module Language.Brainfuck.Instruction.Extended (Operation(..), Interaction(..), ExtendedInstruction(Modify, Move, Interact, Jump), pattern IfNonZero, pattern WithOffset, mkIfNonZero, mkWithOffset, translationSize, parse) where import Data.Word (Word8) + import Data.Vector (Vector) +import qualified Data.Vector as Vector + import Numeric.Natural (Natural) import Language.Brainfuck.Instruction.Compressed (CompressedInstruction) +import Language.Brainfuck.Instruction.Extended.Operation (Operation) +import Language.Brainfuck.Instruction.Extended.Interaction (Interaction) + -import qualified Data.Vector as Vector import qualified Language.Brainfuck.Instruction.Compressed as CompressedInstruction - -data Operation - = Add - | Subtract - deriving (Show) - -data Interaction - = Read - | Write - deriving (Show) +import qualified Language.Brainfuck.Instruction.Extended.Operation as Operation +import qualified Language.Brainfuck.Instruction.Extended.Interaction as Interaction pattern WithOffset :: Integer -> ExtendedInstruction -> ExtendedInstruction pattern WithOffset offset embedded <- AtOffset offset embedded @@ -61,12 +58,12 @@ prependTranslation :: CompressedInstruction -> [ExtendedInstruction] -> [Extende prependTranslation instruction rest = let addSingle = (:rest) in case instruction of - CompressedInstruction.Add i -> addSingle $ Modify Add i - CompressedInstruction.Subtract i -> addSingle $ Modify Subtract i + CompressedInstruction.Add i -> addSingle $ Modify Operation.Add i + CompressedInstruction.Subtract i -> addSingle $ Modify Operation.Subtract i CompressedInstruction.MoveRight n -> addSingle $ Move $ toInteger n CompressedInstruction.MoveLeft n -> addSingle $ Move $ toInteger (-n) - CompressedInstruction.ReadByte -> addSingle $ Interact Read - CompressedInstruction.PutByte -> addSingle $ Interact Write + CompressedInstruction.ReadByte -> addSingle $ Interact Interaction.Read + CompressedInstruction.PutByte -> addSingle $ Interact Interaction.Write CompressedInstruction.Loop body -> let bodySize = translationSize body backJump = IfNonZero $ Jump $ -(toInteger bodySize + 1) diff --git a/src/Language/Brainfuck/Instruction/Extended/Interaction.hs b/src/Language/Brainfuck/Instruction/Extended/Interaction.hs new file mode 100644 index 0000000..ef80011 --- /dev/null +++ b/src/Language/Brainfuck/Instruction/Extended/Interaction.hs @@ -0,0 +1,13 @@ + +-- | This module holds the 'Interaction' enum, this allows for nice qualified imports. +-- +-- Also, it makes for smaller compilation units and easier changes, because I I just happen to know what's where. + +module Language.Brainfuck.Instruction.Extended.Interaction (Interaction(..)) where + +-- | All the Interactions with the outside world, I think it is safe to say that this is the final state of this module. + +data Interaction + = Read + | Write + deriving (Show) diff --git a/src/Language/Brainfuck/Instruction/Extended/Operation.hs b/src/Language/Brainfuck/Instruction/Extended/Operation.hs new file mode 100644 index 0000000..595fbb0 --- /dev/null +++ b/src/Language/Brainfuck/Instruction/Extended/Operation.hs @@ -0,0 +1,13 @@ + +-- | This module holds the 'Operation' enum, this allows for nice qualified imports. +-- +-- Also, it makes for smaller compilation units and easier changes, because I I just happen to know what's where. + +module Language.Brainfuck.Instruction.Extended.Operation (Operation(..)) where + +-- | All the Operations that are allowed on a Variable in the Extended Instruction format. + +data Operation + = Add + | Subtract + deriving (Show)