summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-02-14 17:15:05 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-02-14 17:15:05 +1300
commit775d5cd3a313ddac799c187e518bf6925023c3ee (patch)
tree02225dad153d6701050a1f316883c57983c5be59
parent74b380efbcbdc1bb2558c2c5bced43fd36dff6fb (diff)
Generalise binary arithmetic operations
-rw-r--r--DSL.hs17
1 files changed, 8 insertions, 9 deletions
diff --git a/DSL.hs b/DSL.hs
index ea4c8c2..06c0724 100644
--- a/DSL.hs
+++ b/DSL.hs
@@ -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