module Language.C.DSL.Decl where
import Language.C
import Data.String
import Language.C.DSL.StringLike
decl :: CDeclSpec
-> CDeclr
-> Maybe CExpr
-> CDecl
decl ty name exp = CDecl [ty] [(Just name, flip CInitExpr undefNode `fmap` exp, Nothing)] undefNode
voidSpec :: CTypeSpec
voidSpec = CVoidType undefNode
charSpec :: CTypeSpec
charSpec = CCharType undefNode
shortSpec :: CTypeSpec
shortSpec = CShortType undefNode
intSpec :: CTypeSpec
intSpec = CIntType undefNode
longSpec :: CTypeSpec
longSpec = CLongType undefNode
floatSpec :: CTypeSpec
floatSpec = CFloatType undefNode
doubleSpec :: CTypeSpec
doubleSpec = CDoubleType undefNode
voidTy :: CDeclSpec
voidTy = CTypeSpec $ CVoidType undefNode
charTy :: CDeclSpec
charTy = CTypeSpec $ CCharType undefNode
shortTy :: CDeclSpec
shortTy = CTypeSpec $ CShortType undefNode
intTy :: CDeclSpec
intTy = CTypeSpec $ CIntType undefNode
longTy :: CDeclSpec
longTy = CTypeSpec $ CLongType undefNode
floatTy :: CDeclSpec
floatTy = CTypeSpec $ CFloatType undefNode
doubleTy :: CDeclSpec
doubleTy = CTypeSpec $ CDoubleType undefNode
ty :: Ident -> CTypeSpec
ty = flip CTypeDef undefNode
ptr :: CDeclr -> CDeclr
ptr (CDeclr nm mods cstr attrs node) = CDeclr nm (CPtrDeclr [] undefNode : mods) cstr attrs node
char :: CDeclr -> Maybe CExpr -> CDecl
char = decl charTy
short :: CDeclr -> Maybe CExpr -> CDecl
short = decl shortTy
int :: CDeclr -> Maybe CExpr -> CDecl
int = decl intTy
long :: CDeclr -> Maybe CExpr -> CDecl
long = decl longTy
float :: CDeclr -> Maybe CExpr -> CDecl
float = decl floatTy
double :: CDeclr -> Maybe CExpr -> CDecl
double = decl doubleTy
charPtr :: CDeclr -> Maybe CExpr -> CDecl
charPtr = char . ptr
shortPtr :: CDeclr -> Maybe CExpr -> CDecl
shortPtr = short . ptr
intPtr :: CDeclr -> Maybe CExpr -> CDecl
intPtr = int . ptr
longPtr :: CDeclr -> Maybe CExpr -> CDecl
longPtr = long . ptr
floatPtr :: CDeclr -> Maybe CExpr -> CDecl
floatPtr = float . ptr
doublePtr:: CDeclr -> Maybe CExpr -> CDecl
doublePtr = double . ptr
(.=) :: (Maybe CExpr -> CDecl) -> CExpr -> CDecl
f .= e = f (Just e)
infixl 7 .=
uninit :: (Maybe CExpr -> CDecl) -> CDecl
uninit = ($ Nothing)
csu :: CStructTag -> String -> [(String, CTypeSpec)] -> CDecl
csu tag ident fields = CDecl
[CStorageSpec $ CTypedef undefNode, CTypeSpec $ CSUType structTy undefNode]
[(Just $ fromString ident, Nothing, Nothing)]
undefNode
where structTy = CStruct tag (Just $ fromString ident) (Just $ map structify fields) [] undefNode
structify (name, ty) = CDecl [CTypeSpec ty] [(Just (fromString name), Nothing, Nothing)] undefNode
struct :: String -> [(String, CTypeSpec)] -> CDecl
struct = csu CStructTag
union :: String -> [(String, CTypeSpec)] -> CDecl
union = csu CUnionTag
fun :: [CDeclSpec] -> String -> [Maybe CExpr -> CDecl] -> CStat -> CFunDef
fun specs name args body = annotatedFun specs name args [] body
annotatedFun :: [CDeclSpec] -> String -> [Maybe CExpr -> CDecl] -> [String] -> CStat -> CFunDef
annotatedFun specs name args annots body = CFunDef specs decl [] body undefNode
where decl = CDeclr (Just $ fromString name)
[CFunDeclr (Right (fmap ($Nothing) args, False)) [] undefNode]
Nothing attrs undefNode
attrs :: [CAttr]
attrs = map (\ s -> CAttr (fromString s) [] undefNode) annots
class External a where
export :: a -> CExtDecl
instance External CFunDef where
export = CFDefExt
instance External CDecl where
export = CDeclExt
instance External CStrLit where
export = flip CAsmExt undefNode
transUnit :: [CExtDecl] -> CTranslUnit
transUnit = flip CTranslUnit undefNode