time for unit tests, instructionExecIf
This commit is contained in:
parent
d8de5ff761
commit
9e40df0012
34
src/Push.hs
34
src/Push.hs
@ -13,11 +13,7 @@ data Gene
|
|||||||
| StringGene String
|
| StringGene String
|
||||||
| StateFunc (State -> State)
|
| StateFunc (State -> State)
|
||||||
| Close
|
| Close
|
||||||
|
| Block [Gene]
|
||||||
-- | 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?
|
|
||||||
|
|
||||||
data State = State
|
data State = State
|
||||||
{ exec :: [Gene],
|
{ exec :: [Gene],
|
||||||
@ -46,7 +42,19 @@ emptyState =
|
|||||||
-- which should take the number and type of parameter they have.
|
-- which should take the number and type of parameter they have.
|
||||||
instructionIntAdd :: State -> State
|
instructionIntAdd :: State -> State
|
||||||
instructionIntAdd (State es [] fs bs ss ps) = State es [] fs bs ss ps
|
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.
|
-- This is one of the push genome functions itself, not infrastructure.
|
||||||
-- Optionally, split this off into independent functions
|
-- 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 -> State
|
||||||
interpretExec (State [] is fs bs ss ps) = State [] is fs bs ss ps
|
interpretExec (State [] is fs bs ss ps) = State [] is fs bs ss ps
|
||||||
interpretExec (State (e : es) is fs bs ss ps) =
|
interpretExec (State (e : es) is fs bs ss ps) =
|
||||||
case e of
|
case e of
|
||||||
(IntGene val) -> interpretExec (State es (val : is) fs bs ss ps)
|
(IntGene val) -> interpretExec (State es (val : is) fs bs ss ps)
|
||||||
(FloatGene val) -> interpretExec (State es is (val : 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)
|
(BoolGene val) -> interpretExec (State es is fs (val : bs) ss ps)
|
||||||
(StringGene val) -> interpretExec (State es is fs bs (val : ss) ps)
|
(StringGene val) -> interpretExec (State es is fs bs (val : ss) ps)
|
||||||
(StateFunc func) -> interpretExec (func (State es is fs bs 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)
|
(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.
|
-- The safety of interpretExec on empty stacks depends on the functions it calls.
|
||||||
-- Need to make interpretExec strict, right?
|
-- Need to make interpretExec strict, right?
|
||||||
|
21
src/Tests.hs
21
src/Tests.hs
@ -5,22 +5,25 @@ import Push
|
|||||||
exampleState =
|
exampleState =
|
||||||
State
|
State
|
||||||
{ exec = [IntGene 5, StateFunc instructionParameterLoad, StateFunc instructionIntAdd],
|
{ exec = [IntGene 5, StateFunc instructionParameterLoad, StateFunc instructionIntAdd],
|
||||||
int = [1, 2, 3],
|
int = [2, 6, 3],
|
||||||
float = [1.2, 1.7],
|
float = [1.2, 1.7],
|
||||||
bool = [True, False],
|
bool = [True, False],
|
||||||
string = ["Hello", "Push"],
|
string = ["Hello", "Push"],
|
||||||
parameter = [IntGene 1, StringGene "Hi", BoolGene True, FloatGene 1.3]
|
parameter = [IntGene 1, StringGene "Hi", BoolGene True, FloatGene 1.3]
|
||||||
}
|
}
|
||||||
|
|
||||||
-- intAdd
|
testResult1 = [8, 3] == int (instructionIntAdd exampleState)
|
||||||
testResult1 = [3, 3] == int (instructionIntAdd exampleState)
|
|
||||||
|
|
||||||
-- interpretExec
|
testResult2 = [4, 3] == int (instructionIntSubtract exampleState)
|
||||||
testResult2 = [6,1,2,3] == int (interpretExec exampleState)
|
|
||||||
|
|
||||||
-- This nukes the exec stack, just as an example of how to load at the start.
|
testResult3 = [12, 3] == int (instructionIntMultiply exampleState)
|
||||||
loadedState = loadProgarm [IntGene 6, IntGene 6, StateFunc instructionParameterLoad, StateFunc instructionIntAdd] emptyState
|
|
||||||
|
|
||||||
-- interpretExec
|
testResult4 = [3, 3] == int (instructionIntDivide exampleState)
|
||||||
testResult3 = [12] == int (interpretExec loadedState)
|
|
||||||
|
|
||||||
|
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]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user