gonna abandon TH

This commit is contained in:
Rowan Torbitzky-Lane 2025-02-11 16:30:15 -06:00
parent cff71ac4ca
commit 2f2b19e3d0
3 changed files with 31 additions and 19 deletions

View File

@ -96,19 +96,11 @@ instructionCodeDoThenPop :: State -> State
instructionCodeDoThenPop state@(State {_code = c1 : _, _exec = es}) = state {_exec = c1 : StateFunc (instructionCodePop, "instructionCodePop") : es} instructionCodeDoThenPop state@(State {_code = c1 : _, _exec = es}) = state {_exec = c1 : StateFunc (instructionCodePop, "instructionCodePop") : es}
instructionCodeDoThenPop state = state 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. -- |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
instructionCodeDoRange state@(State {_code = c1 : cs, _int = i0 : i1 : is, _exec = es}) = instructionCodeDoRange state@(State {_code = c1 : cs, _int = i0 : i1 : is, _exec = es}) =
if increment i0 i1 /= 0 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} else state {_exec = c1: es, _int = i1 : is, _code = cs}
where where
increment :: Int -> Int -> Int increment :: Int -> Int -> Int
@ -123,7 +115,7 @@ instructionCodeDoCount :: State -> State
instructionCodeDoCount state@(State {_code = c : cs, _int = i1 : is, _exec = es}) = instructionCodeDoCount state@(State {_code = c : cs, _int = i1 : is, _exec = es}) =
if i1 < 1 if i1 < 1
then state 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 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. -- |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}) = instructionCodeDoTimes state@(State {_code = c : cs, _int = i1 : is, _exec = es}) =
if i1 < 1 if i1 < 1
then state 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 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. -- |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.

View File

@ -70,16 +70,12 @@ instructionExecShoveDup = instructionShoveDup exec
instructionExecIsStackEmpty :: State -> State instructionExecIsStackEmpty :: State -> State
instructionExecIsStackEmpty = instructionIsStackEmpty exec 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 -- |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. -- 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
instructionExecDoRange state@(State {_exec = e1 : es, _int = i0 : i1 : is}) = instructionExecDoRange state@(State {_exec = e1 : es, _int = i0 : i1 : is}) =
if increment i0 i1 /= 0 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} else state {_exec = e1 : es, _int = i1 : is}
where where
increment :: Int -> Int -> Int increment :: Int -> Int -> Int
@ -95,7 +91,7 @@ instructionExecDoCount :: State -> State
instructionExecDoCount state@(State {_exec = e1 : es, _int = i1 : is}) = instructionExecDoCount state@(State {_exec = e1 : es, _int = i1 : is}) =
if i1 < 1 if i1 < 1
then state 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 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. -- |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}) = instructionExecDoTimes state@(State {_exec = e1 : es, _int = i1 : is}) =
if i1 < 1 if i1 < 1
then state 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 instructionExecDoTimes state = state
-- |Utility: A shorthand for instructionExecWhile -- |Utility: A shorthand for instructionExecWhile

View File

@ -2,11 +2,35 @@
module HushGP.TH where module HushGP.TH where
import System.IO 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 :: IO ()
thTest = do thTest = do
handle <- openFile "src/HushGP/Instructions/IntInstructions.hs" ReadMode handle <- openFile "src/HushGP/Instructions/IntInstructions.hs" ReadMode
let list = hGetContents handle let list = hGetContents handle
toPrint <- list toPrint <- list
print toPrint let funcs = map (strHead . words) (filter testRegex (lines toPrint))
let names = map (newName :: (String -> IO Name)) funcs
hClose handle hClose handle
print "hello"
-- let instruction = "instructionIntAdd"