Blocks done, execIf done
This commit is contained in:
parent
9e40df0012
commit
99e0938f08
30
src/Push.hs
30
src/Push.hs
@ -41,20 +41,28 @@ emptyState =
|
|||||||
-- Everntually, this can be part of the apply func to state helpers,
|
-- Everntually, this can be part of the apply func to state helpers,
|
||||||
-- 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 (i : is) fs bs ss ps) = State es ((head is + i) : 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
|
||||||
|
instructionIntAdd state = state
|
||||||
|
|
||||||
instructionIntSubtract :: State -> State
|
instructionIntSub :: State -> State
|
||||||
instructionIntSubtract (State es [] fs bs ss ps) = State es [] fs bs ss ps
|
instructionIntSub (State es (i : is) fs bs ss ps) = State es ((head is - i) : drop 1 is) 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
|
instructionIntSub state = state
|
||||||
|
|
||||||
instructionIntMultiply :: State -> State
|
instructionIntMul :: State -> State
|
||||||
instructionIntMultiply (State es [] fs bs ss ps) = State es [] fs bs ss ps
|
instructionIntMul (State es (i : is) fs bs ss ps) = State es ((head is * i) : drop 1 is) 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
|
instructionIntMul state = state
|
||||||
|
|
||||||
instructionIntDivide :: State -> State
|
instructionIntDiv :: State -> State
|
||||||
instructionIntDivide (State es [] fs bs ss ps) = State es [] fs bs ss ps
|
instructionIntDiv (State es (i : is) fs bs ss ps) = State es (div (head is) i : drop 1 is) 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
|
instructionIntDiv state = state
|
||||||
|
|
||||||
|
instructionExecIf :: State -> State
|
||||||
|
instructionExecIf (State es is fs [] ss ps) = (State es is fs [] ss ps)
|
||||||
|
instructionExecIf (State (e : es) is fs bs ss ps) =
|
||||||
|
case head bs of
|
||||||
|
True -> State (e : drop 1 es) is fs bs ss ps
|
||||||
|
False -> State (es) is fs bs ss ps
|
||||||
|
instructionExecIf state = state
|
||||||
|
|
||||||
-- 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
|
||||||
@ -98,7 +106,7 @@ interpretExec (State (e : es) is 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 (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?
|
||||||
|
15
src/Tests.hs
15
src/Tests.hs
@ -14,16 +14,21 @@ exampleState =
|
|||||||
|
|
||||||
testResult1 = [8, 3] == int (instructionIntAdd exampleState)
|
testResult1 = [8, 3] == int (instructionIntAdd exampleState)
|
||||||
|
|
||||||
testResult2 = [4, 3] == int (instructionIntSubtract exampleState)
|
testResult2 = [4, 3] == int (instructionIntSub exampleState)
|
||||||
|
|
||||||
testResult3 = [12, 3] == int (instructionIntMultiply exampleState)
|
testResult3 = [12, 3] == int (instructionIntMul exampleState)
|
||||||
|
|
||||||
testResult4 = [3, 3] == int (instructionIntDivide exampleState)
|
testResult4 = [3, 3] == int (instructionIntDiv exampleState)
|
||||||
|
|
||||||
testResult5 = [6, 2, 6, 3] == int (interpretExec exampleState)
|
testResult5 = [6, 2, 6, 3] == int (interpretExec exampleState)
|
||||||
|
|
||||||
loadedState = loadProgarm [IntGene 6, IntGene 6, StateFunc instructionIntAdd] emptyState
|
loadedState = loadProgarm [IntGene 6, IntGene 6, StateFunc instructionIntAdd] emptyState
|
||||||
|
|
||||||
testResult6 = [12] == int (interpretExec loadedState)
|
testResult6 = [12] == int (interpretExec loadedState)
|
||||||
|
|
||||||
allTests = and [testResult1, testResult2, testResult3, testResult4, testResult5, testResult6]
|
loadedState2 = loadProgarm [BoolGene True, StateFunc instructionExecIf, Block [IntGene 5, IntGene 6], Block [IntGene 7, IntGene 8]] emptyState
|
||||||
|
testResult7 = [6, 5] == int (interpretExec loadedState2)
|
||||||
|
|
||||||
|
loadedState3 = loadProgarm [BoolGene False, StateFunc instructionExecIf, Block [IntGene 5, IntGene 6], Block [IntGene 7, IntGene 8]] emptyState
|
||||||
|
testResult8 = [8, 7] == int (interpretExec loadedState3)
|
||||||
|
|
||||||
|
allTests = and [testResult1, testResult2, testResult3, testResult4, testResult5, testResult6, testResult7, testResult8]
|
Loading…
x
Reference in New Issue
Block a user