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
10 ( decimal,
11 fractExponent,
12 hexnum,
13 octnum,
14 sign,
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 -- TODO: QBE probably doesn't support a '+' sign prefix
31 s <- sign
32 s <$> decimal
33
34octNumber :: Parser Word64
35octNumber = do
36 char '0' >> octnum
37
38-- A float parser that tries to be compatible with strtod(3).
39float :: (Floating f, Read f) => Parser f
40float = do
41 _ <- skipSpace
42 s <- sign
43 -- TODO: Support infininty and NaN
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"