-- SPDX-FileCopyrightText: Chris Pressey, the creator of this work, has dedicated it to the public domain.
-- For more information, please refer to <https://unlicense.org/>
-- SPDX-License-Identifier: Unlicense
module Language.Turmac.Renderer where
import Language.Turmac.Model
import Language.Turmac.Utils (intercalate)
headers = "at step,in state,at position,the cell contains,with heads present\n"
renderConfig stepNo cfg =
headers ++ renderConfigN stepNo cfg
renderConfigN stepNo cfg@(Configuration tape pos state) =
let
renderTapeCellRow (cellPos, cell) =
let
headsPresent = if cellPos == pos then "*" else ""
in
(show stepNo) ++ "," ++ state ++ "," ++ (show cellPos) ++ "," ++ cell ++ "," ++ headsPresent
numberCells pos delta cells = zip (map (\i -> pos + i * delta) [0..]) cells
getDecoratedTapeContents :: Tape -> [(Integer,Symbol)]
getDecoratedTapeContents Tape{ leftCells=lc, currentCell=cc, rightCells=rc } =
let
lc' = reverse $ numberCells (pos-1) (-1) $ stripRightBlanks lc
rc' = numberCells (pos+1) 1 $ stripRightBlanks rc
in
lc' ++ [(pos, cc)] ++ rc'
in
intercalate "\n" $ map renderTapeCellRow $ getDecoratedTapeContents tape
renderTrace configurations = headers ++ renderTraceN 0 configurations
renderTraceN n [] = ""
renderTraceN n (cfg:cfgs) =
(renderConfigN n cfg) ++ "\n" ++ (renderTraceN (n+1) cfgs)