module DDC.Core.Salt.Name
( Name (..)
, PrimTyCon (..)
, readPrimTyCon
, primTyConIsIntegral
, primTyConIsFloating
, primTyConIsUnsigned
, primTyConIsSigned
, primTyConWidth
, PrimOp (..)
, PrimArith (..)
, readPrimArith
, PrimCall (..)
, readPrimCall
, PrimCast (..)
, readPrimCast
, primCastPromoteIsValid
, primCastTruncateIsValid
, PrimControl (..)
, readPrimControl
, PrimStore (..)
, readPrimStore
, PrimVec (..)
, readPrimVec
, multiOfPrimVec
, liftPrimArithToVec
, lowerPrimVecToArith
, readLitInteger
, readLitPrimNat
, readLitPrimInt
, readLitPrimWordOfBits
, readLitPrimFloatOfBits
, readName)
where
import DDC.Core.Salt.Name.PrimArith
import DDC.Core.Salt.Name.PrimCall
import DDC.Core.Salt.Name.PrimCast
import DDC.Core.Salt.Name.PrimControl
import DDC.Core.Salt.Name.PrimStore
import DDC.Core.Salt.Name.PrimTyCon
import DDC.Core.Salt.Name.PrimVec
import DDC.Core.Salt.Name.Lit
import DDC.Base.Pretty
import Data.Typeable
import Data.Char
import Data.List
import Control.DeepSeq
data Name
= NameVar String
| NameCon String
| NameObjTyCon
| NamePrimTyCon PrimTyCon
| NamePrimOp PrimOp
| NameLitVoid
| NameLitBool Bool
| NameLitNat Integer
| NameLitInt Integer
| NameLitTag Integer
| NameLitWord Integer Int
deriving (Eq, Ord, Show, Typeable)
instance NFData Name where
rnf name
= case name of
NameVar s -> rnf s
NameCon s -> rnf s
NameObjTyCon -> ()
NamePrimTyCon con -> rnf con
NamePrimOp op -> rnf op
NameLitVoid -> ()
NameLitBool b -> rnf b
NameLitNat i -> rnf i
NameLitInt i -> rnf i
NameLitTag i -> rnf i
NameLitWord i bits -> rnf i `seq` rnf bits
instance Pretty Name where
ppr nn
= case nn of
NameVar n -> text n
NameCon n -> text n
NameObjTyCon -> text "Obj"
NamePrimTyCon tc -> ppr tc
NamePrimOp p -> ppr p
NameLitVoid -> text "V#"
NameLitBool True -> text "True#"
NameLitBool False -> text "False#"
NameLitNat i -> integer i <> text "#"
NameLitInt i -> integer i <> text "i#"
NameLitTag i -> text "TAG" <> integer i <> text "#"
NameLitWord i bits -> integer i <> text "w" <> int bits <> text "#"
readName :: String -> Maybe Name
readName str
| str == "Obj"
= Just $ NameObjTyCon
| Just p <- readPrimTyCon str
= Just $ NamePrimTyCon p
| Just p <- readPrimArith str
= Just $ NamePrimOp $ PrimArith p
| Just p <- readPrimCast str
= Just $ NamePrimOp $ PrimCast p
| Just p <- readPrimCall str
= Just $ NamePrimOp $ PrimCall p
| Just p <- readPrimControl str
= Just $ NamePrimOp $ PrimControl p
| Just p <- readPrimStore str
= Just $ NamePrimOp $ PrimStore p
| str == "V#"
= Just $ NameLitVoid
| Just val <- readLitPrimNat str
= Just $ NameLitNat val
| Just val <- readLitPrimInt str
= Just $ NameLitInt val
| Just rest <- stripPrefix "TAG" str
, (ds, "#") <- span isDigit rest
= Just $ NameLitTag (read ds)
| str == "True#" = Just $ NameLitBool True
| str == "False#" = Just $ NameLitBool False
| Just (val, bits) <- readLitPrimWordOfBits str
, elem bits [8, 16, 32, 64]
= Just $ NameLitWord val bits
| c : _ <- str
, isUpper c
= Just $ NameVar str
| c : _ <- str
, isLower c
= Just $ NameVar str
| otherwise
= Nothing
data PrimOp
= PrimArith PrimArith
| PrimCast PrimCast
| PrimStore PrimStore
| PrimCall PrimCall
| PrimControl PrimControl
deriving (Eq, Ord, Show)
instance NFData PrimOp where
rnf op
= case op of
PrimArith pa -> rnf pa
PrimCast pc -> rnf pc
PrimStore ps -> rnf ps
PrimCall pc -> rnf pc
PrimControl pc -> rnf pc
instance Pretty PrimOp where
ppr pp
= case pp of
PrimArith op -> ppr op
PrimCast c -> ppr c
PrimStore p -> ppr p
PrimCall c -> ppr c
PrimControl c -> ppr c