-- 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.IR where
import Language.Turmac.Model
data Prog = Program Prog
| Seq [Prog]
| CondState [(StateId, Prog)]
| CondSymbol [(Symbol, Prog)]
| WriteMoveGoto Symbol Int StateId
deriving (Eq, Show)
indent n = replicate (n * 2) ' '
showProg n (Program p) = "Program\n" ++ indent (n+1) ++ showProg (n+1) p
showProg n (Seq progs) = "Seq\n" ++ concatMap (\p -> indent (n+1) ++ showProg (n+1) p ++ "\n") progs
showProg n (CondState branches) = "CondState\n" ++
concatMap (\(state, p) -> indent (n+1) ++ show state ++ " ->\n" ++
indent (n+2) ++ showProg (n+2) p) branches
showProg n (CondSymbol branches) = "CondSymbol\n" ++
concatMap (\(sym, p) -> indent (n+1) ++ show sym ++ " ->\n" ++
indent (n+2) ++ showProg (n+2) p) branches
showProg n (WriteMoveGoto sym dir state) = "Write " ++ show sym ++ "\n" ++
(indent n) ++ showGoto state ++ "\n" ++
(indent n) ++ showDir dir
showDir (-1) = "Left"
showDir 1 = "Right"
showDir other = "?ERROR(" ++ (show other) ++ ")"
showGoto "H" = "Halt"
showGoto state = "Goto " ++ show state