From 346cd96d672394b8fec35e687073304e81bcd0fe Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 18 Jan 2025 19:56:51 -0600 Subject: [PATCH] change to lens --- src/State.hs | 48 ++++++++++++++++++++++++++---------------------- test/Main.hs | 4 ++-- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/State.hs b/src/State.hs index 43aca0d..33fc05a 100644 --- a/src/State.hs +++ b/src/State.hs @@ -1,6 +1,8 @@ +{-# LANGUAGE TemplateHaskell #-} module State where import qualified Data.Map as Map +import Control.Lens -- The exec stack must store heterogenous types, -- and we must be able to detect that type at runtime. @@ -50,33 +52,35 @@ instance Show Gene where show (Block xs) = "Block: " <> show xs data State = State - { exec :: [Gene], - int :: [Int], - float :: [Float], - bool :: [Bool], - string :: [String], - vectorInt :: [[Int]], - vectorFloat :: [[Float]], - vectorBool :: [[Bool]], - vectorString :: [[String]], - parameter :: [Gene], - input :: Map.Map String Gene + { _exec :: [Gene], + _int :: [Int], + _float :: [Float], + _bool :: [Bool], + _string :: [String], + _intVector :: [[Int]], + _floatVector :: [[Float]], + _boolVector :: [[Bool]], + _stringVector :: [[String]], + _parameter :: [Gene], + _input :: Map.Map String Gene } deriving (Show, Eq) +$(makeLenses ''State) + emptyState :: State emptyState = State - { exec = [], - int = [], - float = [], - bool = [], - string = [], - parameter = [], - vectorInt = [], - vectorFloat = [], - vectorBool = [], - vectorString = [], - input = Map.empty + { _exec = [], + _int = [], + _float = [], + _bool = [], + _string = [], + _parameter = [], + _intVector = [], + _floatVector = [], + _boolVector = [], + _stringVector = [], + _input = Map.empty } diff --git a/test/Main.hs b/test/Main.hs index f60c02f..d1bfe71 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -10,12 +10,12 @@ import Instructions.FloatInstructions intTestFunc :: String -> [Int] -> [Gene] -> State -> IO () intTestFunc name goal genome startState = let state = loadProgram genome startState - in assert (goal == int (interpretExec state)) putStrLn (name ++ " passed test.") + in assert (goal == _int (interpretExec state)) putStrLn (name ++ " passed test.") floatTestFunc :: String -> [Float] -> [Gene] -> State -> IO () floatTestFunc name goal genome startState = let state = loadProgram genome startState - in assert (goal == float (interpretExec state)) putStrLn (name ++ " passed test.") + in assert (goal == _float (interpretExec state)) putStrLn (name ++ " passed test.") main :: IO () main = do