From f99179278762a9b2212aee4a4b64eb9ddc3f86e6 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 20:50:47 -0600 Subject: [PATCH] add generic(ish) int instructions --- src/Instructions/IntInstructions.hs | 39 ++++++++++++++++++++++++++--- test/Main.hs | 4 +++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/Instructions/IntInstructions.hs b/src/Instructions/IntInstructions.hs index 1227003..8564967 100644 --- a/src/Instructions/IntInstructions.hs +++ b/src/Instructions/IntInstructions.hs @@ -81,6 +81,39 @@ instructionIntStackDepth :: State -> State instructionIntStackDepth state = instructionStackDepth state int -- int specific --- instructionIntYank :: State -> State --- instructionIntYank state@(State {_int = index : i2 : is}) = undefined --- instructionIntYank state = state +instructionIntYank :: State -> State +-- instructionIntYank state = instructionYank state int +instructionIntYank state@(State {_int = rawIndex : i1 : is}) = + let + myIndex :: Int + myIndex = max 0 (min rawIndex (length is - 1)) + in + state {_int = is !! myIndex : i1 : deleteAt myIndex is} +instructionIntYank state = state + +instructionIntYankDup :: State -> State +instructionIntYankDup state@(State {_int = rawIndex : item : is}) = + let + myIndex :: Int + myIndex = max 0 (min rawIndex (length is - 1)) + in + state {_int = is !! myIndex : item : is} +instructionIntYankDup state = state + +instructionIntShove :: State -> State +instructionIntShove state@(State {_int = rawIndex : item : is}) = + let + myIndex :: Int + myIndex = max 0 (min rawIndex (length is - 1)) + in + state {_int = combineTuple item (splitAt myIndex is)} +instructionIntShove state = state + +instructionIntShoveDup :: State -> State +instructionIntShoveDup state@(State {_int = rawIndex : item : is}) = + let + myIndex :: Int + myIndex = max 0 (min rawIndex (length is - 1)) + in + state {_int = item : combineTuple item (splitAt myIndex is)} +instructionIntShoveDup state = state diff --git a/test/Main.hs b/test/Main.hs index 831118d..7941c3a 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -42,6 +42,10 @@ main = do intTestFunc "instructionIntRotFail" [7, 8] [GeneInt 8, GeneInt 7, StateFunc instructionIntRot] emptyState intTestFunc "instructionIntFlush" [] [GeneInt 9696, GeneInt 92, GeneInt 420, StateFunc instructionIntFlush] emptyState -- I think I'm funny intTestFunc "instructionIntStackDepth" [2, 51, 52] [GeneInt 52, GeneInt 51, StateFunc instructionIntStackDepth] emptyState + intTestFunc "instructionIntYank" [3,3,2,1] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYank] emptyState + intTestFunc "instructionIntYankDup" [3,3,2,1,3] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYankDup] emptyState + intTestFunc "instructionIntShove" [2,1,3,1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShove] emptyState + intTestFunc "instructionIntShoveDup" [3,2,1,3,1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShoveDup] emptyState -- Exec tests intTestFunc "instructionExecIf" [6, 5] [GeneBool True, StateFunc instructionExecIf, Block [GeneInt 5, GeneInt 6], Block [GeneInt 7, GeneInt 8]] emptyState