-- | Building blocks for parsing prim primexpressions.  *Not* an infix
-- representation.
module Futhark.Analysis.PrimExp.Parse
  ( pPrimExp,
    pPrimValue,

    -- * Module reexport
    module Futhark.Analysis.PrimExp,
  )
where

import Data.Functor
import Data.Text qualified as T
import Data.Void
import Futhark.Analysis.PrimExp
import Futhark.Util.Pretty (prettyText)
import Language.Futhark.Primitive.Parse
import Text.Megaparsec

pBinOp :: Parsec Void T.Text BinOp
pBinOp :: Parsec Void Text BinOp
pBinOp = forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall {b}. Pretty b => b -> ParsecT Void Text Identity b
p [BinOp]
allBinOps
  where
    p :: b -> ParsecT Void Text Identity b
p b
op = Text -> Parsec Void Text ()
keyword (forall a. Pretty a => a -> Text
prettyText b
op) forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> b
op

pCmpOp :: Parsec Void T.Text CmpOp
pCmpOp :: Parsec Void Text CmpOp
pCmpOp = forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall {b}. Pretty b => b -> ParsecT Void Text Identity b
p [CmpOp]
allCmpOps
  where
    p :: b -> ParsecT Void Text Identity b
p b
op = Text -> Parsec Void Text ()
keyword (forall a. Pretty a => a -> Text
prettyText b
op) forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> b
op

pUnOp :: Parsec Void T.Text UnOp
pUnOp :: Parsec Void Text UnOp
pUnOp = forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall {b}. Pretty b => b -> ParsecT Void Text Identity b
p [UnOp]
allUnOps
  where
    p :: b -> ParsecT Void Text Identity b
p b
op = Text -> Parsec Void Text ()
keyword (forall a. Pretty a => a -> Text
prettyText b
op) forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> b
op

pConvOp :: Parsec Void T.Text ConvOp
pConvOp :: Parsec Void Text ConvOp
pConvOp = forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall {b}. Pretty b => b -> ParsecT Void Text Identity b
p [ConvOp]
allConvOps
  where
    p :: b -> ParsecT Void Text Identity b
p b
op = Text -> Parsec Void Text ()
keyword (forall a. Pretty a => a -> Text
prettyText b
op) forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> b
op

parens :: Parsec Void T.Text a -> Parsec Void T.Text a
parens :: forall a. Parsec Void Text a -> Parsec Void Text a
parens = forall (m :: * -> *) open close a.
Applicative m =>
m open -> m close -> m a -> m a
between (forall a. Parsec Void Text a -> Parsec Void Text a
lexeme Parsec Void Text Text
"(") (forall a. Parsec Void Text a -> Parsec Void Text a
lexeme Parsec Void Text Text
")")

-- | Parse a 'PrimExp' given a leaf parser.
pPrimExp :: PrimType -> Parsec Void T.Text v -> Parsec Void T.Text (PrimExp v)
pPrimExp :: forall v.
PrimType -> Parsec Void Text v -> Parsec Void Text (PrimExp v)
pPrimExp PrimType
t Parsec Void Text v
pLeaf =
  forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
    [ forall a b c. (a -> b -> c) -> b -> a -> c
flip forall v. v -> PrimType -> PrimExp v
LeafExp PrimType
t forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parsec Void Text v
pLeaf,
      forall v. PrimValue -> PrimExp v
ValueExp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parsec Void Text PrimValue
pPrimValue,
      Parsec Void Text BinOp
pBinOp forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= BinOp -> ParsecT Void Text Identity (PrimExp v)
binOpExp,
      Parsec Void Text CmpOp
pCmpOp forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CmpOp -> ParsecT Void Text Identity (PrimExp v)
cmpOpExp,
      Parsec Void Text ConvOp
pConvOp forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ConvOp -> ParsecT Void Text Identity (PrimExp v)
convOpExp,
      Parsec Void Text UnOp
pUnOp forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= UnOp -> ParsecT Void Text Identity (PrimExp v)
unOpExp,
      forall a. Parsec Void Text a -> Parsec Void Text a
parens forall a b. (a -> b) -> a -> b
$ forall v.
PrimType -> Parsec Void Text v -> Parsec Void Text (PrimExp v)
pPrimExp PrimType
t Parsec Void Text v
pLeaf
    ]
  where
    binOpExp :: BinOp -> ParsecT Void Text Identity (PrimExp v)
binOpExp BinOp
op =
      forall v. BinOp -> PrimExp v -> PrimExp v -> PrimExp v
BinOpExp BinOp
op
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall v.
PrimType -> Parsec Void Text v -> Parsec Void Text (PrimExp v)
pPrimExp (BinOp -> PrimType
binOpType BinOp
op) Parsec Void Text v
pLeaf
        forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall v.
PrimType -> Parsec Void Text v -> Parsec Void Text (PrimExp v)
pPrimExp (BinOp -> PrimType
binOpType BinOp
op) Parsec Void Text v
pLeaf
    cmpOpExp :: CmpOp -> ParsecT Void Text Identity (PrimExp v)
cmpOpExp CmpOp
op =
      forall v. CmpOp -> PrimExp v -> PrimExp v -> PrimExp v
CmpOpExp CmpOp
op
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall v.
PrimType -> Parsec Void Text v -> Parsec Void Text (PrimExp v)
pPrimExp (CmpOp -> PrimType
cmpOpType CmpOp
op) Parsec Void Text v
pLeaf
        forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall v.
PrimType -> Parsec Void Text v -> Parsec Void Text (PrimExp v)
pPrimExp (CmpOp -> PrimType
cmpOpType CmpOp
op) Parsec Void Text v
pLeaf
    convOpExp :: ConvOp -> ParsecT Void Text Identity (PrimExp v)
convOpExp ConvOp
op =
      forall v. ConvOp -> PrimExp v -> PrimExp v
ConvOpExp ConvOp
op forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall v.
PrimType -> Parsec Void Text v -> Parsec Void Text (PrimExp v)
pPrimExp (forall a b. (a, b) -> a
fst (ConvOp -> (PrimType, PrimType)
convOpType ConvOp
op)) Parsec Void Text v
pLeaf
    unOpExp :: UnOp -> ParsecT Void Text Identity (PrimExp v)
unOpExp UnOp
op =
      forall v. UnOp -> PrimExp v -> PrimExp v
UnOpExp UnOp
op forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall v.
PrimType -> Parsec Void Text v -> Parsec Void Text (PrimExp v)
pPrimExp (UnOp -> PrimType
unOpType UnOp
op) Parsec Void Text v
pLeaf