convert to basic lenses, need to abstract

This commit is contained in:
Rowan Torbitzky-Lane 2025-01-18 19:56:27 -06:00
parent 7a33477b0c
commit 1907ac6c87
3 changed files with 54 additions and 54 deletions

View File

@ -4,22 +4,22 @@ import State
import Instructions.IntInstructions import Instructions.IntInstructions
instructionExecIf :: State -> State instructionExecIf :: State -> State
instructionExecIf state@(State {exec = (e1 : e2 : es), bool = (b : _)}) = instructionExecIf state@(State {_exec = (e1 : e2 : es), _bool = (b : _)}) =
if b if b
then state {exec = e1 : es} then state {_exec = e1 : es}
else state {exec = e2 : es} else state {_exec = e2 : es}
instructionExecIf state = state instructionExecIf state = state
instructionExecDup :: State -> State instructionExecDup :: State -> State
instructionExecDup state@(State {exec = alles@(e0 : _)}) = instructionExecDup state@(State {_exec = alles@(e : _)}) =
state {exec = e0 : alles} state {_exec = e : alles}
instructionExecDup state = state instructionExecDup state = state
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, StateFunc instructionExecDoRange, e1] : es, int = i1 : is} then state {_exec = e1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, StateFunc 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
increment destIdx currentIdx increment destIdx currentIdx
@ -29,37 +29,37 @@ instructionExecDoRange state@(State {exec = (e1 : es), int = (i0 : i1 : is)}) =
instructionExecDoRange state = state instructionExecDoRange state = state
instructionExecDoCount :: State -> State instructionExecDoCount :: State -> State
instructionExecDoCount state@(State {exec = (e1 : es), int = (i1 : is)}) = instructionExecDoCount state@(State {_exec = (e : es), _int = (i : is)}) =
if i1 < 1 if i < 1
then state then state
else state {exec = Block [GeneInt 0, GeneInt $ i1 - 1, StateFunc instructionExecDoRange, e1] : es, int = is} else state {_exec = Block [GeneInt 0, GeneInt $ i - 1, StateFunc instructionExecDoRange, e] : es, _int = is}
instructionExecDoCount state = state instructionExecDoCount state = state
instructionExecDoTimes :: State -> State instructionExecDoTimes :: State -> State
instructionExecDoTimes state@(State {exec = (e1 : es), int = (i1 : is)}) = instructionExecDoTimes state@(State {_exec = (e : es), _int = (i : is)}) =
if i1 < 1 if i < 1
then state then state
else state {exec = Block [GeneInt 0, GeneInt $ i1 - 1, StateFunc instructionExecDoRange, Block [StateFunc instructionIntPop, e1]] : es, int = is} else state {_exec = Block [GeneInt 0, GeneInt $ i - 1, StateFunc instructionExecDoRange, Block [StateFunc instructionIntPop, e]] : es, _int = is}
instructionExecDoTimes state = state instructionExecDoTimes state = state
instructionExecWhile :: State -> State instructionExecWhile :: State -> State
instructionExecWhile state@(State {exec = (_ : es), bool = []}) = instructionExecWhile state@(State {_exec = (_ : es), _bool = []}) =
state {exec = es} state {_exec = es}
instructionExecWhile state@(State {exec = alles@(e1 : es), bool = (b1 : bs)}) = instructionExecWhile state@(State {_exec = alles@(e : es), _bool = (b : bs)}) =
if b1 if b
then state {exec = e1 : StateFunc instructionExecWhile : alles, bool = bs} then state {_exec = e : StateFunc instructionExecWhile : alles, _bool = bs}
else state {exec = es} else state {_exec = es}
instructionExecWhile state = state instructionExecWhile state = state
instructionExecDoWhile :: State -> State instructionExecDoWhile :: State -> State
instructionExecDoWhile state@(State {exec = alles@(e1 : _)}) = instructionExecDoWhile state@(State {_exec = alles@(e : _)}) =
state {exec = e1 : StateFunc instructionExecWhile : alles} state {_exec = e : StateFunc instructionExecWhile : alles}
instructionExecDoWhile state = state instructionExecDoWhile state = state
-- Eats the boolean no matter what -- Eats the _boolean no matter what
instructionExecWhen :: State -> State instructionExecWhen :: State -> State
instructionExecWhen state@(State {exec = (_ : es), bool = (b1 : bs)}) = instructionExecWhen state@(State {_exec = (_ : es), _bool = (b : bs)}) =
if not b1 if not b
then state {exec = es, bool = bs} then state {_exec = es, _bool = bs}
else state {bool = bs} else state {_bool = bs}
instructionExecWhen state = state instructionExecWhen state = state

View File

@ -3,54 +3,54 @@ module Instructions.FloatInstructions where
import State import State
instructionFloatAdd :: State -> State instructionFloatAdd :: State -> State
instructionFloatAdd state@(State {float = (f1 : f2 : fs)}) = state {float = f2 + f1 : fs} instructionFloatAdd state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 + f1 : fs}
instructionFloatAdd state = state instructionFloatAdd state = state
instructionFloatSub :: State -> State 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
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}
instructionFloatMul state = state instructionFloatMul state = state
instructionFloatDiv :: State -> State 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
instructionFloatMin :: State -> State instructionFloatMin :: State -> State
instructionFloatMin state@(State {float = (f1 : f2 : fs)}) = state {float = min f1 f2 : fs} instructionFloatMin state@(State {_float = (f1 : f2 : fs)}) = state {_float = min f1 f2 : fs}
instructionFloatMin state = state instructionFloatMin state = state
instructionFloatMax :: State -> State instructionFloatMax :: State -> State
instructionFloatMax state@(State {float = (f1 : f2 : fs)}) = state {float = max f1 f2 : fs} instructionFloatMax state@(State {_float = (f1 : f2 : fs)}) = state {_float = max f1 f2 : fs}
instructionFloatMax state = state instructionFloatMax state = state
instructionFloatInc :: State -> State instructionFloatInc :: State -> State
instructionFloatInc state@(State {float = (f1 : fs)}) = state {float = f1 + 1 : fs} instructionFloatInc state@(State {_float = (f1 : fs)}) = state {_float = f1 + 1 : fs}
instructionFloatInc state = state instructionFloatInc state = state
instructionFloatDec :: State -> State instructionFloatDec :: State -> State
instructionFloatDec state@(State {float = (f1 : fs)}) = state {float = f1 - 1 : fs} instructionFloatDec state@(State {_float = (f1 : fs)}) = state {_float = f1 - 1 : fs}
instructionFloatDec state = state instructionFloatDec state = state
instructionFloatLT :: State -> State instructionFloatLT :: State -> State
instructionFloatLT state@(State {float = f1 : f2 : fs, bool = bs}) = state {float = fs, bool = (f1 < f2) : bs} instructionFloatLT state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 < f2) : bs}
instructionFloatLT state = state instructionFloatLT state = state
instructionFloatGT :: State -> State instructionFloatGT :: State -> State
instructionFloatGT state@(State {float = f1 : f2 : fs, bool = bs}) = state {float = fs, bool = (f1 > f2) : bs} instructionFloatGT state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 > f2) : bs}
instructionFloatGT state = state instructionFloatGT state = state
instructionFloatLTE :: State -> State instructionFloatLTE :: State -> State
instructionFloatLTE state@(State {float = f1 : f2 : fs, bool = bs}) = state {float = fs, bool = (f1 <= f2) : bs} instructionFloatLTE state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 <= f2) : bs}
instructionFloatLTE state = state instructionFloatLTE state = state
instructionFloatGTE :: State -> State instructionFloatGTE :: State -> State
instructionFloatGTE state@(State {float = f1 : f2 : fs, bool = bs}) = state {float = fs, bool = (f1 >= f2) : bs} instructionFloatGTE state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 >= f2) : bs}
instructionFloatGTE state = state instructionFloatGTE state = state
instructionFloatPop :: State -> State instructionFloatPop :: State -> State
instructionFloatPop state@(State {float = (_ : fs)}) = state {float = fs} instructionFloatPop state@(State {_float = (_ : fs)}) = state {_float = fs}
instructionFloatPop state = state instructionFloatPop state = state

View File

@ -4,57 +4,57 @@ import State
-- import Debug.Trace -- import Debug.Trace
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
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
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
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
instructionIntMod :: State -> State instructionIntMod :: State -> State
instructionIntMod state@(State {int = (i1 : i2 : is)}) = state {int = i2 `mod` i1 : is} instructionIntMod state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 `mod` i1 : is}
instructionIntMod state = state instructionIntMod state = state
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
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
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
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
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
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
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
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
instructionIntPop :: State -> State instructionIntPop :: State -> State
instructionIntPop state@(State {int = (_ : is)}) = state {int = is} instructionIntPop state@(State {_int = (_ : is)}) = state {_int = is}
instructionIntPop state = state instructionIntPop state = state