From 66bb2921e81369df68663a4617aa8de18b42e5b1 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 23 Jan 2025 17:10:54 -0600 Subject: [PATCH] insert function, needs testing --- src/Instructions/CodeInstructions.hs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs index 39395dc..e75f25a 100644 --- a/src/Instructions/CodeInstructions.hs +++ b/src/Instructions/CodeInstructions.hs @@ -47,8 +47,14 @@ codeAtPoint [] _ = Block [] -- Should only happen if an empty block is last Gene codeAtPoint ((Block nestedGenes) : genes) index = codeAtPoint (nestedGenes <> genes) (index - 1) codeAtPoint (_ : genes) index = codeAtPoint genes (index - 1) +codeInsertAtPoint :: [Gene] -> Gene -> Int -> [Gene] +codeInsertAtPoint oldGenes gene 0 = gene : oldGenes +codeInsertAtPoint [] gene _ = [gene] -- This shouldn't happen (lol) +codeInsertAtPoint ((Block genes) : oldGenes) gene index = Block (codeInsertAtPoint genes gene (index - 1)) : oldGenes +codeInsertAtPoint (oldGene : oldGenes) gene index = oldGene : codeInsertAtPoint oldGenes gene (index - 1) + -- This one functions differently than pysh. --- I like this one because it preserves ordering +-- I like this one because it preserves ordering in the second case codeCombine :: Gene -> Gene -> Gene codeCombine (Block xs) (Block ys) = Block (xs <> ys) codeCombine (Block xs) ygene = Block (xs <> [ygene]) @@ -200,6 +206,18 @@ instructionCodeExtract state@(State {_code = (block@(Block c1) : cs), _int = i1 index = abs i1 `mod` codeRecursiveSize block in state{_code = codeAtPoint c1 index : cs, _int = is} - instructionCodeExtract state@(State {_code = cs, _int = _ : is}) = state{_code = cs, _int = is} instructionCodeExtract state = state + +instructionCodeInsert :: State -> State +instructionCodeInsert state@(State {_code = (block@(Block c1) : c2 : cs), _int = i1 : is}) = + let + index = abs i1 `mod` codeRecursiveSize block + in + state{_code = Block (codeInsertAtPoint c1 c2 index) : cs, _int = is} +instructionCodeInsert state@(State {_code = c1 : c2 : cs, _int = i1 : is}) = + let + index = abs i1 `mod` codeRecursiveSize (Block [c1]) + in + state{_code = Block (codeInsertAtPoint [c1] c2 index) : cs, _int = is} +instructionCodeInsert state = state