-- Copyright (c) 2020-2026, Chris Pressey, Cat's Eye Technologies.
-- This file is distributed under a 2-clause BSD license, see LICENSES/ dir.
-- SPDX-License-Identifier: LicenseRef-BSD-2-Clause-X-Burro
module Language.Burro.Debugger where
import Control.Monad.Trans.State (StateT, evalStateT, get, put, modify)
import Control.Monad.IO.Class
import Data.List (intercalate, intersperse)
import Language.Burro.Definition hiding (get, exec, run, interpret)
import qualified Language.Burro.Definition as Burro
-- Debugger state containing indentation level, tape position and Burro state
data DebugState = DebugState {
indents :: [String],
tapeChunkSize :: Int,
tapeDisplayWidth :: Int,
burroState :: State
}
type DebuggerM = StateT DebugState IO
ljust width str =
case width - length str of
x | x >= 0 -> str ++ (replicate x ' ')
_ -> str
-- | Visualize the current execution state of the Burro program in a readable way.
dump :: Burro -> DebuggerM ()
dump p = do
ds <- get
liftIO $ putStrLn $ showState (burroState ds) (indents ds) (tapeChunkSize ds) (tapeDisplayWidth ds) p
showState State{ dataTape=tape, stackTape=stack, haltFlag=halt, pos=pos } indents tapeChunkSize tapeDisplayWidth p =
let
tapes = ljust tapeDisplayWidth $ showTape tape pos tapeChunkSize
stacks = ljust 20 $ "(" ++ show stack ++ ")"
halts = if halt then "[H]" else "[_]"
desc = concat (map (\i -> i ++ " ") (reverse indents)) ++ show p
in
tapes ++ stacks ++ " " ++ halts ++ " " ++ desc
showTape t@(Tape l r) pos 1 =
let
(Tape l' r') = strip t
in
case l' of
[] ->
"..." ++ " [" ++ (show 0) ++ "]" ++ (showList r') ++ " ..."
(curr:l'') ->
"..." ++ showList (reverse l'') ++ " [" ++ (show curr) ++ "]" ++ (showList r') ++ " ..."
where
showList [] = ""
showList (x:xs) = " " ++ (show x) ++ (showList xs)
showTape t@(Tape l r) posInteger chunkSize =
let
pos = (fromIntegral posInteger) :: Int
(Tape l' r') = normalizeTape t (pos + 1) -- one more so the "origin" is on the right now
normalizeTape t 0 = t
normalizeTape t pos = normalizeTape (left t) (pos - 1)
chunks :: Int -> [a] -> [[a]]
chunks n [] = []
chunks n xs = take n xs : chunks n (drop n xs)
-- r'' = [reverse l'] ++ chunks chunkSize r'
-- blissfully assume the LHS of the tape is unused.
r'' = chunks chunkSize r'
-- Calculate which chunk contains our position and where in that chunk
targetChunk = pos `div` chunkSize
posInChunk = pos `mod` chunkSize
-- Add highlighting to the appropriate chunk
highlightAtPos :: Int -> Int -> [[Integer]] -> String
highlightAtPos chunk pos chunks =
"[" ++ intercalate "," (highlightChunks chunk pos chunks) ++ "]"
-- Process all chunks, highlighting the target position in the target chunk
highlightChunks :: Int -> Int -> [[Integer]] -> [String]
highlightChunks _ _ [] = []
highlightChunks chunk pos (xs:rest)
| chunk == 0 = showHighlightedChunk pos xs : map show rest
| otherwise = show xs : highlightChunks (chunk - 1) pos rest
-- Helper to show a chunk with one highlighted position
showHighlightedChunk :: Int -> [Integer] -> String
showHighlightedChunk pos xs =
"[" ++ intercalate "," (
map show (take pos xs) ++
["<" ++ show (xs !! pos) ++ ">"] ++
map show (drop (pos + 1) xs)
) ++ "]"
in
highlightAtPos targetChunk posInChunk r''
withIndent :: String -> DebuggerM a -> DebuggerM a
withIndent x action = do
modify (\s -> s { indents = (x:indents s) })
result <- action
modify (\s -> s { indents = tail $ indents s })
return result
exec :: Burro -> DebuggerM ()
exec (Seq a b) = do
exec a
exec b
exec Null = do
ds <- get
dump Null
exec ToggleHalt = do
ds <- get
dump ToggleHalt
let st@State{ haltFlag=halt } = burroState ds
let newState = st{ haltFlag=not halt }
put ds{ burroState = newState }
exec Inc = do
ds <- get
dump Inc
let st@State{ dataTape=dat } = burroState ds
let newState = st{ dataTape=inc dat }
put ds{ burroState = newState }
exec Dec = do
ds <- get
dump Dec
let st@State{ dataTape=dat } = burroState ds
let newState = st{ dataTape=dec dat }
put ds{ burroState = newState }
exec GoLeft = do
ds <- get
dump GoLeft
let st@State{ dataTape=dat, pos=pos } = burroState ds
let newState = st{ dataTape=left dat, pos=pos - 1 }
put ds{ burroState = newState }
exec GoRight = do
ds <- get
dump GoRight
let st@State{ dataTape=dat, pos=pos } = burroState ds
let newState = st{ dataTape=right dat, pos=pos + 1 }
put ds{ burroState = newState }
exec p@(Test thn els) = do
ds <- get
dump p
let st@State{ dataTape=dat, stackTape=stack } = burroState ds
let x = Burro.get dat
let (dat', stack') = swap dat stack
let stack'' = right (Burro.set stack' (0 - (Burro.get stack')))
put ds{ burroState = st{ dataTape=dat', stackTape=stack'' } }
if x > 0 then
withIndent "[then]" $ do
dump thn
exec thn
else if x < 0 then
withIndent "[else]" $ do
dump els
exec els
else do
withIndent "[non-]" $ do
dump Null
ds' <- get
let st@State{ dataTape=dat'', stackTape=stack''' } = burroState ds'
let (dat''', stack'''') = swap dat'' (left stack''')
let finalState = st{ dataTape=dat''', stackTape=stack'''' }
put ds' { burroState = finalState }
run :: Burro -> DebuggerM ()
run program = do
ds <- get
exec program
ds' <- get
let st@State{ dataTape=dat', haltFlag=halt' } = burroState ds'
case halt' of
False -> do
liftIO $ putStrLn "::: LOOP :::"
put ds' { burroState = st{ stackTape=tape [0], haltFlag=True } }
run program
True ->
liftIO $ putStrLn "::: HALT :::"
interpret :: Burro -> Int -> Int -> IO ()
interpret program tapeChunkSize tapeDisplayWidth = do
let initialState = DebugState {
indents = [],
tapeChunkSize = tapeChunkSize,
tapeDisplayWidth = tapeDisplayWidth,
burroState = newstate
}
flip evalStateT initialState $ do
liftIO $ putStrLn $ show program
run program