feat[modules]: give datatypes their own file

This commit is contained in:
vegowotenks 2025-06-29 13:51:03 +02:00
parent 0411e8af19
commit 9dfbb4fb1e
4 changed files with 40 additions and 15 deletions

View file

@ -29,6 +29,8 @@ library
Language.Brainfuck.Instruction Language.Brainfuck.Instruction
Language.Brainfuck.Instruction.Compressed Language.Brainfuck.Instruction.Compressed
Language.Brainfuck.Instruction.Extended Language.Brainfuck.Instruction.Extended
Language.Brainfuck.Instruction.Extended.Interaction
Language.Brainfuck.Instruction.Extended.Operation
Language.Brainfuck.Interpreter Language.Brainfuck.Interpreter
other-modules: other-modules:
Paths_bf_optimize Paths_bf_optimize

View file

@ -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 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.Word (Word8)
import Data.Vector (Vector) import Data.Vector (Vector)
import qualified Data.Vector as Vector
import Numeric.Natural (Natural) import Numeric.Natural (Natural)
import Language.Brainfuck.Instruction.Compressed (CompressedInstruction) 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 import qualified Language.Brainfuck.Instruction.Compressed as CompressedInstruction
import qualified Language.Brainfuck.Instruction.Extended.Operation as Operation
data Operation import qualified Language.Brainfuck.Instruction.Extended.Interaction as Interaction
= Add
| Subtract
deriving (Show)
data Interaction
= Read
| Write
deriving (Show)
pattern WithOffset :: Integer -> ExtendedInstruction -> ExtendedInstruction pattern WithOffset :: Integer -> ExtendedInstruction -> ExtendedInstruction
pattern WithOffset offset embedded <- AtOffset offset embedded pattern WithOffset offset embedded <- AtOffset offset embedded
@ -61,12 +58,12 @@ prependTranslation :: CompressedInstruction -> [ExtendedInstruction] -> [Extende
prependTranslation instruction rest = let prependTranslation instruction rest = let
addSingle = (:rest) addSingle = (:rest)
in case instruction of in case instruction of
CompressedInstruction.Add i -> addSingle $ Modify Add i CompressedInstruction.Add i -> addSingle $ Modify Operation.Add i
CompressedInstruction.Subtract i -> addSingle $ Modify Subtract i CompressedInstruction.Subtract i -> addSingle $ Modify Operation.Subtract i
CompressedInstruction.MoveRight n -> addSingle $ Move $ toInteger n CompressedInstruction.MoveRight n -> addSingle $ Move $ toInteger n
CompressedInstruction.MoveLeft n -> addSingle $ Move $ toInteger (-n) CompressedInstruction.MoveLeft n -> addSingle $ Move $ toInteger (-n)
CompressedInstruction.ReadByte -> addSingle $ Interact Read CompressedInstruction.ReadByte -> addSingle $ Interact Interaction.Read
CompressedInstruction.PutByte -> addSingle $ Interact Write CompressedInstruction.PutByte -> addSingle $ Interact Interaction.Write
CompressedInstruction.Loop body -> let CompressedInstruction.Loop body -> let
bodySize = translationSize body bodySize = translationSize body
backJump = IfNonZero $ Jump $ -(toInteger bodySize + 1) backJump = IfNonZero $ Jump $ -(toInteger bodySize + 1)

View file

@ -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)

View file

@ -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)