c-dsl 0.2 → 0.3
raw patch · 4 files changed
+111/−18 lines, 4 files
Files
- c-dsl.cabal +2/−2
- src/Language/C/DSL.hs +24/−5
- src/Language/C/DSL/Decl.hs +83/−11
- src/Language/C/DSL/StringLike.hs +2/−0
c-dsl.cabal view
@@ -1,8 +1,8 @@ name: c-dsl-version: 0.2+version: 0.3 synopsis: A higher level DSL on top of language-c description: This DSL is meant to make it easier to write language-c's DSL by providing simple- functions/operators for writing C AST's in Haskell+ functions/operators for writing C AST's in Haskell. license: MIT license-file: LICENSE author: Danny Gratzer
src/Language/C/DSL.hs view
@@ -1,8 +1,27 @@--- | This module simply exists to export the entirety of the DSL and as--- a convenience, "Language.C".---+{-# LANGUAGE OverloadedStrings #-}+-- | This module provides a more pleasant way to write C ASTs for language-c+-- As a simple example,+-- +-- > {-# LANGUAGE OverloadedStrings #-}+-- >+-- > import Language.C.DSL+-- >+-- > example :: CFunDef+-- > example =+-- > fun [intTy] "foo"[int "a", int "b"] $ block [+-- > creturn $ "a" + "b"+-- > ]+-- >+-- +-- And when loaded into GHCi+-- +-- > Main*> pretty example+-- > int foo(int a, int b)+-- > {+-- > return a + b;+-- > }+-- +-- This module also exports "Language.C" for simplicity. module Language.C.DSL ( module Language.C.DSL.StringLike, module Language.C.DSL.Exp,
src/Language/C/DSL/Decl.hs view
@@ -5,22 +5,83 @@ import Language.C.DSL.StringLike -- | A low level way to declare something.-decl :: CDeclSpec -- ^ The declaration specifier, usually this is a type- -> CDeclr -- ^ Equivalent to the name of the object being declared. Often this will- -- make use of the overloaded string instance for 'CDeclr's+decl :: CDeclSpec -- ^ The declaration specifier, usually this is a type+ -> CDeclr -- ^ Equivalent to the name of the object being declared. Often this will+ -- make use of the overloaded string instance for 'CDeclr's -> Maybe CExpr -- ^ The optional init expression -> CDecl decl ty name exp = CDecl [ty] [(Just name, flip CInitExpr undefNode `fmap` exp, Nothing)] undefNode -voidTy, charTy, shortTy, intTy, longTy, floatTy :: CDeclSpec-voidTy = CTypeSpec $ CVoidType undefNode-charTy = CTypeSpec $ CCharType undefNode+-- | The 'CTypeSpec' for @void@+voidSpec :: CTypeSpec+voidSpec = CVoidType undefNode++-- | The 'CTypeSpec' for @char@+charSpec :: CTypeSpec+charSpec = CCharType undefNode++-- | The 'CTypeSpec' for @short@+shortSpec :: CTypeSpec+shortSpec = CShortType undefNode++-- | The 'CTypeSpec' for @int@+intSpec :: CTypeSpec+intSpec = CIntType undefNode++-- | The 'CTypeSpec' for @long@+longSpec :: CTypeSpec+longSpec = CLongType undefNode++-- | The 'CTypeSpec' for @float@+floatSpec :: CTypeSpec+floatSpec = CFloatType undefNode++-- | The 'CTypeSpec' for @double@+doubleSpec :: CTypeSpec+doubleSpec = CDoubleType undefNode+++-- | The 'CDeclSpec' for declarations of type @void@+voidTy :: CDeclSpec+voidTy = CTypeSpec $ CVoidType undefNode++-- | The 'CDeclSpec' for declarations of type @char@+charTy :: CDeclSpec+charTy = CTypeSpec $ CCharType undefNode++-- | The 'CDeclSpec' for declarations of type @short@+shortTy :: CDeclSpec shortTy = CTypeSpec $ CShortType undefNode-intTy = CTypeSpec $ CIntType undefNode-longTy = CTypeSpec $ CLongType undefNode++-- | The 'CDeclSpec' for declarations of type @int@+intTy :: CDeclSpec+intTy = CTypeSpec $ CIntType undefNode++-- | The 'CDeclSpec' for declarations of type @long@+longTy :: CDeclSpec+longTy = CTypeSpec $ CLongType undefNode++-- | The 'CDeclSpec' for declarations of type @float@+floatTy :: CDeclSpec floatTy = CTypeSpec $ CFloatType undefNode++-- | The 'CDeclSpec' for declarations of type @double@+doubleTy :: CDeclSpec doubleTy = CTypeSpec $ CDoubleType undefNode ++-- | Turns a string into the corresponding typedefed type.+-- +-- For example+-- +-- > struct "foo" [("bar, ty "quux")]+-- +-- will generate the corresponding+-- +-- > typedef foo {quux bar;} foo+ty :: Ident -> CTypeSpec+ty = flip CTypeDef undefNode+ -- | Modifies a declarator to be a pointer. For example -- @ptr someName@ would be @*x@ in C. ptr :: CDeclr -> CDeclr@@ -38,18 +99,23 @@ char :: CDeclr -> Maybe CExpr -> CDecl char = decl charTy +-- | A short cut for declaring a @short@ short :: CDeclr -> Maybe CExpr -> CDecl short = decl shortTy +-- | A short cut for declaring a @int@ int :: CDeclr -> Maybe CExpr -> CDecl int = decl intTy +-- | A short cut for declaring a @long@ long :: CDeclr -> Maybe CExpr -> CDecl long = decl longTy +-- | A short cut for declaring a @float@ float :: CDeclr -> Maybe CExpr -> CDecl float = decl floatTy +-- | A short cut for declaring a @double@ double :: CDeclr -> Maybe CExpr -> CDecl double = decl doubleTy @@ -116,10 +182,17 @@ -- > return a + b; -- > } fun :: [CDeclSpec] -> String -> [Maybe CExpr -> CDecl] -> CStat -> CFunDef-fun specs name args body = CFunDef specs decl [] body undefNode+fun specs name args body = annotatedFun specs name args [] body++-- | Identical to fun except this annotates the list of attributes given+-- as a list of strings.+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 [] undefNode+ Nothing attrs undefNode+ attrs :: [CAttr]+ attrs = map (\ s -> CAttr (fromString s) [] undefNode) annots class External a where export :: a -> CExtDecl@@ -133,4 +206,3 @@ -- | Exports a series of declarations to a translation unit. transUnit :: [CExtDecl] -> CTranslUnit transUnit = flip CTranslUnit undefNode-
src/Language/C/DSL/StringLike.hs view
@@ -12,3 +12,5 @@ fromString str = CDeclr (Just $ fromString str) [] Nothing [] undefNode instance IsString CDecl where fromString str = CDecl [CTypeSpec (CTypeDef (fromString str) undefNode)] [] undefNode+instance IsString CTypeSpec where+ fromString = flip CTypeDef undefNode . fromString