c-dsl 0.1.0.0 → 0.2
raw patch · 5 files changed
+74/−18 lines, 5 filesdep ~language-c
Dependency ranges changed: language-c
Files
- LICENSE +1/−1
- c-dsl.cabal +2/−2
- src/Language/C/DSL/Decl.hs +53/−13
- src/Language/C/DSL/Exp.hs +17/−2
- src/Language/C/DSL/Stat.hs +1/−0
LICENSE view
@@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) <year> <copyright holders>+Copyright (c) 2014 Daniel Gratzer <jozefg@cmu.edu> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
c-dsl.cabal view
@@ -1,5 +1,5 @@ name: c-dsl-version: 0.1.0.0+version: 0.2 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@@ -21,7 +21,7 @@ Language.C.DSL.StringLike build-depends: base >=4.6 && <4.7,- language-c+ language-c == 0.4.* hs-source-dirs: src/ default-language: Haskell2010
src/Language/C/DSL/Decl.hs view
@@ -5,10 +5,13 @@ import Language.C.DSL.StringLike -- | A low level way to declare something.-decl :: CDeclSpec -> CDeclr -> Maybe CExpr -> CDecl+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 --- | Simple types that can be used in declarations. voidTy, charTy, shortTy, intTy, longTy, floatTy :: CDeclSpec voidTy = CTypeSpec $ CVoidType undefNode charTy = CTypeSpec $ CCharType undefNode@@ -19,35 +22,66 @@ doubleTy = CTypeSpec $ CDoubleType undefNode -- | Modifies a declarator to be a pointer. For example--- @ptr "x"@ would be @*x@ in C.+-- @ptr someName@ would be @*x@ in C. ptr :: CDeclr -> CDeclr ptr (CDeclr nm mods cstr attrs node) = CDeclr nm (CPtrDeclr [] undefNode : mods) cstr attrs node --- | Clever functions that can be applied to declarators, for example @int "x" .= 1@ is equivalent--- to @int x = 1@. To leave a variable uninitialized @int "x" Nothing@ does the job.-char, short, int, long, float, double :: CDeclr -> Maybe CExpr -> CDecl+-- | A short cut for declaring a @char@.+-- +-- > char "x" .= 1+-- > uninit $ char "y"+--+-- Would generate+--+-- > char x = 1;+-- > char y;+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 --- | Equivalent to the above but with pointer wrappers.-charPtr, shortPtr, intPtr, longPtr, floatPtr, doublePtr :: CDeclr -> Maybe CExpr -> CDecl+-- | Equivalent to @'char'@ but wraps the @'CDeclr'@ in a pointer.+-- This means that @uninit $ charPtr someName@ is equivalent to @char *someName;@+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 --- | Supplies an initializer for an expression.++-- | Supplies an initializer for an for a declaration. This+-- is meant to be used with the 'char' and friends short cuts (.=) :: (Maybe CExpr -> CDecl) -> CExpr -> CDecl f .= e = f (Just e) infixl 7 .= --- | Leave a declaration uninitialized. For example @uninit $ char "x"@.+-- | Leave a declaration uninitialized. This is meant to be used+-- with the 'char' and friends declaration uninit :: (Maybe CExpr -> CDecl) -> CDecl uninit = ($ Nothing) @@ -59,18 +93,24 @@ 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, union :: String -> [(String, CTypeSpec)] -> CDecl--- ^ Create a structure or union, for example @struct "foo" [("bar", intTy)]@ is+-- | Create a structure, for example @struct "foo" [("bar", intTy)]@ is -- @typedef struct foo {int bar;} foo;@+struct :: String -> [(String, CTypeSpec)] -> CDecl struct = csu CStructTag++-- | Equivalent to 'struct' but generates a C union instead.+union :: String -> [(String, CTypeSpec)] -> CDecl union = csu CUnionTag --- Defines a C function. For example+-- | Defines a C function. For example+-- -- > test = -- > fun [intTy] "test"[int "a", int "b"] $ hblock [ -- > creturn ("a" + "b") -- > ]+-- -- Would be the equivalent of+-- -- > int test(int a, int b) -- > { -- > return a + b;
src/Language/C/DSL/Exp.hs view
@@ -14,13 +14,28 @@ cOp :: CBinaryOp -> CExpr -> CExpr -> CExpr cOp op a b = CBinary op a b undefNode --- | Comparison operators, normal haskell operators with @:@ at the end.-(==:), (/=:), (<:), (>:), (<=:), (>=:) :: CExpr -> CExpr -> CExpr+-- | Equality test, @a ==: b@ is equivalent to @a == b@+(==:) :: CExpr -> CExpr -> CExpr (==:) = cOp CEqOp++-- | Inequality test, @a /=: b@ is equivalent to @a != b@+(/=:) :: CExpr -> CExpr -> CExpr (/=:) = cOp CNeqOp++-- | Less-than test, @a <: b@ is equivalent to @a < b@+(<:) :: CExpr -> CExpr -> CExpr (<:) = cOp CLeOp++-- | Greater-than test, @a >: b@ is equivalent to @a > b@+(>:) :: CExpr -> CExpr -> CExpr (>:) = cOp CGrOp++-- | Less than or equal to, @a <=: b@ is equivalent to @a <= b@+(<=:) :: CExpr -> CExpr -> CExpr (<=:) = cOp CLeqOp++-- | Greater than or equal to, @a >=: b@ is equivalent to @a >= b@+(>=:) :: CExpr -> CExpr -> CExpr (>=:) = cOp CGeqOp -- | The ternary operator in C. @ternary a b c@ will turn into @a ? b : c@.
src/Language/C/DSL/Stat.hs view
@@ -15,6 +15,7 @@ while exp stat = CWhile exp stat False undefNode -- | A for loop, an example use+-- -- > for(int "x" .= 1, 1, PlusPlus `pre` "x") -- > "printf"#[str "%d\n", "x"] for :: (CDecl, CExpr, CExpr) -> CStat -> CStat