on master help sign in
String primitives
commit348cf91ff8446c794ca59c1f2cb51997087d4029 authorBenjamin Saunders <besaunde@sfu.ca> dateThu 20 Dec 2012, 16:08 (14 years ago) parent0045b68
3 files changed+28
lib/Prelude/Bits.idr+7 src/IRTS/Lang.hs+1 src/Idris/Primitives.hs+20
lib/Prelude/Bits.idr+7
@@ -56,3 +56,10 @@ bitsUDiv {n=m} x y with (Bits m)
5656 | Bits16 = prim__udivB16 x y
5757 | Bits32 = prim__udivB32 x y
5858 | Bits64 = prim__udivB64 x y
59+
60+bitsToStr : {n : Nat} -> Bits n -> String
61+bitsToStr {n=m} x with (Bits m)
62+ | Bits8 = prim__b8ToStr x
63+ | Bits16 = prim__b16ToStr x
64+ | Bits32 = prim__b32ToStr x
65+ | Bits64 = prim__b64ToStr x
src/IRTS/Lang.hs+1
@@ -49,6 +49,7 @@ data PrimFn = LPlus | LMinus | LTimes | LDiv | LMod
4949 | LB64Plus | LB64Minus | LB64Times | LB64UDiv | LB64SDiv
5050 | LB64T8 | LB64T16 | LB64T32
5151 | LIntB8 | LIntB16 | LIntB32 | LIntB64 | LB32Int
52+ | LB8Str | LB16Str | LB32Str | LB64Str
5253
5354 | LFExp | LFLog | LFSin | LFCos | LFTan | LFASin | LFACos | LFATan
5455 | LFSqrt | LFFloor | LFCeil
src/Idris/Primitives.hs+20
@@ -77,6 +77,8 @@ primitives =
7777 Prim (UN "prim__gteChar") (ty [ChType, ChType] IType) 2 (bcBin (>=))
7878 (2, LGe) total,
7979
80+ Prim (UN "prim__b8ToStr") (ty [B8Type] StrType) 1 (b8ToStr)
81+ (1, LB8Str) total,
8082 Prim (UN "prim__addB8") (ty [B8Type, B8Type] B8Type) 2 (b8Bin (+))
8183 (2, LB8Plus) total,
8284 Prim (UN "prim__subB8") (ty [B8Type, B8Type] B8Type) 2 (b8Bin (-))
@@ -100,6 +102,8 @@ primitives =
100102 Prim (UN "prim__sextB8_64") (ty [B8Type] B64Type) 1 sext8_64
101103 (1, LB8Z64) total,
102104
105+ Prim (UN "prim__b16ToStr") (ty [B16Type] StrType) 1 (b16ToStr)
106+ (1, LB16Str) total,
103107 Prim (UN "prim__addB16") (ty [B16Type, B16Type] B16Type) 2 (b16Bin (+))
104108 (2, LB16Plus) total,
105109 Prim (UN "prim__subB16") (ty [B16Type, B16Type] B16Type) 2 (b16Bin (-))
@@ -121,6 +125,8 @@ primitives =
121125 Prim (UN "prim__truncB16_8") (ty [B16Type] B8Type) 1 trunc16_8
122126 (1, LB16T8) total,
123127
128+ Prim (UN "prim__b32ToStr") (ty [B32Type] StrType) 1 (b32ToStr)
129+ (1, LB32Str) total,
124130 Prim (UN "prim__addB32") (ty [B32Type, B32Type] B32Type) 2 (b32Bin (+))
125131 (2, LB32Plus) total,
126132 Prim (UN "prim__subB32") (ty [B32Type, B32Type] B32Type) 2 (b32Bin (-))
@@ -140,6 +146,8 @@ primitives =
140146 Prim (UN "prim__truncB32_16") (ty [B32Type] B16Type) 1 trunc32_16
141147 (1, LB32T16) total,
142148
149+ Prim (UN "prim__b64ToStr") (ty [B64Type] StrType) 1 (b64ToStr)
150+ (1, LB64Str) total,
143151 Prim (UN "prim__addB64") (ty [B64Type, B64Type] B64Type) 2 (b64Bin (+))
144152 (2, LB64Plus) total,
145153 Prim (UN "prim__subB64") (ty [B64Type, B64Type] B64Type) 2 (b64Bin (-))
@@ -331,6 +339,18 @@ s32div x y = fromIntegral (fromIntegral x `div` fromIntegral y :: Int32)
331339s64div :: Word64 -> Word64 -> Word64
332340s64div x y = fromIntegral (fromIntegral x `div` fromIntegral y :: Int64)
333341
342+b8ToStr [VConstant (B8 x)] = Just $ VConstant (Str (show x))
343+b8ToStr _ = Nothing
344+
345+b16ToStr [VConstant (B16 x)] = Just $ VConstant (Str (show x))
346+b16ToStr _ = Nothing
347+
348+b32ToStr [VConstant (B32 x)] = Just $ VConstant (Str (show x))
349+b32ToStr _ = Nothing
350+
351+b64ToStr [VConstant (B64 x)] = Just $ VConstant (Str (show x))
352+b64ToStr _ = Nothing
353+
334354b8Bin op [VConstant (B8 x), VConstant (B8 y)] = Just $ VConstant (B8 (op x y))
335355b8Bin _ _ = Nothing
336356