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{-# OPTIONS_GHC -Wno-x-partial #-}
 5
 6module Language.QBE.Util where
 7
 8import Data.Word (Word64)
 9import Language.QBE.Numbers (decimal, fractExponent, hexnum, sign)
10import Numeric (readDec)
11import Text.ParserCombinators.Parsec
12  ( Parser,
13    digit,
14    many1,
15    oneOf,
16    skipMany,
17    string,
18    (<|>),
19  )
20
21readWord64 :: String -> Word64
22readWord64 = fst . head . readDec
23
24------------------------------------------------------------------------
25
26bind :: String -> a -> Parser a
27bind str val = val <$ string str
28
29decNumber :: Parser Word64
30decNumber = do
31  -- TODO: QBE probably doesn't support a '+' sign prefix
32  s <- sign
33  n <- many1 digit
34  return (s $ readWord64 n)
35
36-- A float parser that tries to be compatible with strtod(3).
37float :: (Floating f, Read f) => Parser f
38float = do
39  _ <- skipSpace
40  s <- sign
41  -- TODO: Support infininty and NaN
42  (decimal <|> hexnum) >>= fractExponent . s
43  where
44    -- See musl's isspace(3) implementation.
45    skipSpace :: Parser ()
46    skipSpace = skipMany $ oneOf " \t\n\v\f\r"