float docs, typo fixes, string -> float fix

This commit is contained in:
Rowan Torbitzky-Lane 2025-02-09 14:27:45 -06:00
parent 6a78fd0ba6
commit efb4d80962
3 changed files with 44 additions and 5 deletions

View File

@ -346,7 +346,7 @@ instructionCodeRot = instructionRot code
instructionCodeFlush :: State -> State instructionCodeFlush :: State -> State
instructionCodeFlush = instructionFlush code 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 :: State -> State
instructionCodeEq = instructionEq code instructionCodeEq = instructionEq code

View File

@ -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 {_bool = b1 : bs, _float = fs}) = state {_bool = bs, _float = (if b1 then 1.0 else 0.0) : fs}
instructionFloatFromBool state = state 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
instructionFloatFromChar state@(State {_char = c1 : cs, _float = fs}) = state {_char = cs, _float = (fromIntegral (ord c1) :: Float) : fs} instructionFloatFromChar state@(State {_char = c1 : cs, _float = fs}) = state {_char = cs, _float = (fromIntegral (ord c1) :: Float) : fs}
instructionFloatFromChar state = state 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. -- |Reads the top string and converts it to a float if possible. If not, acts as a NoOp.
instructionFloatFromString :: State -> State instructionFloatFromString :: State -> State
instructionFloatFromString state@(State {_string = s1 : ss, _float = fs}) = 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} then state{_string = ss, _float = read @Float s1 : fs}
else state else state
instructionFloatFromString state = state instructionFloatFromString state = state
@ -117,7 +117,8 @@ instructionFloatRot = instructionRot float
instructionFloatFlush :: State -> State instructionFloatFlush :: State -> State
instructionFloatFlush = instructionFlush float 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 :: State -> State
instructionFloatEq = instructionEq float instructionFloatEq = instructionEq float
@ -130,7 +131,7 @@ instructionFloatStackDepth = instructionStackDepth float
instructionFloatYankDup :: State -> State instructionFloatYankDup :: State -> State
instructionFloatYankDup = instructionYankDup float 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. -- the top int from the int stack.
instructionFloatYank :: State -> State instructionFloatYank :: State -> State
instructionFloatYank = instructionYank float instructionFloatYank = instructionYank float

View File

@ -5,18 +5,22 @@ import HushGP.Instructions.GenericInstructions
import Data.Char import Data.Char
-- import Debug.Trace -- import Debug.Trace
-- |Converts the top float to an int and pushes the result to the int stack.
instructionIntFromFloat :: State -> State instructionIntFromFloat :: State -> State
instructionIntFromFloat state@(State {_float = f1 : fs, _int = is}) = state {_float = fs, _int = floor f1 : is} instructionIntFromFloat state@(State {_float = f1 : fs, _int = is}) = state {_float = fs, _int = floor f1 : is}
instructionIntFromFloat state = state instructionIntFromFloat state = state
-- |If the top bool True, pushes 1 to the int stack. Pushes 0 if False.
instructionIntFromBool :: State -> State 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 {_bool = b1 : bs, _int = is}) = state {_bool = bs, _int = (if b1 then 1 else 0) : is}
instructionIntFromBool state = state 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
instructionIntFromChar state@(State {_char = c1 : cs, _int = is}) = state {_char = cs, _int = ord c1 : is} instructionIntFromChar state@(State {_char = c1 : cs, _int = is}) = state {_char = cs, _int = ord c1 : is}
instructionIntFromChar state = state 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
instructionIntFromString state@(State {_string = s1 : ss, _int = is}) = instructionIntFromString state@(State {_string = s1 : ss, _int = is}) =
if all isDigit s1 if all isDigit s1
@ -24,96 +28,130 @@ instructionIntFromString state@(State {_string = s1 : ss, _int = is}) =
else state else state
instructionIntFromString state = 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
instructionIntAdd state@(State {_int = i1 : i2 : is}) = state {_int = i2 + i1 : is} instructionIntAdd state@(State {_int = i1 : i2 : is}) = state {_int = i2 + i1 : is}
instructionIntAdd state = state 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
instructionIntSub state@(State {_int = i1 : i2 : is}) = state {_int = i2 - i1 : is} instructionIntSub state@(State {_int = i1 : i2 : is}) = state {_int = i2 - i1 : is}
instructionIntSub state = state 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
instructionIntMul state@(State {_int = i1 : i2 : is}) = state {_int = i2 * i1 : is} instructionIntMul state@(State {_int = i1 : i2 : is}) = state {_int = i2 * i1 : is}
instructionIntMul state = state 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
instructionIntDiv state@(State {_int = i1 : i2 : is}) = state {_int = if i1 /= 0 then (i2 `div` i1) : is else i1 : i2 : is} instructionIntDiv state@(State {_int = i1 : i2 : is}) = state {_int = if i1 /= 0 then (i2 `div` i1) : is else i1 : i2 : is}
instructionIntDiv state = state 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
instructionIntMod state@(State {_int = i1 : i2 : is}) = state {_int = if i1 /= 0 then (i2 `mod` i1) : is else i1 : i2 : is} instructionIntMod state@(State {_int = i1 : i2 : is}) = state {_int = if i1 /= 0 then (i2 `mod` i1) : is else i1 : i2 : is}
instructionIntMod state = state 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
instructionIntMin state@(State {_int = i1 : i2 : is}) = state {_int = min i1 i2 : is} instructionIntMin state@(State {_int = i1 : i2 : is}) = state {_int = min i1 i2 : is}
instructionIntMin state = state 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
instructionIntMax state@(State {_int = i1 : i2 : is}) = state {_int = max i1 i2 : is} instructionIntMax state@(State {_int = i1 : i2 : is}) = state {_int = max i1 i2 : is}
instructionIntMax state = state 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
instructionIntInc state@(State {_int = i1 : is}) = state {_int = i1 + 1 : is} instructionIntInc state@(State {_int = i1 : is}) = state {_int = i1 + 1 : is}
instructionIntInc state = state 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
instructionIntDec state@(State {_int = i1 : is}) = state {_int = i1 - 1 : is} instructionIntDec state@(State {_int = i1 : is}) = state {_int = i1 - 1 : is}
instructionIntDec state = state 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
instructionIntLT state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 < i2) : bs} instructionIntLT state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 < i2) : bs}
instructionIntLT state = state 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
instructionIntGT state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 > i2) : bs} instructionIntGT state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 > i2) : bs}
instructionIntGT state = state 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
instructionIntLTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 <= i2) : bs} instructionIntLTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 <= i2) : bs}
instructionIntLTE state = state 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
instructionIntGTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 >= i2) : bs} instructionIntGTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 >= i2) : bs}
instructionIntGTE state = state instructionIntGTE state = state
-- |Pops the top int from the int stack.
instructionIntDup :: State -> State instructionIntDup :: State -> State
instructionIntDup = instructionDup int instructionIntDup = instructionDup int
-- |Duplicates the top int on the int stack.
instructionIntPop :: State -> State instructionIntPop :: State -> State
instructionIntPop = instructionPop int 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 :: State -> State
instructionIntDupN = instructionDupN int instructionIntDupN = instructionDupN int
-- |Swaps the top two ints on the int stack.
instructionIntSwap :: State -> State instructionIntSwap :: State -> State
instructionIntSwap = instructionSwap int instructionIntSwap = instructionSwap int
-- |Rotates the top three ints and pushes the result to the int stack.
instructionIntRot :: State -> State instructionIntRot :: State -> State
instructionIntRot = instructionRot int instructionIntRot = instructionRot int
-- |Sets the int stack to [].
instructionIntFlush :: State -> State instructionIntFlush :: State -> State
instructionIntFlush = instructionFlush int instructionIntFlush = instructionFlush int
-- |Checks if the top two floats are equal
instructionIntEq :: State -> State instructionIntEq :: State -> State
instructionIntEq = instructionEq int instructionIntEq = instructionEq int
-- |Pushes the depth of the int stack to top of the int stack after the caluculation.
instructionIntStackDepth :: State -> State instructionIntStackDepth :: State -> State
instructionIntStackDepth = instructionStackDepth int 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 :: State -> State
instructionIntYank = instructionYank int 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 :: State -> State
instructionIntYankDup = instructionYankDup int 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 :: State -> State
instructionIntShove = instructionShove int 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 :: State -> State
instructionIntShoveDup = instructionShoveDup int instructionIntShoveDup = instructionShoveDup int
-- |Pushes True to the bool stack if the int stack is empty. False if not.
instructionIntIsStackEmpty :: State -> State instructionIntIsStackEmpty :: State -> State
instructionIntIsStackEmpty = instructionIsStackEmpty int 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 :: State -> State
instructionIntDupItems = instructionDupItems int instructionIntDupItems = instructionDupItems int