代码:
main = do
putStrLn "4917 Microprocessor\nEnter the Machine Code to be run: "
inp <- getContents
putStrLn "The output of the Program is:"
fState <- ((runStateT _4917) . construct . parse) inp
args <- getArgs
if elem "-v" args then putStrLn ("\nFinal state was: " ++ (show . snd) fState) else return ()
putStrLn "\n================================ RESTART ================================"
main
where parse xs = array (0,15) $
zip [0..15] $
take 16 $
map ((makeArbInt 4) . read) (words (filter ((/=)'.') xs)) ++ repeat (makeArbInt 4 0)
construct xs = Program xs z z z z 0 False
z = (makeArbInt 4 0)
还有更多,但这是相关部分。基本上第 3 行需要多次评估,但 getContents 正在关闭标准输入句柄:
4917: <stdin>: hGetContents: illegal operation (handle is closed)
有没有办法重新打开手柄?还是某种阻止 getContents 这样做的方法?(也许我发送了错误的信号。我在 Linux 上通过 Ctrl-D EOF 发送。也许我应该使用 EOT 或其他东西?)
编辑:我已经设法获得所需的行为,但它不会移植到 Windows。
mystdin <- openFile "/dev/tty" ReadMode
inp <- hGetContents mystdin
新问题:是否有一种通用的方法可以可移植地打开标准输入的句柄?