forked from B-Lang-org/bsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeOps.hs
31 lines (26 loc) · 1.18 KB
/
TypeOps.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module TypeOps(opNumT, numOpNames, opStrT, strOpNames) where
-- common routines for handling numeric and string types
import Id
import PreIds(idTAdd, idTSub, idTMul, idTDiv, idTLog, idTExp, idTMax, idTMin, idTStrCat, idTNumToStr)
import Util(divC, log2)
import FStringCompat(FString, concatFString)
-- do a numeric type operation on a list of arguments
-- note that we have to validate that the result is going to
-- to be >= 0 - otherwise it isn't a valid numeric type
opNumT :: Id -> [Integer] -> Maybe Integer
opNumT i [x, y] | i == idTAdd = Just (x + y)
opNumT i [x, y] | i == idTSub && x >= y = Just (x - y)
opNumT i [x, y] | i == idTMul = Just (x * y)
opNumT i [x, y] | i == idTDiv && y /= 0 = Just (divC x y)
opNumT i [x] | i == idTExp = Just (2^x)
opNumT i [x] | i == idTLog && x /= 0 = Just (log2 x)
opNumT i [x, y] | i == idTMax = Just (max x y)
opNumT i [x, y] | i == idTMin = Just (min x y)
opNumT _ _ = Nothing
numOpNames :: [Id]
numOpNames = [idTAdd, idTSub, idTMul, idTDiv, idTExp, idTLog, idTMax, idTMin, idTNumToStr]
opStrT :: Id -> [FString] -> Maybe FString
opStrT i xs | i == idTStrCat = Just $ concatFString xs
opStrT _ _ = Nothing
strOpNames :: [Id]
strOpNames = [idTStrCat]