diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs index 2248de5..599619a 100644 --- a/src/HushGP/Instructions/CodeInstructions.hs +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -346,7 +346,7 @@ instructionCodeRot = instructionRot code instructionCodeFlush :: State -> State instructionCodeFlush = instructionFlush code --- |Checks if the top code items are equal. Pushes true to the bool stack if so, False if not. +-- |Checks if the top code items are equal. Pushes True to the bool stack if so, False if not. instructionCodeEq :: State -> State instructionCodeEq = instructionEq code diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index 2b7e6b5..a0efe95 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -15,7 +15,7 @@ instructionFloatFromBool :: State -> State instructionFloatFromBool state@(State {_bool = b1 : bs, _float = fs}) = state {_bool = bs, _float = (if b1 then 1.0 else 0.0) : fs} instructionFloatFromBool state = state --- |Takes the top char and converts it to int representation. That int then gets casted to a float. +-- |Takes the top char and converts it to int representation. That int then gets casted to a float and pushed to the float stack. instructionFloatFromChar :: State -> State instructionFloatFromChar state@(State {_char = c1 : cs, _float = fs}) = state {_char = cs, _float = (fromIntegral (ord c1) :: Float) : fs} instructionFloatFromChar state = state @@ -23,7 +23,7 @@ instructionFloatFromChar state = state -- |Reads the top string and converts it to a float if possible. If not, acts as a NoOp. instructionFloatFromString :: State -> State instructionFloatFromString state@(State {_string = s1 : ss, _float = fs}) = - if all isDigit s1 + if all (\x -> isDigit x || x == '.') s1 && amtOccurences "." s1 <= 1 then state{_string = ss, _float = read @Float s1 : fs} else state instructionFloatFromString state = state @@ -117,7 +117,8 @@ instructionFloatRot = instructionRot float instructionFloatFlush :: State -> State instructionFloatFlush = instructionFlush float --- |Checks if the top two floats are equal. Pushes the result to the float stack. +-- |Checks if the top two floats are equal. Pushes the result to the bool stack. +-- Might override this later to check for equality in a range rather than exact equality. instructionFloatEq :: State -> State instructionFloatEq = instructionEq float @@ -130,7 +131,7 @@ instructionFloatStackDepth = instructionStackDepth float instructionFloatYankDup :: State -> State instructionFloatYankDup = instructionYankDup float --- |Copies an item from deep within the char stack to the top of the char stack based on +-- |Copies an item from deep within the float stack to the top of the float stack based on -- the top int from the int stack. instructionFloatYank :: State -> State instructionFloatYank = instructionYank float diff --git a/src/HushGP/Instructions/IntInstructions.hs b/src/HushGP/Instructions/IntInstructions.hs index 4d0401b..d1a2682 100644 --- a/src/HushGP/Instructions/IntInstructions.hs +++ b/src/HushGP/Instructions/IntInstructions.hs @@ -5,18 +5,22 @@ import HushGP.Instructions.GenericInstructions import Data.Char -- import Debug.Trace +-- |Converts the top float to an int and pushes the result to the int stack. instructionIntFromFloat :: State -> State instructionIntFromFloat state@(State {_float = f1 : fs, _int = is}) = state {_float = fs, _int = floor f1 : is} instructionIntFromFloat state = state +-- |If the top bool True, pushes 1 to the int stack. Pushes 0 if False. instructionIntFromBool :: State -> State instructionIntFromBool state@(State {_bool = b1 : bs, _int = is}) = state {_bool = bs, _int = (if b1 then 1 else 0) : is} instructionIntFromBool state = state +-- |Takes the top char and converts it to int representation. The result is pushed to the int stack. instructionIntFromChar :: State -> State instructionIntFromChar state@(State {_char = c1 : cs, _int = is}) = state {_char = cs, _int = ord c1 : is} instructionIntFromChar state = state +-- |Reads the top string and converts it to a int if possible. If not, acts as a NoOp. instructionIntFromString :: State -> State instructionIntFromString state@(State {_string = s1 : ss, _int = is}) = if all isDigit s1 @@ -24,96 +28,130 @@ instructionIntFromString state@(State {_string = s1 : ss, _int = is}) = else state instructionIntFromString state = state +-- |Adds the top two ints from the int stack and pushes the result to the int stack. instructionIntAdd :: State -> State instructionIntAdd state@(State {_int = i1 : i2 : is}) = state {_int = i2 + i1 : is} instructionIntAdd state = state +-- |Subtracts the first int from the second int and pushes the result to the int stack. instructionIntSub :: State -> State instructionIntSub state@(State {_int = i1 : i2 : is}) = state {_int = i2 - i1 : is} instructionIntSub state = state +-- |Multiplies the top two ints from the int stack and pushes the result to the int stack. instructionIntMul :: State -> State instructionIntMul state@(State {_int = i1 : i2 : is}) = state {_int = i2 * i1 : is} instructionIntMul state = state +-- |Divides the first float from the second float and pushes the result to the int stack. +-- This does truncate. instructionIntDiv :: State -> State instructionIntDiv state@(State {_int = i1 : i2 : is}) = state {_int = if i1 /= 0 then (i2 `div` i1) : is else i1 : i2 : is} instructionIntDiv state = state +-- |Mods the first float from the second float and pushes the result to the int stack. +-- This does truncate. instructionIntMod :: State -> State instructionIntMod state@(State {_int = i1 : i2 : is}) = state {_int = if i1 /= 0 then (i2 `mod` i1) : is else i1 : i2 : is} instructionIntMod state = state +-- |Takes the top two ints from the int stack and pushes the minimum of the two back on top. instructionIntMin :: State -> State instructionIntMin state@(State {_int = i1 : i2 : is}) = state {_int = min i1 i2 : is} instructionIntMin state = state +-- |Takes the top two ints from the int stack and pushes the maximum of the two back on top. instructionIntMax :: State -> State instructionIntMax state@(State {_int = i1 : i2 : is}) = state {_int = max i1 i2 : is} instructionIntMax state = state +-- |Adds one to the top of the int stack and pushes the result back to the int stack. instructionIntInc :: State -> State instructionIntInc state@(State {_int = i1 : is}) = state {_int = i1 + 1 : is} instructionIntInc state = state +-- |Subtracts one from the top of the int stack and pushes the result back to the int stack. instructionIntDec :: State -> State instructionIntDec state@(State {_int = i1 : is}) = state {_int = i1 - 1 : is} instructionIntDec state = state +-- |Takes the top two ints from the int stack and pushes the result of: the top int item < the second int item instructionIntLT :: State -> State instructionIntLT state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 < i2) : bs} instructionIntLT state = state +-- |Takes the top two ints from the int stack and pushes the result of: the top int item > the second int item instructionIntGT :: State -> State instructionIntGT state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 > i2) : bs} instructionIntGT state = state +-- |Takes the top two ints from the int stack and pushes the result of: the top int item <= the second int item instructionIntLTE :: State -> State instructionIntLTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 <= i2) : bs} instructionIntLTE state = state +-- |Takes the top two ints from the int stack and pushes the result of: the top int item >= the second int item instructionIntGTE :: State -> State instructionIntGTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 >= i2) : bs} instructionIntGTE state = state +-- |Pops the top int from the int stack. instructionIntDup :: State -> State instructionIntDup = instructionDup int +-- |Duplicates the top int on the int stack. instructionIntPop :: State -> State instructionIntPop = instructionPop int +-- |Duplicates the second to top int on the int stack based on the top int +-- and pushes the result to the int stack. instructionIntDupN :: State -> State instructionIntDupN = instructionDupN int +-- |Swaps the top two ints on the int stack. instructionIntSwap :: State -> State instructionIntSwap = instructionSwap int +-- |Rotates the top three ints and pushes the result to the int stack. instructionIntRot :: State -> State instructionIntRot = instructionRot int +-- |Sets the int stack to []. instructionIntFlush :: State -> State instructionIntFlush = instructionFlush int +-- |Checks if the top two floats are equal instructionIntEq :: State -> State instructionIntEq = instructionEq int +-- |Pushes the depth of the int stack to top of the int stack after the caluculation. instructionIntStackDepth :: State -> State instructionIntStackDepth = instructionStackDepth int +-- |Moves an item from deep within the int stack to the top of the int stack based on +-- the top int from the int stack. instructionIntYank :: State -> State instructionIntYank = instructionYank int +-- |Copies an item from deep within the float stack to the top of the float stack based on +-- the top int from the int stack. instructionIntYankDup :: State -> State instructionIntYankDup = instructionYankDup int +-- |Moves an item from the top of the int stack to deep within the int stack based on +-- the top int from the int stack. instructionIntShove :: State -> State instructionIntShove = instructionShove int +-- |Copies an item from the top of the int stack to deep within the int stack based on +-- the top int from the int stack. instructionIntShoveDup :: State -> State instructionIntShoveDup = instructionShoveDup int +-- |Pushes True to the bool stack if the int stack is empty. False if not. instructionIntIsStackEmpty :: State -> State instructionIntIsStackEmpty = instructionIsStackEmpty int +-- |Duplicate the top N items from the int stack based on the top int from the int stack. instructionIntDupItems :: State -> State instructionIntDupItems = instructionDupItems int