diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs index afe2d91..1588465 100644 --- a/src/HushGP/Instructions/CodeInstructions.hs +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -96,19 +96,11 @@ instructionCodeDoThenPop :: State -> State instructionCodeDoThenPop state@(State {_code = c1 : _, _exec = es}) = state {_exec = c1 : StateFunc (instructionCodePop, "instructionCodePop") : es} instructionCodeDoThenPop state = state --- |Utility: A shorthand for instrucitonCodeFromExec to make code instructions less bloated -codeFromExec :: Gene -codeFromExec = StateFunc (instructionCodeFromExec, "instructionCodeFromExec") - --- |Utility: A shorthand for instructionCodoDoRange to make code instructions less bloated -codeDoRange :: Gene -codeDoRange = StateFunc (instructionCodeDoRange, "instructionCodeDoRange") - -- |Evaluates the top item on the code stack for each step along the range i to j. Both i and j are taken from the int stack. instructionCodeDoRange :: State -> State instructionCodeDoRange state@(State {_code = c1 : cs, _int = i0 : i1 : is, _exec = es}) = if increment i0 i1 /= 0 - then state {_exec = c1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, codeFromExec, c1, codeDoRange] : es, _int = i1 : is, _code = cs} + then state {_exec = c1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, StateFunc (instructionCodeFromExec, "instructionCodeFromExec"), c1, StateFunc (instructionCodeDoRange, "instructionCodeDoRange")] : es, _int = i1 : is, _code = cs} else state {_exec = c1: es, _int = i1 : is, _code = cs} where increment :: Int -> Int -> Int @@ -123,7 +115,7 @@ instructionCodeDoCount :: State -> State instructionCodeDoCount state@(State {_code = c : cs, _int = i1 : is, _exec = es}) = if i1 < 1 then state - else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i1 - 1, codeFromExec, c, codeDoRange] : es} + else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i1 - 1, StateFunc (instructionCodeFromExec, "instructionCodeFromExec"), c, StateFunc (instructionCodeDoRange, "instructionCodeDoRange")] : es} instructionCodeDoCount state = state -- |Evaluates the top item on the code stack n times, where n comes from the n comes from the top of the int stack. @@ -131,7 +123,7 @@ instructionCodeDoTimes :: State -> State instructionCodeDoTimes state@(State {_code = c : cs, _int = i1 : is, _exec = es}) = if i1 < 1 then state - else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i1 - 1, codeFromExec, Block [StateFunc (instructionIntPop, "instructionIntPop"), c], codeDoRange] : es} + else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i1 - 1, StateFunc (instructionCodeFromExec, "instructionCodeFromExec"), Block [StateFunc (instructionIntPop, "instructionIntPop"), c], StateFunc (instructionCodeDoRange, "instructionCodeDoRange")] : es} instructionCodeDoTimes state = state -- |If the top boolean is true, execute the top element of the code stack and skip the second. Otherwise, skip the top element of the code stack and execute the second. diff --git a/src/HushGP/Instructions/ExecInstructions.hs b/src/HushGP/Instructions/ExecInstructions.hs index 8e5f53e..48f9005 100644 --- a/src/HushGP/Instructions/ExecInstructions.hs +++ b/src/HushGP/Instructions/ExecInstructions.hs @@ -70,16 +70,12 @@ instructionExecShoveDup = instructionShoveDup exec instructionExecIsStackEmpty :: State -> State instructionExecIsStackEmpty = instructionIsStackEmpty exec --- |Utility: Shorthand for instructionExecDoRange -execDoRange :: Gene -execDoRange = StateFunc (instructionExecDoRange, "instructionExecDoRange") - -- |Evaluates the top item on the exec stack for each step along the range i to j. Both i and j are -- taken from the int stack. Differs from code_do_range only in the source of the code and the recursive call. instructionExecDoRange :: State -> State instructionExecDoRange state@(State {_exec = e1 : es, _int = i0 : i1 : is}) = if increment i0 i1 /= 0 - then state {_exec = e1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, execDoRange, e1] : es, _int = i1 : is} + then state {_exec = e1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, StateFunc (instructionExecDoRange, "instructionExecDoRange"), e1] : es, _int = i1 : is} else state {_exec = e1 : es, _int = i1 : is} where increment :: Int -> Int -> Int @@ -95,7 +91,7 @@ instructionExecDoCount :: State -> State instructionExecDoCount state@(State {_exec = e1 : es, _int = i1 : is}) = if i1 < 1 then state - else state {_exec = Block [GeneInt 0, GeneInt $ i1 - 1, execDoRange, e1] : es, _int = is} + else state {_exec = Block [GeneInt 0, GeneInt $ i1 - 1, StateFunc (instructionExecDoRange, "instructionExecDoRange"), e1] : es, _int = is} instructionExecDoCount state = state -- |Evaluates the top item on the code stack n times, where n comes from the n comes from the top of the int stack. @@ -103,7 +99,7 @@ instructionExecDoTimes :: State -> State instructionExecDoTimes state@(State {_exec = e1 : es, _int = i1 : is}) = if i1 < 1 then state - else state {_exec = Block [GeneInt 0, GeneInt $ i1 - 1, execDoRange, Block [StateFunc (instructionIntPop, "instructionIntPop"), e1]] : es, _int = is} + else state {_exec = Block [GeneInt 0, GeneInt $ i1 - 1, StateFunc (instructionExecDoRange, "instructionExecDoRange"), Block [StateFunc (instructionIntPop, "instructionIntPop"), e1]] : es, _int = is} instructionExecDoTimes state = state -- |Utility: A shorthand for instructionExecWhile diff --git a/src/HushGP/TH.hs b/src/HushGP/TH.hs index 52b3cac..426f25a 100644 --- a/src/HushGP/TH.hs +++ b/src/HushGP/TH.hs @@ -2,11 +2,35 @@ module HushGP.TH where import System.IO +import Text.Regex.TDFA +import Data.List +import HushGP.State +import Language.Haskell.TH +import Language.Haskell.TH.Syntax + +-- listFields :: Name -> Q [Dec] +-- listFields name = do + -- TyConI (DataD _ _ _ [RecC _ fields] _ ) <- reify name + +strHead :: [String] -> String +strHead strxs = + case uncons strxs of + Just (str, _) -> str + _ -> [] + +instructionRegex :: String +instructionRegex = "instruction[a-zA-Z0-9]* ::" + +testRegex :: String -> Bool +testRegex str = str =~ instructionRegex :: Bool thTest :: IO () thTest = do handle <- openFile "src/HushGP/Instructions/IntInstructions.hs" ReadMode let list = hGetContents handle toPrint <- list - print toPrint + let funcs = map (strHead . words) (filter testRegex (lines toPrint)) + let names = map (newName :: (String -> IO Name)) funcs hClose handle + print "hello" + -- let instruction = "instructionIntAdd"