1-- SPDX-FileCopyrightText: 2025 Sören Tempel <soeren+git@soeren-tempel.net>2--3-- SPDX-License-Identifier: GPL-3.0-only45module Language.QBE.Util where67import Data.Word (Word64)8import Language.QBE.Numbers9 ( decimal,10 fractExponent,11 hexnum,12 octnum,13 sign,14 signMinus,15 )16import Text.ParserCombinators.Parsec17 ( Parser,18 char,19 oneOf,20 skipMany,21 string,22 (<|>),23 )2425bind :: String -> a -> Parser a26bind str val = val <$ string str2728decNumber :: Parser Word6429decNumber = do30 s <- signMinus31 s <$> decimal3233octNumber :: Parser Word6434octNumber = do35 char '0' >> octnum3637-- A float parser that tries to be compatible with strtod(3).38float :: (Floating f, Read f) => Parser f39float = do40 _ <- skipSpace41 s <- sign42 -- TODO: Support infininty and NaN43 -- TODO: Also parse `1.0`, `1.`, and `1` (check qbe's parser)44 (decimal <|> hexnum) >>= fractExponent . s45 where46 -- See musl's isspace(3) implementation.47 skipSpace :: Parser ()48 skipSpace = skipMany $ oneOf " \t\n\v\f\r"