quebex

A software analysis framework built around the QBE intermediate language

git clone https://git.8pit.net/quebex.git

 1-- SPDX-FileCopyrightText: 2025 Sören Tempel <soeren+git@soeren-tempel.net>
 2--
 3-- SPDX-License-Identifier: GPL-3.0-only
 4
 5module Language.QBE.Util where
 6
 7import Data.Word (Word64)
 8import Language.QBE.Numbers
 9  ( decimal,
10    fractExponent,
11    hexnum,
12    octnum,
13    sign,
14    signMinus,
15  )
16import Text.ParserCombinators.Parsec
17  ( Parser,
18    char,
19    oneOf,
20    skipMany,
21    string,
22    (<|>),
23  )
24
25bind :: String -> a -> Parser a
26bind str val = val <$ string str
27
28decNumber :: Parser Word64
29decNumber = do
30  s <- signMinus
31  s <$> decimal
32
33octNumber :: Parser Word64
34octNumber = do
35  char '0' >> octnum
36
37-- A float parser that tries to be compatible with strtod(3).
38float :: (Floating f, Read f) => Parser f
39float = do
40  _ <- skipSpace
41  s <- sign
42  -- TODO: Support infininty and NaN
43  -- TODO: Also parse `1.0`, `1.`, and `1` (check qbe's parser)
44  (decimal <|> hexnum) >>= fractExponent . s
45  where
46    -- See musl's isspace(3) implementation.
47    skipSpace :: Parser ()
48    skipSpace = skipMany $ oneOf " \t\n\v\f\r"