-
Notifications
You must be signed in to change notification settings - Fork 11
/
Syntax.lean
182 lines (136 loc) · 5.17 KB
/
Syntax.lean
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/-
Copyright (c) 2022 Mac Malone. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mac Malone
-/
import Alloy.C.Grammar
/-!
# C Syntax Utilities
Definitions for dealing with C-specific syntax.
-/
open Lean
namespace Alloy.C
/-!
## Typed Syntax Helpers
-/
/-! ### Categories -/
abbrev Cmd := TSyntax `cCmd
abbrev PPCmd := TSyntax `ppCmd
abbrev ExternDecl := TSyntax `cExternDecl
abbrev DirectAbsDeclarator := TSyntax `cDirectAbsDeclarator
abbrev DirectDeclarator := TSyntax `cDirectDeclarator
abbrev Index := TSyntax `cIndex
abbrev DeclSpec := TSyntax `cDeclSpec
abbrev StorageClassSpec := TSyntax `cStorageClassSpec
abbrev FunSpec := TSyntax `cFunSpec
abbrev Spec := TSyntax `cSpec
abbrev TypeSpec := TSyntax `cTypeSpec
abbrev TypeQ := TSyntax `cTypeQ
abbrev Designator := TSyntax `cDesignator
abbrev Initializer := TSyntax `cInitializer
abbrev Expr := TSyntax `cExpr
abbrev AssignOp := TSyntax `cAssignOp
abbrev Stmt := TSyntax `cStmt
abbrev StmtLike := TSyntax `cStmtLike
/-! ### Kinds -/
abbrev Pointer := TSyntax ``pointer
abbrev AlignSpec := TSyntax ``alignSpec
abbrev ParamDecl := TSyntax ``paramDecl
abbrev Params := TSyntax ``params
abbrev Function := TSyntax ``function
abbrev InitializerElem := TSyntax ``initializerElem
abbrev Declarator := TSyntax ``declarator
abbrev AbsDeclarator := TSyntax ``absDeclarator
abbrev InitDeclarator := TSyntax ``initDeclarator
abbrev AggrDeclarator := TSyntax ``aggrDeclarator
abbrev AggrSig := TSyntax ``aggrSig
abbrev Enumerator := TSyntax ``enumerator
abbrev EnumSig := TSyntax ``enumSig
abbrev Declaration := TSyntax ``declaration
abbrev CompStmt := TSyntax ``compStmt
abbrev LabelStmt := TSyntax ``labelStmt
abbrev ConstExpr := TSyntax ``constExpr
/-! ### Coercions -/
instance : Coe Ident Expr where
coe x := Unhygienic.run `(cExpr| $x:ident)
instance : Coe NumLit Expr where
coe x := Unhygienic.run `(cExpr| $x:num)
instance : Coe ScientificLit Expr where
coe x := Unhygienic.run `(cExpr| $x:scientific)
instance : Coe StrLit Expr where
coe x := Unhygienic.run `(cExpr| $x:str)
instance : Coe CharLit Expr where
coe x := Unhygienic.run `(cExpr| $x:char)
instance : Coe ConstExpr Expr where
coe x :=
match x with
| `(constExpr| $x:cExpr) => Unhygienic.run `(cExpr| $x)
| _ => ⟨.missing⟩
instance : Coe Expr ConstExpr where
coe x := Unhygienic.run `(constExpr| $x:cExpr)
instance : Coe Expr Initializer where
coe x := Unhygienic.run `(cInitializer| $x:cExpr)
instance : Coe Initializer InitializerElem where
coe x := Unhygienic.run `(initializerElem| $x:cInitializer)
instance : Coe Expr Index where
coe x := Unhygienic.run `(cIndex| $x:cExpr)
instance : Coe Pointer AbsDeclarator where
coe x := Unhygienic.run `(absDeclarator| $x:pointer)
instance : Coe DirectAbsDeclarator AbsDeclarator where
coe x := Unhygienic.run `(absDeclarator| $x:cDirectAbsDeclarator)
instance : Coe Ident DirectDeclarator where
coe x := Unhygienic.run `(cDirectDeclarator| $x:ident)
instance : Coe DirectDeclarator Declarator where
coe x := Unhygienic.run `(declarator| $x:cDirectDeclarator)
instance : Coe Declarator InitDeclarator where
coe x := Unhygienic.run `(initDeclarator| $x:declarator)
instance : Coe Declarator AggrDeclarator where
coe x := Unhygienic.run `(aggrDeclarator| $x:declarator)
instance : Coe Ident AggrSig where
coe x := Unhygienic.run `(aggrSig| $x:ident)
instance : Coe Ident Enumerator where
coe x := Unhygienic.run `(enumerator| $x:ident)
instance : Coe Ident EnumSig where
coe x := Unhygienic.run `(enumSig| $x:ident)
instance : Coe Ident TypeSpec where
coe x := Unhygienic.run `(cTypeSpec| $x:ident)
instance : Coe TypeQ Spec where
coe x := Unhygienic.run `(cSpec| $x:cTypeQ)
instance : Coe TypeSpec Spec where
coe x := Unhygienic.run `(cSpec| $x:cTypeSpec)
instance : Coe AlignSpec Spec where
coe x := Unhygienic.run `(cSpec| $x:alignSpec)
instance : Coe Spec DeclSpec where
coe x := Unhygienic.run `(cDeclSpec| $x:cSpec)
instance : Coe StorageClassSpec DeclSpec where
coe x := Unhygienic.run `(cDeclSpec| $x:cStorageClassSpec)
instance : Coe FunSpec DeclSpec where
coe x := Unhygienic.run `(cDeclSpec| $x:cFunSpec)
instance : Coe DeclSpec ParamDecl where
coe x := Unhygienic.run `(paramDecl|$x:cDeclSpec)
instance : Coe Stmt StmtLike where
coe x := Unhygienic.run `(cStmtLike| $x:cStmt)
instance : Coe Declaration StmtLike where
coe x := Unhygienic.run `(cStmtLike| $x:declaration)
instance : Coe CompStmt Stmt where
coe x := Unhygienic.run `(cStmt| $x:compStmt)
instance : Coe LabelStmt Stmt where
coe x := Unhygienic.run `(cStmt| $x:labelStmt)
instance : Coe Function ExternDecl where
coe x := Unhygienic.run `(cExternDecl| $x:function)
instance : Coe Declaration ExternDecl where
coe x := Unhygienic.run `(cExternDecl| $x:declaration)
instance : Coe ExternDecl Cmd where
coe x := Unhygienic.run `(cCmd| $x:cExternDecl)
instance : Coe PPCmd Cmd where
coe x := Unhygienic.run `(cCmd| $x:ppCmd)
/-!
## Other Helpers
-/
def packBody (stmts : Array StmtLike) : CompStmt :=
if h : 0 < stmts.size then
match stmts[0]'h with
| `(cStmtLike| $x:compStmt) => x
| _ => Unhygienic.run `(compStmt| {$stmts*})
else
Unhygienic.run `(compStmt| {$stmts*})