diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-02-14 17:15:05 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-02-14 17:15:05 +1300 |
| commit | 775d5cd3a313ddac799c187e518bf6925023c3ee (patch) | |
| tree | 02225dad153d6701050a1f316883c57983c5be59 | |
| parent | 74b380efbcbdc1bb2558c2c5bced43fd36dff6fb (diff) | |
Generalise binary arithmetic operations
| -rw-r--r-- | DSL.hs | 17 |
1 files changed, 8 insertions, 9 deletions
@@ -219,19 +219,18 @@ rot = StackModifier { smName="ROT", smTypes=ts, smFunc=f } f (x:y:z:xs) = return $ y:z:x:xs f _ = unreachable -plus :: StackModifier -plus = StackModifier { smName="PLUS", smTypes=ts, smFunc=f } +binArithmetic :: String -> (Integer -> Integer -> [Integer]) -> StackModifier +binArithmetic name f = StackModifier { smName=name, smTypes=ts, smFunc=f' } where ts = [tInt, tInt] - f (StackInt x:StackInt y:xs) = return $ StackInt (x+y):xs - f _ = unreachable + f' (StackInt x:StackInt y:xs) = return $ (StackInt <$> f x y) ++ xs + f' _ = unreachable + +plus :: StackModifier +plus = binArithmetic "PLUS" (\ x y -> [x + y]) minus :: StackModifier -minus = StackModifier { smName="MINUS", smTypes=ts, smFunc=f } - where - ts = [tInt, tInt] - f (StackInt x:StackInt y:xs) = return $ StackInt (x-y):xs - f _ = unreachable +minus = binArithmetic "MINUS" (\ x y -> [x - y]) ------------------------------------------------------------------------------ -- Core operations |
