我为语句创建了简单的评估器。我想使用变压器来做到这一点 - 将 IO monad 与 State 混合。
有人可以解释怎么做吗?这是我无法处理的东西——变形金刚。
execStmt :: Stmt -> State (Map.Map String Int) ()
execStmt s = case s of
SAssigment v e -> get >>= (\s -> put (Map.insert v (fromJust (runReader (evalExpM e) s)) s))
SIf e s1 s2 -> get >>= (\s -> case (fromJust (runReader (evalExpM e) s)) of
0 -> execStmt s2
_ -> execStmt s1
)
SSkip -> return ()
SSequence s1 s2 -> get >>= (\s -> (execStmt s1) >>= (\s' -> execStmt s2))
SWhile e s1 -> get >>= (\s -> case (fromJust (runReader (evalExpM e) s)) of
0 -> return ()
_ -> (execStmt s1) >>= (\s' -> execStmt (SWhile e s1)))
execStmt' :: Stmt -> IO ()
execStmt' stmt = putStrLn $ show $ snd $ runState (execStmt stmt) Map.empty