fix: function naming
This commit is contained in:
parent
a94455f365
commit
f9fd24261a
1 changed files with 29 additions and 26 deletions
|
@ -30,7 +30,7 @@ parse :: Text -> Either ParseFailure (Vector Instruction)
|
||||||
parse text = runST $ do
|
parse text = runST $ do
|
||||||
collectorVector <- MutableVector.new (Text.length text)
|
collectorVector <- MutableVector.new (Text.length text)
|
||||||
|
|
||||||
result <- go text collectorVector 0
|
result <- parseBlock text collectorVector 0
|
||||||
|
|
||||||
pure $ case result of
|
pure $ case result of
|
||||||
|
|
||||||
|
@ -42,36 +42,39 @@ parse text = runST $ do
|
||||||
|
|
||||||
where
|
where
|
||||||
|
|
||||||
go :: Text -> MVector s Instruction -> Int -> ST s (Either ParseFailure (Vector Instruction, Text))
|
-- | Parses the supplied text until the next closing bracket., the closing bracket will be returned in the rest.
|
||||||
go Text.Empty instructions index = do
|
--
|
||||||
|
-- Assumes the vector has enough space to hold all the instructions. Assumes all elements until the Int index have already been initialized.
|
||||||
|
parseBlock :: Text -> MVector s Instruction -> Int -> ST s (Either ParseFailure (Vector Instruction, Text))
|
||||||
|
parseBlock Text.Empty instructions index = do
|
||||||
let populatedSlice = MutableVector.take index instructions
|
let populatedSlice = MutableVector.take index instructions
|
||||||
frozen <- Vector.force <$!> Vector.freeze populatedSlice
|
frozen <- Vector.force <$!> Vector.freeze populatedSlice
|
||||||
pure . Right $ (frozen, Text.empty)
|
pure . Right $ (frozen, Text.empty)
|
||||||
go t@(c Text.:< cs) instructions index = let
|
parseBlock t@(c Text.:< cs) instructions index = let
|
||||||
|
|
||||||
recognizeInstruction i cont = do
|
recognizeInstruction i cont = do
|
||||||
MutableVector.write instructions index i
|
MutableVector.write instructions index i
|
||||||
go cont instructions (succ index)
|
parseBlock cont instructions (succ index)
|
||||||
|
|
||||||
in case c of
|
in case c of
|
||||||
'+' -> recognizeInstruction Increment cs
|
'+' -> recognizeInstruction Increment cs
|
||||||
'-' -> recognizeInstruction Decrement cs
|
'-' -> recognizeInstruction Decrement cs
|
||||||
'>' -> recognizeInstruction MoveRight cs
|
'>' -> recognizeInstruction MoveRight cs
|
||||||
'<' -> recognizeInstruction MoveLeft cs
|
'<' -> recognizeInstruction MoveLeft cs
|
||||||
',' -> recognizeInstruction ReadByte cs
|
',' -> recognizeInstruction ReadByte cs
|
||||||
'.' -> recognizeInstruction PutByte cs
|
'.' -> recognizeInstruction PutByte cs
|
||||||
']' -> do
|
']' -> do
|
||||||
let populatedSlice = MutableVector.take index instructions
|
let populatedSlice = MutableVector.take index instructions
|
||||||
frozen <- Vector.force <$!> Vector.freeze populatedSlice
|
frozen <- Vector.force <$!> Vector.freeze populatedSlice
|
||||||
pure $ Right (frozen, t)
|
pure $ Right (frozen, t)
|
||||||
'[' -> do
|
'[' -> do
|
||||||
innerVector <- MutableVector.new (Text.length cs)
|
innerVector <- MutableVector.new (Text.length cs)
|
||||||
innerResult <- go cs innerVector 0
|
innerResult <- parseBlock cs innerVector 0
|
||||||
case innerResult of
|
case innerResult of
|
||||||
Right (body, ']' Text.:< rest) -> recognizeInstruction (Loop body) rest
|
Right (body, ']' Text.:< rest) -> recognizeInstruction (Loop body) rest
|
||||||
Right _ -> pure $ Left (UnmatchedOpenBracket . fromIntegral $ Text.length text - Text.length t)
|
Right _ -> pure $ Left (UnmatchedOpenBracket . fromIntegral $ Text.length text - Text.length t)
|
||||||
_ -> pure innerResult
|
_ -> pure innerResult
|
||||||
_ -> go cs instructions index
|
_ -> parseBlock cs instructions index
|
||||||
|
|
||||||
|
|
||||||
-- >>> parse $ Text.pack "<<>>,,..<,.>"
|
-- >>> parse $ Text.pack "<<>>,,..<,.>"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue