add commutative opposites where it applies

This commit is contained in:
Rowan Torbitzky-Lane 2025-02-13 00:39:52 -06:00
parent 24398989be
commit 480f600ad3
2 changed files with 22 additions and 0 deletions

View File

@ -41,6 +41,11 @@ instructionFloatSub :: State -> State
instructionFloatSub state@(State {_float = f1 : f2 : fs}) = state {_float = f2 - f1 : fs} instructionFloatSub state@(State {_float = f1 : f2 : fs}) = state {_float = f2 - f1 : fs}
instructionFloatSub state = state instructionFloatSub state = state
-- |Subtracts the second float from the first float and pushes the result to the float stack.
instructionFloatSubOpp :: State -> State
instructionFloatSubOpp state@(State {_float = i1 : i2 : is}) = state {_float = i1 - i2 : is}
instructionFloatSubOpp state = state
-- |Multiplies the top two floats on the float stack. -- |Multiplies the top two floats on the float stack.
instructionFloatMul :: State -> State instructionFloatMul :: State -> State
instructionFloatMul state@(State {_float = f1 : f2 : fs}) = state {_float = f2 * f1 : fs} instructionFloatMul state@(State {_float = f1 : f2 : fs}) = state {_float = f2 * f1 : fs}
@ -51,6 +56,12 @@ instructionFloatDiv :: State -> State
instructionFloatDiv state@(State {_float = f1 : f2 : fs}) = state {_float = if f1 /= 0 then f2 / f1 : fs else f1 : f2 : fs} instructionFloatDiv state@(State {_float = f1 : f2 : fs}) = state {_float = if f1 /= 0 then f2 / f1 : fs else f1 : f2 : fs}
instructionFloatDiv state = state instructionFloatDiv state = state
-- |Divides the second float from the first float and pushes the result to the float stack.
-- This does truncate.
instructionFloatDivOpp :: State -> State
instructionFloatDivOpp state@(State {_float = i1 : i2 : is}) = state {_float = if i2 /= 0 then (i1 / i2) : is else i1 : i2 : is}
instructionFloatDivOpp state = state
-- |Mods the first float from the second float on the float stack. -- |Mods the first float from the second float on the float stack.
instructionFloatMod :: State -> State instructionFloatMod :: State -> State
instructionFloatMod state@(State {_float = f1 : f2 : fs}) = state {_float = if f1 /= 0 then f2 `mod'` f1 : fs else f1 : f2 : fs} instructionFloatMod state@(State {_float = f1 : f2 : fs}) = state {_float = if f1 /= 0 then f2 `mod'` f1 : fs else f1 : f2 : fs}

View File

@ -39,6 +39,11 @@ 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
-- |Subtracts the second int from the first int and pushes the result to the int stack.
instructionIntSubOpp :: State -> State
instructionIntSubOpp state@(State {_int = i1 : i2 : is}) = state {_int = i1 - i2 : is}
instructionIntSubOpp state = state
-- |Multiplies the top two ints from the int stack and pushes the result to the int stack. -- |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}
@ -50,6 +55,12 @@ 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
-- |Divides the second int from the first int and pushes the result to the int stack.
-- This does truncate.
instructionIntDivOpp :: State -> State
instructionIntDivOpp state@(State {_int = i1 : i2 : is}) = state {_int = if i2 /= 0 then (i1 `div` i2) : is else i1 : i2 : is}
instructionIntDivOpp state = state
-- |Mods the first float from the second float and pushes the result to the int stack. -- |Mods the first float from the second float and pushes the result to the int stack.
-- This does truncate. -- This does truncate.
instructionIntMod :: State -> State instructionIntMod :: State -> State