From 961b6fc01f9c2f0220070849d22b2a30ca031324 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sat, 9 Oct 2021 10:19:46 +0200 Subject: Static C functions; clean up --- Language/C.hs | 15 +++++++++++---- Language/C/Print.hs | 10 ++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) (limited to 'Language') diff --git a/Language/C.hs b/Language/C.hs index 6a3256f..9a65115 100644 --- a/Language/C.hs +++ b/Language/C.hs @@ -5,10 +5,17 @@ data Program = Program [FunDef] deriving (Show) data FunDef - = FunDef Type Name [(Type, Name)] StExpr - | ProcDef Name [(Type, Name)] [Stmt] + = FunDef FunAttrs Type Name [(Type, Name)] StExpr + | ProcDef FunAttrs Name [(Type, Name)] [Stmt] deriving (Show, Eq) +data FunAttrs = FunAttrs + { faStatic :: Bool } + deriving (Show, Eq) + +defAttrs :: FunAttrs +defAttrs = FunAttrs { faStatic = False } + -- | Some C types. data Type = TInt Bits @@ -53,5 +60,5 @@ data Expr fundefName :: FunDef -> Name -fundefName (FunDef _ n _ _) = n -fundefName (ProcDef n _ _) = n +fundefName (FunDef _ _ n _ _) = n +fundefName (ProcDef _ n _ _) = n diff --git a/Language/C/Print.hs b/Language/C/Print.hs index cc511b2..5852601 100644 --- a/Language/C/Print.hs +++ b/Language/C/Print.hs @@ -14,15 +14,17 @@ printProgram :: Program -> PrintS printProgram (Program defs) = intercalates "\n" (map printFunDef defs) printFunDef :: FunDef -> PrintS -printFunDef (FunDef rt n as (StExpr ss rete)) = - printType rt % printString " " % printName n +printFunDef (FunDef attrs rt n as (StExpr ss rete)) = + printString (if faStatic attrs then "static " else "") + % printType rt % printString " " % printName n % printString "(" % intercalates ", " [printType t % printString " " % printName an | (t, an) <- as] % printString ") {\n " % addIndent 2 (intercalates "\n" (map printStmt ss)) % printString "\n return (" % printExpr rete % printString ");\n}\n" -printFunDef (ProcDef n as ss) = - printString "void " % printName n +printFunDef (ProcDef attrs n as ss) = + printString (if faStatic attrs then "static " else "") + % printString "void " % printName n % printString "(" % intercalates ", " [printType t % printString " " % printName an | (t, an) <- as] % printString ") " % printBlock ss % printString "\n" -- cgit v1.2.3-70-g09d2