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
 6  ( Program,
 7    Definition (..),
 8    globalFuncs,
 9    Language.QBE.parse,
10  )
11where
12
13import Data.Maybe (mapMaybe)
14import Language.QBE.Parser (dataDef, funcDef, typeDef)
15import Language.QBE.Types (DataDef, FuncDef, TypeDef)
16import Text.ParserCombinators.Parsec
17  ( ParseError,
18    Parser,
19    SourceName,
20    choice,
21    eof,
22    many,
23    parse,
24  )
25
26data Definition
27  = DefData DataDef
28  | DefType TypeDef
29  | DefFunc FuncDef
30  deriving (Eq, Show)
31
32parseDef :: Parser Definition
33parseDef =
34  choice
35    [ DefType <$> typeDef,
36      DefFunc <$> funcDef,
37      DefData <$> dataDef
38    ]
39
40type Program = [Definition]
41
42globalFuncs :: Program -> [FuncDef]
43globalFuncs = mapMaybe globalFuncs'
44  where
45    globalFuncs' :: Definition -> Maybe FuncDef
46    globalFuncs' (DefFunc f) = Just f
47    globalFuncs' _ = Nothing
48
49parse :: SourceName -> String -> Either ParseError Program
50parse = Text.ParserCombinators.Parsec.parse (many parseDef <* eof)