{-# LANGUAGE OverloadedStrings, NamedFieldPuns #-} module Evaluate where import Base (State(..), BFAction(..)) import Memory import Formatter import qualified Data.HashMap as HM import qualified Data.Text as T import qualified Data.Text.IO as T type ActionResult = (State, Maybe (IO ())) processAction :: State -> BFAction -> ActionResult processAction s@State {pointer, memory, codePos, jumpTable} instruction = let noIO :: State -> ActionResult noIO s = (s, Nothing) jump :: ActionResult jump = case HM.lookup codePos jumpTable of Just jumpTo -> noIO $ s { pointer = jumpTo } Nothing -> (s, Just $ T.putStrLn "ERROR: no matching jump point") in case instruction of MoveRight -> noIO $ s { pointer = pointer + 1 } MoveLeft -> noIO $ s { pointer = pointer - 1 } Increment -> noIO $ s { memory = increment memory pointer } Decrement -> noIO $ s { memory = decrement memory pointer } Replace -> noIO $ s { memory = setMemAdr memory pointer $ getMemAdr memory $ getMemAdr memory pointer } Print -> (s, Just $ prettyPrintState' s) JumpRight -> jump JumpLeft -> jump