packages feed

language-c99-simple 0.1.2 → 0.2.0

raw patch · 4 files changed

+65/−13 lines, 4 filesdep ~language-c99dep ~language-c99-util

Dependency ranges changed: language-c99, language-c99-util

Files

ChangeLog.md view
@@ -1,5 +1,10 @@ # Revision history for language-c99-simple +## 0.2.0 -- 2022-05-21++* Added support for initializers with designators.+* Added support for enum and union.+ ## 0.1.2 -- 2019-05-12  * Added support for storagespec.
language-c99-simple.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/  name:                language-c99-simple-version:             0.1.2+version:             0.2.0 synopsis:            C-like AST to simplify writing C99 programs. description:   This package is a wrapper on top of 'language-c99'. It provides a simpler@@ -33,8 +33,8 @@   -- other-modules:          -- other-extensions:       build-depends:       base >=4.9 && <5,-                       language-c99 >= 0.1 && < 0.2,-                       language-c99-util >= 0.1 && < 0.2,+                       language-c99 >= 0.2 && < 0.3,+                       language-c99-util >= 0.2 && < 0.3,                        mtl >= 2.2.0 && < 2.3   hs-source-dirs:      src   default-language:    Haskell2010
src/Language/C99/Simple/AST.hs view
@@ -11,6 +11,7 @@ module Language.C99.Simple.AST where  import Prelude hiding (LT, GT)+import Data.List.NonEmpty (NonEmpty)  type Ident    = String @@ -85,13 +86,21 @@               | TypedefName Ident                | Struct      Ident-              | StructDecln (Maybe Ident) [FieldDecln]+              | StructDecln (Maybe Ident) (NonEmpty FieldDecln) +              | Union      Ident+              | UnionDecln (Maybe Ident) (NonEmpty FieldDecln)++              | Enum      Ident+              | EnumDecln (Maybe Ident) (NonEmpty Ident)+ data FieldDecln = FieldDecln Type Ident -data Init = InitExpr  Expr-          | InitArray [Init]+data Init = InitExpr Expr+          | InitList (NonEmpty InitItem) +data InitItem = InitItem (Maybe Ident) Init+ data Expr = Ident     Ident           | LitBool   Bool           | LitInt    Integer@@ -103,7 +112,7 @@           | Funcall Expr [Expr]           | Dot     Expr Ident           | Arrow   Expr Ident-          | InitVal TypeName [Init]+          | InitVal TypeName (NonEmpty InitItem)            | UnaryOp UnaryOp Expr 
src/Language/C99/Simple/Translate.hs view
@@ -4,6 +4,8 @@  import GHC.Exts             (fromList) import Control.Monad.State  (State, execState, get, put)+import Data.List.NonEmpty (NonEmpty)+import qualified Data.List.NonEmpty as NE  import           Language.C99.Simple.AST import qualified Language.C99.AST         as C@@ -157,15 +159,35 @@   TypedefName name -> [C.TTypedef $ C.TypedefName $ ident name]   Struct      name -> [C.TStructOrUnion $ C.StructOrUnionForwDecln C.Struct (ident name)]   StructDecln name declns -> [C.TStructOrUnion $ C.StructOrUnionDecln C.Struct (ident <$> name) declns'] where-    declns' = fromList $ map transfielddecln declns+    declns' = transfielddeclns declns+  Union      name -> [C.TStructOrUnion $ C.StructOrUnionForwDecln C.Union (ident name)]+  UnionDecln name declns -> [C.TStructOrUnion $ C.StructOrUnionDecln C.Union (ident <$> name) declns'] where+    declns' = transfielddeclns declns+  Enum      name -> [C.TEnum $ C.EnumSpecForw (ident name)]+  EnumDecln name declns -> [C.TEnum $ C.EnumSpec (ident <$> name) declns'] where+    declns' = transvariantdeclns declns +transfielddeclns :: NonEmpty FieldDecln -> C.StructDeclnList+transfielddeclns (decln NE.:| []) = C.StructDeclnBase (transfielddecln decln)+transfielddeclns (decln NE.:| (d : ds)) = C.StructDeclnCons+  (transfielddeclns (d NE.:| ds))+  (transfielddecln decln)+ transfielddecln :: FieldDecln -> C.StructDecln transfielddecln (FieldDecln ty name) = C.StructDecln quals declrlist where   declrlist = C.StructDeclrBase $ C.StructDeclr declr   declr = execState (getdeclr ty) (identdeclr name)   quals = getspecquals ty +transvariantdeclns :: NonEmpty Ident -> C.EnumrList+transvariantdeclns (decln NE.:| []) = C.EnumrBase (transvariantdecln decln)+transvariantdeclns (decln NE.:| (d : ds)) = C.EnumrCons+  (transvariantdeclns (d NE.:| ds))+  (transvariantdecln decln) +transvariantdecln :: Ident -> C.Enumr+transvariantdecln name = C.Enumr (C.Enum (ident name))+ getspecquals :: Type -> C.SpecQualList getspecquals ty = case ty of   Type     ty'     -> getspecquals ty'@@ -252,12 +274,28 @@     AssignOr     -> C.AOr  transinit :: Init -> C.Init-transinit (InitExpr e)   = C.InitExpr (wrap $ transexpr e)-transinit (InitArray es) = C.InitArray (fromList $ map transinit es)+transinit (InitExpr e)  = C.InitExpr (wrap $ transexpr e)+transinit (InitList es) = C.InitList (transinitlist es) -initexpr ty init = C.PostfixInits ty' init' where-  ty'   = transtypename ty-  init' = fromList $ map transinit init+transinitlist :: NonEmpty InitItem -> C.InitList+transinitlist (InitItem mident x NE.:| [])     = C.InitBase+  (transdesigr <$> mident) (transinit x)+transinitlist (InitItem mident x NE.:| (y:zs)) = C.InitCons+  (transinitlist (y NE.:| zs)) (transdesigr <$> mident) (transinit x)++transdesigr :: Ident -> C.Design+transdesigr = C.Design . C.DesigrBase . C.DesigrIdent . ident++initexpr :: TypeName -> NonEmpty InitItem -> C.PostfixExpr+initexpr ty inits = C.PostfixInits ty' inits' where+  ty'    = transtypename ty+  inits' = transinititems inits++transinititems :: NonEmpty InitItem -> C.InitList+transinititems (InitItem mident init NE.:| []) =+  C.InitBase ((C.Design . C.DesigrBase . C.DesigrIdent . ident) <$> mident) (transinit init)+transinititems (InitItem mident init NE.:| (x:xs)) =+  C.InitCons (transinititems (x NE.:| xs)) ((C.Design . C.DesigrBase . C.DesigrIdent . ident) <$> mident) (transinit init)  indexexpr arr idx = C.PostfixIndex arr' idx' where   arr' = wrap $ transexpr arr