blob: 8b2a9d5af065f7578110f0bfaa68a22018918595 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
module Language.C where
data Program = Program [FunDef]
deriving (Show)
data FunDef
= 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
| TUInt Bits
| TFloat
| TDouble
| TPtr Type
deriving (Show, Eq)
-- | The number of bits in a C integral type.
data Bits = B8 | B16 | B32 | B64
deriving (Show, Eq)
-- | A C variable or function name.
newtype Name = Name String
deriving (Show, Eq, Ord)
-- Statement expression: conceptually gcc's ({ ... })
data StExpr = StExpr [Stmt] Expr
deriving (Show, Eq)
data Stmt
= SDecl Type Name (Maybe Expr)
| SAsg Name Expr
| SStore Name Expr Expr -- ^ name[expr] = expr
| SCall Name [Expr]
-- | @SFor ty i lo hi body@: @for (ty i = lo; i < hi; i++) body@
| SFor Type Name Expr Expr [Stmt]
-- | @SWhile pre cond post@: @while (true) { pre; if (!cond) break; post; }@
-- Special case if @pre == []@: @while (cond) { post; }
| SWhile [Stmt] Expr [Stmt]
| SIf Expr [Stmt] [Stmt]
deriving (Show, Eq)
data Expr
= EOp Expr String Expr
| ENot Expr -- ^ @!expr@
| ELit String
| EVar Name
| ECall Name [Expr]
| EIndex Name Expr
| EPtrTo Expr
| ESizeOf Type
| ECast Type Expr
deriving (Show, Eq)
fundefName :: FunDef -> Name
fundefName (FunDef _ _ n _ _) = n
fundefName (ProcDef _ n _ _) = n
|