0 | module Text.ILex.Util
2 | import Data.ByteString
3 | import Data.Linear.Ref1
4 | import Derive.Prelude
7 | import Text.ParseError
8 | import Text.ILex.Parser
9 | import Text.ILex.RExp
18 | decimaldigit : Bits8 -> Integer
19 | decimaldigit x = cast x - 48
22 | hexdigit : Bits8 -> Integer
24 | if x <= byte_9 then cast x - cast byte_0
25 | else if x <= byte_F then 10 + cast x - cast byte_A
26 | else 10 + cast x - cast byte_a
28 | parameters (bv : ByteVect n)
30 | binaryBV : Integer -> (k : Nat) -> (x : Ix k n) => Integer
31 | binaryBV res 0 = res
32 | binaryBV res (S k) =
34 | 48 => binaryBV (res * 2) k
35 | _ => binaryBV (res * 2 + 1) k
38 | binarySepBV : Integer -> (k : Nat) -> (x : Ix k n) => Integer
39 | binarySepBV res 0 = res
40 | binarySepBV res (S k) =
42 | 48 => binarySepBV (res * 2) k
43 | 49 => binarySepBV (res * 2 + 1) k
44 | _ => binarySepBV res k
47 | octalBV : Integer -> (k : Nat) -> (x : Ix k n) => Integer
49 | octalBV res (S k) = octalBV (res * 8 + decimaldigit (ix bv k)) k
52 | octalSepBV : Bits8 -> Integer -> (k : Nat) -> (x : Ix k n) => Integer
53 | octalSepBV sep res 0 = res
54 | octalSepBV sep res (S k) =
56 | in case prim__eq_Bits8 sep b of
57 | 0 => octalSepBV sep (res * 8 + decimaldigit b) k
58 | _ => octalSepBV sep res k
61 | decimalBV : Integer -> (k : Nat) -> (x : Ix k n) => Integer
62 | decimalBV res 0 = res
63 | decimalBV res (S k) = decimalBV (res * 10 + decimaldigit (ix bv k)) k
66 | decimalSepBV : Bits8 -> Integer -> (k : Nat) -> (x : Ix k n) => Integer
67 | decimalSepBV sep res 0 = res
68 | decimalSepBV sep res (S k) =
70 | in case prim__eq_Bits8 sep b of
71 | 0 => decimalSepBV sep (res * 10 + decimaldigit b) k
72 | _ => decimalSepBV sep res k
75 | hexadecimalBV : Integer -> (k : Nat) -> (x : Ix k n) => Integer
76 | hexadecimalBV res 0 = res
77 | hexadecimalBV res (S k) = hexadecimalBV (res * 16 + hexdigit (ix bv k)) k
80 | hexadecimalSepBV : Bits8 -> Integer -> (k : Nat) -> (x : Ix k n) => Integer
81 | hexadecimalSepBV sep res 0 = res
82 | hexadecimalSepBV sep res (S k) =
84 | in case prim__eq_Bits8 sep b of
85 | 0 => hexadecimalSepBV sep (res * 16 + hexdigit b) k
86 | _ => hexadecimalSepBV sep res k
89 | integerBV : (k : Nat) -> (x : Ix k n) => Integer
93 | 45 => negate $
decimalBV 0 k
95 | b => decimalBV (decimaldigit b) k
99 | binary : ByteString -> Integer
100 | binary (BS n bv) = binaryBV bv 0 n
108 | binarySep : ByteString -> Integer
109 | binarySep (BS n bv) = binarySepBV bv 0 n
113 | octal : ByteString -> Integer
114 | octal (BS n bv) = octalBV bv 0 n
122 | octalSep : Bits8 -> ByteString -> Integer
123 | octalSep sep (BS n bv) = octalSepBV bv sep 0 n
127 | decimal : ByteString -> Integer
128 | decimal (BS n bv) = decimalBV bv 0 n
136 | decimalSep : Bits8 -> ByteString -> Integer
137 | decimalSep sep (BS n bv) = decimalSepBV bv sep 0 n
141 | hexadecimal : ByteString -> Integer
142 | hexadecimal (BS n bv) = hexadecimalBV bv 0 n
150 | hexadecimalSep : Bits8 -> ByteString -> Integer
151 | hexadecimalSep sep (BS n bv) = hexadecimalSepBV bv sep 0 n
156 | integer : ByteString -> Integer
157 | integer (BS n bv) = integerBV bv n
167 | mergeL : Ord o => (o -> e -> e -> e) -> SnocList (e,o) -> e -> e
168 | mergeL merge sp y =
171 | (x,ox)::t => go [<] x ox t y
174 | app : SnocList (e,o) -> e -> o -> List (e,o) -> e -> e
176 | go : SnocList (e,o) -> e -> o -> List (e,o) -> e -> e
179 | [<] => merge ox x z
180 | sp:<(w,ow) => go sp w ow [] (merge ox x z)
182 | go sx x ox ((y,oy) :: xs) z =
183 | case compare ox oy of
184 | LT => go (sx:<(x,ox)) y oy xs z
185 | EQ => go sx (merge ox x y) oy xs z
186 | GT => app sx (merge ox x y) oy xs z
188 | app [<] x ox xs z = go [<] x ox xs z
189 | app outer@(sp:<(w,ow)) x ox xs z =
190 | case compare ow ox of
191 | LT => go outer x ox xs z
192 | _ => app sp (merge ow w x) ox xs z
199 | maybeList : SnocList a -> Maybe (List a)
200 | maybeList [<] = Nothing
201 | maybeList sx = Just (sx <>> [])
209 | snocPack : SnocList String -> String
212 | snocPack ss = fastConcat $
ss <>> []