From 9e40df00123ceb1ece4c581cdf3db451a9910a88 Mon Sep 17 00:00:00 2001 From: Taylor Date: Wed, 15 Jan 2025 14:09:31 -0600 Subject: [PATCH] time for unit tests, instructionExecIf --- src/Push.hs | 34 +++++++++++++++++++++------------- src/Tests.hs | 21 ++++++++++++--------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/Push.hs b/src/Push.hs index 78f834a..d5554bb 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -13,11 +13,7 @@ data Gene | StringGene String | StateFunc (State -> State) | Close - --- | Block [Gene] --- If we do plushy (as opposed to just detecting the Close itself, --- then we may need to make a structually recursive data structure for the "program" data structure --- exampleGenome = [Program] rather than [Gene], or just include the Block above? + | Block [Gene] data State = State { exec :: [Gene], @@ -46,7 +42,19 @@ emptyState = -- which should take the number and type of parameter they have. instructionIntAdd :: State -> State instructionIntAdd (State es [] fs bs ss ps) = State es [] fs bs ss ps -instructionIntAdd (State es (i : is) fs bs ss ps) = State es ((i + head is) : drop 1 is) fs bs ss ps +instructionIntAdd (State es (i : is) fs bs ss ps) = State es ((head is + i) : drop 1 is) fs bs ss ps + +instructionIntSubtract :: State -> State +instructionIntSubtract (State es [] fs bs ss ps) = State es [] fs bs ss ps +instructionIntSubtract (State es (i : is) fs bs ss ps) = State es ((head is - i) : drop 1 is) fs bs ss ps + +instructionIntMultiply :: State -> State +instructionIntMultiply (State es [] fs bs ss ps) = State es [] fs bs ss ps +instructionIntMultiply (State es (i : is) fs bs ss ps) = State es ((head is * i) : drop 1 is) fs bs ss ps + +instructionIntDivide :: State -> State +instructionIntDivide (State es [] fs bs ss ps) = State es [] fs bs ss ps +instructionIntDivide (State es (i : is) fs bs ss ps) = State es (div (head is) i : drop 1 is) fs bs ss ps -- This is one of the push genome functions itself, not infrastructure. -- Optionally, split this off into independent functions @@ -84,13 +92,13 @@ loadProgarm newstack (State _ i f b s p) = State newstack i f b s p interpretExec :: State -> State interpretExec (State [] is fs bs ss ps) = State [] is fs bs ss ps interpretExec (State (e : es) is fs bs ss ps) = - case e of - (IntGene val) -> interpretExec (State es (val : is) fs bs ss ps) - (FloatGene val) -> interpretExec (State es is (val : fs) bs ss ps) - (BoolGene val) -> interpretExec (State es is fs (val : bs) ss ps) - (StringGene val) -> interpretExec (State es is fs bs (val : ss) ps) - (StateFunc func) -> interpretExec (func (State es is fs bs ss ps)) - -- (Block block) -> interpretExec (State ((reverse block) ++ es) is fs bs ss ps) + case e of + (IntGene val) -> interpretExec (State es (val : is) fs bs ss ps) + (FloatGene val) -> interpretExec (State es is (val : fs) bs ss ps) + (BoolGene val) -> interpretExec (State es is fs (val : bs) ss ps) + (StringGene val) -> interpretExec (State es is fs bs (val : ss) ps) + (StateFunc func) -> interpretExec (func (State es is fs bs ss ps)) + (Block block) -> interpretExec (State (reverse block ++ es) is fs bs ss ps) -- The safety of interpretExec on empty stacks depends on the functions it calls. -- Need to make interpretExec strict, right? diff --git a/src/Tests.hs b/src/Tests.hs index a9b07b8..05fc6a3 100644 --- a/src/Tests.hs +++ b/src/Tests.hs @@ -5,22 +5,25 @@ import Push exampleState = State { exec = [IntGene 5, StateFunc instructionParameterLoad, StateFunc instructionIntAdd], - int = [1, 2, 3], + int = [2, 6, 3], float = [1.2, 1.7], bool = [True, False], string = ["Hello", "Push"], parameter = [IntGene 1, StringGene "Hi", BoolGene True, FloatGene 1.3] } --- intAdd -testResult1 = [3, 3] == int (instructionIntAdd exampleState) +testResult1 = [8, 3] == int (instructionIntAdd exampleState) --- interpretExec -testResult2 = [6,1,2,3] == int (interpretExec exampleState) +testResult2 = [4, 3] == int (instructionIntSubtract exampleState) --- This nukes the exec stack, just as an example of how to load at the start. -loadedState = loadProgarm [IntGene 6, IntGene 6, StateFunc instructionParameterLoad, StateFunc instructionIntAdd] emptyState +testResult3 = [12, 3] == int (instructionIntMultiply exampleState) --- interpretExec -testResult3 = [12] == int (interpretExec loadedState) +testResult4 = [3, 3] == int (instructionIntDivide exampleState) +testResult5 = [6, 2, 6, 3] == int (interpretExec exampleState) + +loadedState = loadProgarm [IntGene 6, IntGene 6, StateFunc instructionIntAdd] emptyState + +testResult6 = [12] == int (interpretExec loadedState) + +allTests = and [testResult1, testResult2, testResult3, testResult4, testResult5, testResult6]